文章目录
1、command模块
这个模块可以直接在远程主机上执行命令,并将结果返回本主机,默认ansible使⽤的模块是command。
注意,该命令不支持| 管道命令
。
常见选项
argv
: # 允许用户以列表和字符串的形式提供命令,不能同时使用,也不必须提供其中一种
chdir
: # 在执行命令之前,先cd到指定的目录下
creates
: # 用于判断命令是否要执行,如果指定的文件存在(可以使用通配符)存在,则不执行
free_form
: # 默认的选项,这里只是显示,实际上是没有该选项的
removes
: # 用于判断命令是否要执行,如果指定的文件存在(可以使用通配符)不存在,则不执行
stdin
: # 将命令的stdin直接设置为指定值
warn
: # 设置command的警告信息(在/etc/ansible/ansible.cfg中有配置项)
例子
#不带选项
[root@ansible_center ~]# ansible test -m command -a "free -m"
192.168.189.134 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 1819 358 1045 12 415 1242
Swap: 2047 0 2047
#chdir选项
[root@ansible_center ~]# ansible test -m command -a "chdir=/etc/yum.repos.d ls"
192.168.189.134 | CHANGED | rc=0 >>
CentOS-Base.repo
CentOS-CR.repo
CentOS-Debuginfo.repo
CentOS-fasttrack.repo
CentOS-Media.repo
CentOS-Sources.repo
CentOS-Vault.repo
google-chrome.repo
neo4j.repo
#creates选项
[root@ansible_center ~]# ansible test -m command -a "ls creates=/root/haha.txt" 192.168.189.134 | SUCCESS | rc=0 >>
skipped, since /root/haha.txt exists
[root@ansible_center ~]# ansible test -m command -a "ls creates=/root/haha1.txt"
192.168.189.134 | CHANGED | rc=0 >>
anaconda-ks.cfg
Desktop
Documents
Downloads
haha.txt
initial-setup-ks.cfg
Music
Pictures
Public
Templates
usr
Videos
#removes选项
[root@ansible_center ~]# ansible test -m command -a "ls removes=/root/haha1.txt"
192.168.189.134 | SUCCESS | rc=0 >>
skipped, since /root/haha1.txt does not exist
[root@ansible_center ~]# ansible test -m command -a "ls removes=/root/haha.txt" 192.168.189.134 | CHANGED | rc=0 >>
anaconda-ks.cfg
Desktop
Documents
Downloads
haha.txt
initial-setup-ks.cfg
Music
Pictures
Public
Templates
usr
Videos
2、script模块
用途:script模块用于控制远程主机执行脚本,在执行脚本前,ansible会将本地脚本传输到远程主机,然后在执行,在执行脚本的时候,其采用的是远程主机上的shell环境
常见选项
chdir
: # 在远程执⾏脚本前先切换到此⽬录下
creates
: # 当此⽂件存在时,不执⾏脚本。
decrypt
: # 此选项使用vault控制源文件的自动解密(对使用ansible-vault encrypt 文件名.yml 进行加密的文件解密)
executable
: # 不再使⽤默认的/bin/sh解析并执⾏命令,⽽是使⽤此处指定的命令解析(例如使⽤expect解析expect脚本。必须为绝对路径)
free_form
: # 本地待执⾏的脚本路径、选项、参数。之所以称为free_form,是因为它是脚本名+选项+参数。
removes
: # 当此⽂件不存在时,不执⾏脚本。可⽤于实现幂等性。
例子
[root@ansible_center ~]# vim test.sh
[root@ansible_center ~]# cat test.sh
#!/bin/bash
echo -e "hello\nansible test"
[root@ansible_center ~]# ansible test -m script -a "test.sh"
192.168.189.134 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.189.134 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.189.134 closed."
],
"stdout": "hello\r\nansible test\r\n",
"stdout_lines": [
"hello",
"ansible test"
]
}
3、shell模块
shell模块可以在远程主机上调用shell解释器运行命令,shell和command的⽤法基本⼀样,支持shell的各种功能,例如管道等。
常见选项
chdir
: # 在执行命令之前,先cd到指定的目录下
creates
: # 用于判断命令是否要执行,如果指定的文件存在(可以使用通配符)存在,则不执行
executable
: # 不再使⽤默认的/bin/sh解析并执⾏命令,⽽是使⽤此处指定的命令解析(例如使⽤expect解析expect脚本。必须为绝对路径)
free_form
: # 默认的选项,这里只是显示,实际上是没有该选项的
removes
: # 用于判断命令是否要执行,如果指定的文件存在(可以使用通配符)不存在,则不执行
stdin
: # 将命令的stdin直接设置为指定值
warn
: # 设置command的警告信息(在/etc/ansible/ansible.cfg中有配置项)
例子
[root@ansible_center ~]# ansible test -m shell -a "cat /etc/passwd | grep root"
192.168.189.134 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
4、yum/apt模块
顾名思义,该模块主要用于软件的安装。两个模块的使用大同小异,此处讲解yum模块
其选项如下:
name
#所安装的包的名称
state
#present
—>安装,latest
—>安装最新的,absent
—> 卸载软件。
update_cache
#强制更新yum的缓存
conf_file
#指定远程yum安装时所依赖的配置文件(安装本地已有的包)。
disable_pgp_check
#是否禁止GPG checking,只用于present
orlatest
。
disablerepo
#临时禁止使用yum库。 只用于安装或更新时。
enablerepo
#临时使用的yum库。只用于安装或更新时。
例子
[root@ansible_center ~]# ansible test -m yum -a 'name=httpd state=present'
192.168.189.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"httpd"
]
},
"msg": "",
"rc": 0,
"results": .....
}
5、service模块
该模块用于服务程序的管理。
常用选项:
arguments
#命令行提供额外的参数
enabled
#设置开机启动。
name=
#服务名称
runlevel
#开机启动的级别,一般不用指定。
sleep
#在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)
state
#有四种状态,分别为:started
—>启动服务,stopped
—>停止服务,restarted
—>重启服务,reloaded
—>重载配置
例子
6、setup系统模块
该模块主要用于收集信息,是通过调用facts组件来实现的。
facts组件是Ansible用于采集被管机器设备信息的一个功能,我们可以使用setup模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
facts就是变量,内建变量 。每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。
常用参数
filter参数:用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。filter只能匹配ansible_facts下第一层的内容
例子
#过滤得到网卡信息
192.168.189.134 | SUCCESS => {
"ansible_facts": {
"ansible_ens33": {
"active": true,
"device": "ens33",
"features": {
"busy_poll": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "off [fixed]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "off [fixed]",
"netns_local": "off [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off",
"rx_checksumming": "off",
"rx_fcs": "off",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "on [fixed]",
"rx_vlan_offload": "on",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "on",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "off [fixed]",
"tx_checksumming": "on",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipip_segmentation": "off [fixed]",
"tx_lockless": "off [fixed]",
"tx_nocache_copy": "off",
"tx_scatter_gather": "on",
"tx_scatter_gather_fraglist": "off [fixed]",
"tx_sctp_segmentation": "off [fixed]",
"tx_sit_segmentation": "off [fixed]",
"tx_tcp6_segmentation": "off [fixed]",
"tx_tcp_ecn_segmentation": "off [fixed]",
"tx_tcp_mangleid_segmentation": "off",
"tx_tcp_segmentation": "on",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "on [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off [fixed]",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "192.168.189.134",
"broadcast": "192.168.189.255",
"netmask": "255.255.255.0",
"network": "192.168.189.0"
},
"ipv6": [
{
"address": "fe80::1438:28db:327c:3a4b",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "00:0c:29:eb:8f:9f",
"module": "e1000",
"mtu": 1500,
"pciid": "0000:02:01.0",
"promisc": false,
"speed": 1000,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
7、file模块
该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
常用选项
force
#需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group
#定义文件/目录的属组。后面可以加上mode
:定义文件/目录的权限
owner
#定义文件/目录的属主。后面必须跟上path
:定义文件/目录的路径
recurse
#递归设置文件的属性,只对目录有效,后面跟上src
:被链接的源文件路径,只应用于state=link
的情况
dest
#被链接到的路径,只应用于state=link
的情况
state
#状态,有以下选项:
state=directory
:如果目录不存在,就创建目录
state=file
:即使文件不存在,也不会被创建
state=link
:创建软链接
state=hard
:创建硬链接
state=touch
:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
state=absent
:删除目录、文件或者取消链接文件
例子
#state=directorry参数(创建目录)
[root@ansible_center ~]# ansible test -m file -a "path=/root/file state=directory owner=root group=root mode=0777"
192.168.189.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"path": "/root/file",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
[root@ansible_center ~]# ansible test -m shell -a "ls -ld /root/file "
192.168.189.134 | CHANGED | rc=0 >>
drwxrwxrwx. 2 root root 6 Mar 20 16:54 /root/file
8、copy模块
这个模块用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等。
其相关选项如下:
backup:
# 拷贝的同时也创建⼀个包含时间戳信息的备份⽂件,默认为no,可以指定为backup=yes做文件备份
content:
# 当用content代替src参数的时候,可以把content指定的内容直接写到一个文件
dest:
# ⽬标路径,只能是绝对路径,如果拷贝的⽂件是⽬录,则⽬标路径必须也是⽬录
directory_mode:
# 当对⽬录做递归拷贝时,设置了directory_mode将会使得只拷贝新建⽂件旧⽂件不会被拷贝。默认未设置.
force:
# 设置为yes(默认)时,将覆盖远程同名⽂件。设置为no时,忽略同名⽂件的拷贝。
group:
# 指定文件拷贝到远程主机后的属组,但是远程主机上必须有对应的组,否则会报错
mode:
# 设置远程⽂件的权限。使⽤数值表⽰时不能省略第⼀位,如0644。也可以使⽤’u+rwx’或’u=rw,g=r,o=r’等⽅式设置
owner:
# 设置远程⽂件的所有者
src:
# 拷贝本地源⽂件到远程,可使⽤绝对路径或相对路径。如果路径是⽬录,且⽬录后加了斜杠"/",则只会拷贝⽬录中的内容到远程,如果⽬录后不加斜杠,则拷贝⽬录本⾝和⽬录内的内容到远程
validate:
# 复制前是否检验需要复制目的地的路径
例子
[root@ansible_center ~]# ansible test -m copy -a "src=/root/test.sh dest=/tmp/"
192.168.189.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "3b21337552a6ac5291070245536fb497c6192b36",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"md5sum": "29299b7a045fec4e0f8ae7086b021972",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 43,
"src": "/root/.ansible/tmp/ansible-tmp-1584694828.66-65640912781961/source",
"state": "file",
"uid": 0
}
[root@ansible_center ~]# ^C
[root@ansible_center ~]# ansible test -m command -a "ls -lh /tmp/test.sh" 192.168.189.134 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 43 Mar 20 16:59 /tmp/test.sh
9、template模块
基于模板方式生成一个文件复制到远程主机,其⽤法和copy模块⽤法基本⼀致(template使用Jinjia2格式作为文件模版,进行文档内变量的替换的模块。它的每次使用都会被ansible标记为”changed”状态。)
常用选项
backup:
# 拷贝的同时也创建⼀个包含时间戳信息的备份⽂件,默认为no
dest:
# 远程节点上的绝对路径,用于放置template文件
force:
# 设置为yes (默认)时,将覆盖远程同名⽂件。设置为no时,忽略同名⽂件的拷贝
group:
# 设置远程⽂件的所属组
mode:
# 设置远程⽂件的权限。使⽤数值表⽰时不能省略第⼀位,如0644。也可以使⽤’u+rwx’ or 'u=rw,g=r,o=r’等⽅式设置
output_encoding:
# 改变dest指定模板文件的编码,默认为utf8.
owner:
# 设置远程⽂件的所有者
src:
# ansible控制器上Jinja2格式的模板所在位置,可以是相对或绝对路径.
validate:
# 在复制到⽬标主机后但放到⽬标位置之前,执⾏此选项指定的命令,⼀般⽤于检查配置⽂件语法,语法正确则保存到⽬标位置,如果要引⽤⽬标⽂件名,则使⽤%s
例子
#编辑模板文件
[root@ansible_center ~]# vim test
[root@ansible_center ~]# cat test
this is a test
the first argv is {{ var1 }}
the second argv is {{ var2 }}
#这里采用命令行传参
[root@ansible_center ~]# ansible test -m template -a "src=/root/test dest=/root/test" -e "var1=hello var2=test"
192.168.189.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "4c0ccc7967e8e6121ae1d90674c5ef202b460a6e",
"dest": "/root/test",
"gid": 0,
"group": "root",
"md5sum": "c28a622c6fd44d91405b38a42e358917",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 63,
"src": "/root/.ansible/tmp/ansible-tmp-1584695325.82-228411951287966/source",
"state": "file",
"uid": 0
}
[root@ansible_center ~]# ansible test -m command -a "cat /root/test"
192.168.189.134 | CHANGED | rc=0 >>
this is a test
the first argv is hello
the second argv is test
10、user 模块
该模块主要是用来管理用户账号。
常用选项:
comment
# 用户的描述信息
createhome
# 是否创建家目录
force
# 在使用state=absent时, 行为与userdel –force一致.
group
# 指定基本组
groups
# 指定附加组,如果指定为(groups=)表示删除所有组
home
# 指定用户家目录
move_home
# 如果设置为home=时, 试图将用户主目录移动到指定的目录
name
# 指定用户名
non_unique
# 该选项允许改变非唯一的用户ID值
password
# 指定用户密码
remove
# 在使用state=absent时, 行为是与userdel –remove一致
shell
# 指定默认shell
state
# 设置帐号状态,不指定为创建,指定值为absent表示删除
system
# 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
uid
# 指定用户的uid
例子
#添加一个用户并指定其 uid
[root@ansible_center ~]# ansible test -m user -a "name=xiaohei uid=2222"
192.168.189.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 2222,
"home": "/home/xiaohei",
"name": "xiaohei",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2222
}
[root@ansible_center ~]# ansible test -m shell -a "cat /etc/passwd | grep 'xiaohei'"
192.168.189.134 | CHANGED | rc=0 >>
xiaohei:x:2222:2222::/home/xiaohei:/bin/bash
#删除用户,并移除家目录
[root@ansible_center ~]# ansible test -m user -a 'name=xiaohei state=absent remove=yes'
192.168.189.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "xiaohei",
"remove": true,
"state": "absent"
}
#此时用户已经删除了,查询失败
[root@ansible_center ~]# ansible test -m shell -a "cat /etc/passwd | grep 'xiaohei'"
192.168.189.134 | FAILED | rc=1 >>
non-zero return code
11、group 模块
该模块主要用于添加或删除组。
常用选项
gid=
#设置组的GID号
name=
#指定组的名称
state=
#指定组的状态,默认为创建,设置值为absent
为删除
system=
#设置值为yes
,表示创建为系统组
例子
#创建组
[root@ansible_center ~]# ansible test -m group -a 'name=test gid=12222'
192.168.189.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 12222,
"name": "test",
"state": "present",
"system": false
}
[root@ansible_center ~]# ansible test -m shell -a "cat /etc/group | grep 'test'"
192.168.189.134 | CHANGED | rc=0 >>
test:x:12222:
#删除组
[root@ansible_center ~]# ansible test -m group -a 'name=test state=absent'
192.168.189.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "test",
"state": "absent"
}
#此时查看失败,没有结果
[root@ansible_center ~]# ansible test -m shell -a "cat /etc/group | grep 'test'"
192.168.189.134 | FAILED | rc=1 >>
non-zero return code
nter ~]# ansible test -m shell -a "cat /etc/group | grep 'test'"
192.168.189.134 | CHANGED | rc=0 >>
test:x:12222:
#删除组
[root@ansible_center ~]# ansible test -m group -a 'name=test state=absent' 192.168.189.134 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "test",
"state": "absent"
}
#此时查看失败,没有结果
[root@ansible_center ~]# ansible test -m shell -a "cat /etc/group | grep 'test'"
192.168.189.134 | FAILED | rc=1 >>
non-zero return code