0、查看模块帮助文档
[root@m01 ~]# ansible-doc -l # 查看所有模块
[root@m01 ~]# ansible-doc 模块名称 # 查看指定模块帮助文档
[root@m01 ~]# ansible-doc -s 模块名称 # 查看模块可以使用的参数
详细语法请自行查看帮助文档,以下只列基础常用的。
1、shell
用于执行shell命令
示例:
1、ad-hoc
[root@m1 test]# ansible web -m shell -a "ip a show ens33 | awk '/inet /{print \$2}'" # 注意$符号需要转义
web02 | CHANGED | rc=0 >>
10.0.0.8/24
web01 | CHANGED | rc=0 >>
10.0.0.7/24
2、playbook
[root@m1 test]# vim shell.yml
- hosts: web
tasks:
- name: shell test
shell: "ip a show ens33 | awk '/inet /{print $2}'" # 不需要转义,默认结果不显示
register: Restult # 将结果赋予给Restult变量
- name: print Restult
debug:
msg: "{{ Restult.stdout_lines }}"
2、script
用于执行脚本
示例:
[root@m1 test]# ansible web -m script -a "/script/test.sh"
[root@m1 test]# vim script.yml
- hosts: web
tasks:
- name: script test
script: /script/test.sh
3、yum_repository
用于配置yum仓库。一般采用控制端配置好yum源文件再推送过去的方式,这种并不常用。
参数:
name: yum源名称
file: yum源文件的名字
description: yum源中name的值
baseurl: yum源中的仓库地址
gpgcheck: yum源是否检查
enabled: 是否激活yum源
no
yes 默认
state:
absent 删除
present 添加
示例:
[root@m1 test]# vim yum_repository.yml
- hosts: web1
tasks:
- name: Add nginx repository
yum_repository:
name: nginx-stable
description: nginx stable repo
file: nginx
baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck: no
enabled: yes
[root@web1 yum.repos.d]# cat nginx.repo
[nginx-stable]
baseurl = http://nginx.org/packages/centos/$releasever/$basearch/
enabled = 1
gpgcheck = 0
name = nginx stable repo
注意:相同file名,即往这个文件中添加源。不同file名,才是新创建一个yum文件。
4、yum
用于yum安装软件
参数:
name: 软件包名称或者包的路径
state:
present 安装
absent 卸载
latest 安装最新版本
enablerepo: 指定通过那个仓库下载
disablerepo: 禁止从哪个仓库下载
示例:
- hosts: web1
tasks:
- name: yum test
yum:
name:
- httpd
- vim
- wget
state: present
ignore_errors: yes
5、copy
用于将本地的文件目录拷贝到远程主机。注意copy模块不能识别需要推送文件中ansible自定义的变量,如需要识别,得使用template模块,用法基本相同。
参数:
src: 推送文件的源路径
dest: 推送文件的目标路径
owner: 将本地文件推送到远端,指定文件属主信息。
group: 将本地文件推送到远端,指定文件属组信息。
mode: 将本地文件推送到远端,指定文件权限信息。
content: 当不使用src指定拷贝的文件时,可以使用content直接指定文件内容。src和content不能同时使用。
backup: 是否备份。推送文件时,如果远程主机有同名文件且内容不同,先对远程主机的同名文件进行备份(按时间戳),再推送文件。
yes
no
示例:
[root@m1 test]# vim copy.yml
- hosts: web1
tasks:
- name: copy test
copy:
src: /test/1.txt
dest: /root/1.txt
owner: www
group: www
mode: 0600
backup: yes
[root@web1 ~]# ll
total 37408
-rw------- 1 www www 4 Sep 17 06:38 1.txt
-rw------- 1 www www 6 Sep 17 06:37 1.txt.16591.2020-09-17@06:38:02~
6、template
基本用法同以上 copy 模块参数,可以识别 ansiblen 内置和自定义的变量。
7、file
用于完成一些对文件的基本操作,创建、删除、修改权限等。
参数:
path: 指定创建的文件或者目录的路径
owner: 指定创建文件或目录的属主
group: 指定创建文件或目录的属组
mode: 指定创建文件或目录的权限
state:
touch 创建文件
directory 创建目录
absent 删除
link 软链接
recurse: 是否递归授权目录文件权限
yes
no
src: 源文件(如果做软链接就是远程机器上的文件)
dest: 目标文件(如果做软链接就是远程机器上的链接文件)
示例:
[root@m1 test]# vim file.yml
- hosts: web1
tasks:
- name: file test
file:
path: /root/test1/test2
state: directory
owner: www
group: www
mode: 0700
[root@web1 ~]# ll -d {/root/test1,/root/test1/test2}
drwx------ 3 www www 19 Sep 17 07:17 /root/test1
drwx------ 2 www www 6 Sep 17 07:17 /root/test1/test2
8、get_url
用于下载网络文件
参数:
url: 下载文件的地址
dest: 下载保存的路径
mode: 下载之后的权限
示例:
[root@m1 test]# vim get_url.yml
- hosts: web1
tasks:
- name: get_url test
get_url:
url: https://www.cpweb.top/wp-content/uploads/2020/09/test.txt
dest: /root/
mode: 0600
[root@web1 ~]# ll test.txt
-rw------- 1 root root 17 Sep 17 07:26 test.txt
9、systemd
用于systemctl 管理程序
参数:
name: 服务名称
state:
started 启动
reloaded 重载
stopped 停止
restarted 重启
enabled:
yes 加入开机自启动
no 关闭开机自启动
daemon_reload: 在执行任何其他操作之前运行 daemon-reload,以确保 systemd 已读取任何更改。
yes 即使模块没有启动或停止任何东西,也会运行daemon-reload
no 默认
示例:
[root@m1 test]# vim systemd.yml
- hosts: web1
tasks:
- name: systemd test
systemd:
name: nginx
state: restarted
enabled: yes
10、group
用于管理远程主机上的组
参数:
name: 组名称
gid: 组gid
state:
present 创建
absent 删除
示例:
[root@m1 test]# vim group.yml
- hosts: web1
tasks:
- name: group test
group:
name: test
gid: 6666
state: present
11、user
用于管理远程主机上的用户
参数:
name: 用户名称
uid: 用户uid
group: 指定组
shell: 指定登陆shell
groups: 指定附加组,默认覆盖原有附加组
comment: 指定用户的注释信息
append: 是否追加附加组到现有的附加组
yes
no
create_home: 是否创建用户家目录
yes
no
remove: 是否移除用户相关的文件
yes
no
state
present 创建
absent 删除
示例:
[root@m1 test]# vim user.yml
- hosts: web1
tasks:
- name: user test
user:
name: test
uid: '6666'
group: test
shell: /sbin/nologin
create_home: no
12、selinux
用于设置selinux安全子系统状态
参数:
state:
disabled 禁用
enforcing 启用
permissive 遇到服务越权访问时,只发出警告而不强制拦截
示例:
[root@m1 test]# vim firewalld.yml
- hosts: web1
tasks:
- name: disabled selinux
selinux:
state: disabled
13、firewalld
用于设置firewalld防火墙
参数:
zone: 区域
service: 放行服务
port: 放行端口
source: 源地址
interface: 关联网卡
permanent: [ yes| no ] 永久生效
immediate: [ yes| no ] 临时生效
rich_rule: 富规则
state: 启用和禁用
enabled
disabled
masquerade: 是否激活ip伪装
enable
disable
示例:
[root@m1 test]# vim firewalld.yml
- hosts: web1
tasks:
- name: allow servie
firewalld:
service: https
permanent: yes
state: enabled
- name: allow port
firewalld:
port: 8081/tcp
permanent: yes
state: enabled
- name: add rich rule
firewalld:
rich_rule: rule family=ipv4 forward-port port=443 protocol=tcp to-port=8081
permanent: yes
state: enabled
- name: restart firewalld
systemd:
name: firewalld
state: restarted
14、mount
用于挂载设备
参数:
src: 挂载的目标设备
path: 要挂载到的目录
fstype: 文件系统类型。如iso9660、nfs等。
opts: 挂载选项。如defaults、ro、rw等。
state:
mounted 挂载设备,并将挂载信息写入/etc/fstab。
present 只将挂载信息写入/etc/fstab,不挂载。
absent 卸载设备,会清除/etc/fstab写入的配置。
unmounted 卸载,不会清除/etc/fstab写入的配置。
remounted 重新挂载
示例:
[root@m1 test]# vim mount.yml
- hosts: web1
tasks:
- name: mount test
mount:
src: 172.16.1.31:/data
path: /mnt/
fstype: nfs
opts: defaults
state: mounted
15、cron
用于设置远程主机的计划任务
参数:
name: 设置计划任务的名称,计划任务的名称会在注释中显示。在一台机器中,计划任务的名称应该具有唯一性,方便以后根据名称修改或删除计划任务。
job: 指定当前计划任务实际执行的任务
user: 指定当前计划任务属于哪个用户,当不使用此参数时,默认为管理员用户。
minute: 分钟
hour: 小时
day: 天
month: 月
weekday: 周
state: 当计划任务有名称时,可以根据名称修改或删除对应的任务。
absent 删除
present 设置计划任务,默认
disabled: [ yes|no ] 注释指定的计划任务,注意需要指定任务的名称、任务的job、任务的时间。
示例:
- hosts: web1
tasks:
- name: cron test
cron:
name: cron test
minute: '00'
hour: '3'
day: '1'
job: "echo 'hello'"
[root@web1 ~]# crontab -l
#Ansible: cron test
00 3 1 * * echo "hello"
16、unarchive
用于解压缩
参数:
src: 源路径,可以是ansible控制端上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no
dest: 远程主机上的目标路径,即解压缩到那个目录。
owner: 指定属主
group: 指定属组
mode: 设置解压缩后的文件目录权限
copy: [ yes|no ] 如果为yes(默认),则将文件从本地复制到目标主机,否则,将在目标主机上寻找src压缩文件。建议使用remote_src。
remote_src: [ yes|no ] 如果为yes表示压缩文件已在远程主机上(默认no),而不是在Ansible控制器本地。即将远程主机的压缩文件解压到指定目录。
示例:
[root@m1 test]# vim unarchive.yml
- hosts: web1
tasks:
- name: unarchive test
unarchive:
src: /root/latest.tar.gz
dest: /root/
owner: www
group: www
# 如果压缩包地址来源于网络,需要配合remote_src: yes参数使用
- name: unarchive
unarchive:
src: https://wordpress.org/latest.tar.gz
dest: /opt/
owner: www
group: www
remote_src: yes
[root@web1 ~]# ll
drwxr-xr-x 5 www www 4096 Sep 1 14:54 wordpress
[root@web1 ~]# ll /opt
drwxr-xr-x 5 www www 4096 Sep 1 14:54 wordpress
17、mysql_db
用于建立、删除、导入和导出数据库。注意被控端需要安装MySQL-python 。
参数:
login_host: 数据库运行的主机地址
login_user: 数据库的登录用户
login_password: 数据库密码
login_port: 数据库的端口
name: 数据库的名称
encoding: 使用的编码方式,例如'utf8'或'latinl swedish ci'
target: 目标要导出或导入的SQL文件在远程主机上的位置
state:
present 创建
absent 删除
dump 导出,需要配合target
import 导入,需要配合target
示例:
[root@m1 test]# vim mysql_db.yml
- hosts: web1
tasks:
- name: create a database
mysql_db:
login_host: "localhost"
login_user: "root"
login_password: "000000"
name: "test"
encoding: "utf8"
state: "present"
- name: dump a database
mysql_db:
login_host: "localhost"
login_user: "root"
login_password: "000000"
name: "test"
target: "/root/test.sql"
state: "dump"
[root@web1 ~]# mysql -uroot -p000000 -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
[root@web1 ~]# ll test.sql
-rw-r--r-- 1 root root 1261 Sep 17 22:30 test.sql
18、mysql_user
用于管理数据库用户。
参数:
login_host: 数据库运行的主机地址
login_user: 数据库的登录用户
login_password: 数据库密码
login_port: 数据库的端口
name: 用户名称
passwd: 用户密码
priv: 授权。格式:"db.table:priv1,priv2" 或者 "db.table:priv/db.table:priv"。
host: 用户的"host"部分。授权可登录的地址。
state:
absent 创建
present 删除
update_password: 更新密码,不需要特别设置。always(默认)更新密码;on_create只会为新创建的用户设置密码。
always
on_create
示例:
[root@m1 test]# vim mysql_user.yml
- hosts: web1
tasks:
- name: create database user
mysql_user:
login_host: "localhost"
login_user: "root"
login_password: "000000"
name: "cp"
password: "000000"
priv: "*.*:ALL"
host: "172.16.1.%"
state: present
[root@web1 ~]# mysql -uroot -p000000 -e "show grants for cp@'172.16.1.%'"
+---------------------------------------------------------------------------------------------------------------------+
| Grants for cp@172.16.1.% |
+---------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'cp'@'172.16.1.%' IDENTIFIED BY PASSWORD '*032197AE5731D4664921A6CCAC7CFCE6A0698693' |
+---------------------------------------------------------------------------------------------------------------------+
19、mysql_replication
用于配置数据库主从。
用于新增公钥到目标服务器用户家目录的 .ssh 目录的 authorized_keys 文件中。
没有 authorized_keys 文件会自动创建。
参数:
user: 远程主机上的用户
exclusive: 是否移除authorized_keys文件中其它非指定key,默认为False
key: SSH 公钥,为字符串或url
state: 默认为present
absent: 删除
present: 创建
示例:(如果.ssh 目录不存在会自动创建)
[root@cp-3 ansible]# cat authorized_keys.yaml
- hosts: 10.88.88.11
remote_user: root
gather_facts: false
tasks:
- name: push authorized_keys
authorized_key:
user: root
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
21、lineinfile
用于对文件的行进行操作,如果行存在则不做改变,
参数:
path: 目标文件
line: 要插入/替换到文件中的行
regexp:
正则表达式匹配对应的行。如果正则表达式不匹配,该行将被添加到文件末尾。
当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换。
当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。
state: 默认present
present: 替换或者添加
absent: 删除
backrefs:
当设置为yes,根据正则替换文本时,如果regexp参数中的正则存在分组,在line参数中可以直接对正则中的分组进行引用。
当设置为yes,正则没有匹配到任何的行时,则不会对文件进行任何操作。
insertafter:将文本插入到指定的行之后,值可以设置为正则表达式和 EOF。
值为正则表达式,则将在最后一个匹配之后插入该行。可配合 firstmatch 参数在第一次匹配时候插入。如果正则表达式没有匹配项,则将插入文件末尾。
值为 EOF(End Of File),表示插入到文档的末尾。
如果正则表达式同时传递给 regexp 和 insertbefore,则只有在没有找到匹配 regexp 的情况下才会使用 insertbefore。
当使用backrefs参数时,此参数会被忽略。
insertbefore:将文本插入到指定的行之前,值可以设置为正则表达式和 BOF。
值为正则表达式,则将在最后一个匹配之前插入该行。可配合 firstmatch 参数在第一次匹配时候插入。如果正则表达式没有匹配项,则将插入文件末尾。
值为BOF(Begin Of File),表示插入到文档的开头。
如果正则表达式同时传递给 regexp 和 insertbefore,则只有在没有找到匹配 regexp 的情况下才会使用 insertbefore。
当使用backrefs参数时,此参数会被忽略。
firstmatch:与 insertafter 或 insertbefore 一起使用。当设置为 yes,将对正则表达式第一次匹配的行进行操作。
backup: 是否在修改文件之前对文件进行备份。
create: 当要操作的文件并不存在时,是否创建对应的文件。
owner: 修改文件属主
group: 修改文件属组
mode: 修改文件权限
示例:(建议查看 ansible-doc lineinfile 里面示例)
# 测试文件
root@cp1:~# cat /tmp/test.txt
aa
bb
cc abcd
# playbook内容
[root@cp3 ansible]# cat lineinfile.yaml
- hosts: 10.88.88.11
remote_user: root
gather_facts: false
tasks:
- name: 修改cc开头的行为11
lineinfile:
path: /tmp/test.txt
regexp: "^cc"
line: "11"
- name: 添加dd行
lineinfile:
path: /tmp/test.txt
line: "dd"
- name: 开头添加00
lineinfile:
path: /tmp/test.txt
line: "00"
insertbefore: BOF
- name: 写入多行
lineinfile:
path: /tmp/test.txt
line: "{{ item }}"
with_items:
- "1"
- "2"
# 执行结果
root@cp1:~# cat /tmp/test.txt
00
aa
bb
11
dd
1
2
写入多行也可以像下面这样写,但是写入时不会都不会进行判断行是否存在,每次执行都会去重新执行一遍,所以重复执行可能会有问题。
- name: 写入多行
lineinfile:
path: /tmp/test.txt
line: |
test1
test2
insertbefore 和 regexp 结合使用,如下在一个启动脚本中指定 java 路径。
[root@test01 ~]# cat /tmp/launcher
exec "$(dirname "$0")/launcher.py" "$@"
[root@test01 ~]# cat lineinfile.yaml
- hosts: pt
remote_user: root
gather_facts: false
tasks:
- name: 添加 java 路径变量到启动脚本中
lineinfile:
path: "/tmp/launcher"
regexp: "^PATH"
insertbefore: "^exec"
line: "PATH=/home/jdk/bin/:$PATH"
[root@test01 ~]# cat /tmp/launcher
PATH=/home/jdk/bin/:$PATH
exec "$(dirname "$0")/launcher.py" "$@"
上面 playbook 表示,如果以 PATH 开头的行不存在就将 line 指定的内容添加到以 exec 开头行之前,如果以 PATH 开头的行存在,就用 line 指定的内容替换它。
22、blockinfile
用于在文件中插入、更新或者删除多行文本块。
但是使用这个模块往指定文件中插入一段文本,会自动为这段文本添加两个标记,一个开始标记,一个结束标记。所以给操作的文件设置合理的 marker 很重要。
参数:
path: 目标文件
block: 要插入的文本块
state: 默认 present
present: 替换或者添加
absent: 删除
marker: 默认值为 "# {mark} ANSIBLE MANAGED BLOCK"。
开始标记为:# BEGIN ANSIBLE MANAGED BLOCK,结束标记为:# END ANSIBLE MANAGED BLOCK
我们可以使用此参数自定义标记,比如 marker: "# {mark} test",这样开始标记变成了# BEGIN test,结束标记变成了# END test。
marker_begin: 设置 marker 参数的开始标记中的 {mark} 变量,默认值"BEGIN"。
marker_end: 设置 marker 参数的结束标记中的 {mark} 变量,默认值"END"。
insertafter: 同 lineinfile
insertbefore: 同 lineinfile
backup: 是否在修改文件之前对文件进行备份。
create: 当要操作的文件并不存在时,是否创建对应的文件。
owner: 修改文件属主
group: 修改文件属组
mode: 修改文件权限
示例:
[root@test01 ~]# cat blockinfile.yaml
- hosts: test
remote_user: root
gather_facts: false
tasks:
- name: 写入多行
blockinfile:
path: /tmp/test.txt
create: yes
block: |
test1
test2
# 执行结果
[root@test01 ~]# cat /tmp/test.txt
# BEGIN ANSIBLE MANAGED BLOCK
test1
test2
# END ANSIBLE MANAGED BLOCK
23、raw
用于执行 SSH 命令,而不是通过模块系统调用,所以它不需要远程主机是否有 python。
示例,预装python环境:
- hosts: test
sudo: yes
remote_user: root
gather_facts: false
pre_tasks:
- name: 安装python
raw: yum install -y python
tasks:
......