模块
• ansible-doc
– 模块的手册,相当于 shell 的 man,里面包含ansible管理主机的所有模块
– ansible-doc -l 列出所有模块
– ansible-doc modulename 查看帮助
ansible-doc +相应的模块的名字则会显示具体的帮助
[root@ansible ~]# ansible-doc -l | wc -l ----一共有1378个模块
1378
• ping 模块
– 测试网络连通性, ping模块没有参数
– 注:测试 ssh 的连通性
– ansible host-pattern -m ping
command模块
– 默认模块,进程执行命令
– 用法
– ansible host-pattern -m command -a '[args]'
– 查看所有机器负载
ansible all -m command -a 'uptime'
– 查看日期和时间
ansible all -m command -a 'date +%F_%T'
command模块注意事项:
– 该模块通过-a跟上要执行的命令可以直接执行,不过命令里如果有带有如下字符部分则执行不成功 "<", ">", "|", "&"
– 该模块不启动 shell 直接在 ssh 进程中执行,所有使用到 shell 特性的命令执行都会失败
– 下列命令执行会失败
ansible all -m command -a 'ps aux|grep ssh'
ansible all -m command -a 'set'
运程执行命令的模块
• shell | raw 模块
– shell 模块用法基本和command一样,区别是 shell模块是通过/bin/sh迚行执行命令,可以执行任意命令
– raw模块,用法和shell 模块一样 ,可以执行任意命令
– 区别是 raw 没有chdir、creates、removes参数
– 执行以下命令查看结果
ansible t1 -m command -a 'chdir=/tmp touch f1'
ansible t1 -m shell -a 'chdir=/tmp touch f2'
ansible t1 -m raw -a 'chdir=/tmp touch f3'
举例说明:
使用ansible在db1 db2主机上批量创建用户zhang3
[root@ansible ~]# ansible db -m shell -a "useradd zhang3"
192.168.6.22 | SUCCESS | rc=0 >>
192.168.6.21 | SUCCESS | rc=0 >>
修改zhang3的默认密码为123456
[root@ansible ~]# ansible db -m shell -a 'echo "123456" | passwd --stdin zhang3'
192.168.6.21 | SUCCESS | rc=0 >>
更改用户 zhang3 的密码 。
passwd:所有的身份验证令牌已经成功更新。
192.168.6.22 | SUCCESS | rc=0 >>
更改用户 zhang3 的密码 。
passwd:所有的身份验证令牌已经成功更新。
在zhang3第一次登录的时候要求更改密码
[root@ansible ~]# ansible db -m shell -a 'chage -d 0 zhang3'
192.168.6.21 | SUCCESS | rc=0 >>
192.168.6.22 | SUCCESS | rc=0 >>
script模块
– 复杂命令怎么办?
– 直接在本地写脚本,然后使用 script 模块批量执行
– ansible t1 -m script -a 'urscript'
– 友情提示: 该脚本包含但不限亍 shell 脚本,只要指
定 Sha-bang 解释器的脚本都可运行
举例说明:
对app组内的主机创建lisi,如果主机内存在zhang3用户则不创建
[root@ansible ~]# cat shell.sh
#!/bin/bash
useradd zhang3
if [ $? != 0 ];then
useradd li4
echo 123456 | passwd --stdin lisi
fi
[root@ansible ~]# ansible app -m script -a "/root/shell.sh" -----运行脚本程序
192.168.6.21 | FAILED! => {
"changed": true,
"msg": "non-zero return code",
"rc": 252,
"stderr": "Shared connection to 192.168.6.21 closed.\r\n",
"stdout": "useradd:用户“zhang3”已存在\r\npasswd:未知的用户名 lisi。\r\n",
"stdout_lines": [
"useradd:用户“zhang3”已存在",
"passwd:未知的用户名 lisi。"
]
}
web2 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to web2 closed.\r\n",
"stdout": "",
"stdout_lines": []
}
• copy 模块
– 复制文件到进程主机
– src:要复制到进程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目彔,
它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目彔里的内容,如果没有使用"/"来结尾,
则包含目彔在内的整个内容全部复制,类似亍rsync
– dest:必选项。进程主机的绝对路径,如果源文件是一个目彔,那么该路径也必须是个目彔
举例说明:
[root@ansible ~]# ansible web -m copy -a 'src=/etc/resolv.conf dest=/etc/resolv.conf'
web1 | SUCCESS => {
"changed": false,
"checksum": "73bbab37afa629be4606342ea4b5c1363a19e14d",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/etc/resolv.conf",
"size": 85,
"state": "file",
"uid": 0
}
web2 | SUCCESS => {
"changed": false,
"checksum": "73bbab37afa629be4606342ea4b5c1363a19e14d",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/etc/resolv.conf",
"size": 85,
"state": "file",
"uid": 0
}
模块
• lineinfile | replace 模块
– 类似 sed 的一种行编辑替换模块,path 目的文件,regexp 正则表达式,
-line 替换后的结果
ansible t1 -m lineinfile -a 'path="/etc/selinux/config"
regexp="^SELINUX=" line="SELINUX=disabled"'
– 替换指定字符
ansible t1 -m replace -a 'path="/etc/selinux/config"
regexp="^(SELINUX=).*" replace="\1disabled"'
lineinfile只替换到指定的行,replace是替换到匹配的行
举例说明
ansible t1 -m replace -a 'path="/etc/selinux/config"
regexp="^(SELINUX=).*" replace="\1disabled"'
[root@ansible ~]# ansible cache -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 \
> regexp="^ONBOOT" line="ONBOOT=\"no\""'
cache | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@ansible ~]# ansible cache -m shell -a 'grep ONBOOT /etc/sysconfig/network-scripts/ifcfg-eth0'
cache | SUCCESS | rc=0 >>
ONBOOT="no"
[root@ansible ~]# ansible cache -m replace -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="^(ONBOOT=).*" replace="\1\"yes\""'
cache | SUCCESS => {
"changed": true,
"msg": "1 replacements made"
}
[root@ansible ~]# ansible cache -m shell -a 'grep ONBOOT /etc/sysconfig/network-scripts/ifcfg-eth0'cache | SUCCESS | rc=0 >>
ONBOOT="yes"
yum模块
– 删除软件包
ansible t1 -m yum -a 'name="lrzsz" state=absent'
– 删除多个软件包
ansible t1 -m yum -a 'name="lrzsz,lftp" state=absent'
– 安装软件包
ansible t1 -m yum -a 'name="lrzsz"'
– 安装多个软件包
ansible t1 -m yum -a 'name="lrzsz,lftp"'
举例:
[root@cache ~]# rpm -qa | grep lftp
lftp-4.4.8-8.el7_3.2.x86_64
[root@ansible ~]# ansible cache -m yum -a 'name="lftp" state=removed' ---卸载软件
[root@cache ~]# rpm -qa | grep lftp
[root@cache ~]#
[root@ansible ~]# ansible cache -m yum -a 'name="lftp" state=installed' ----安装软件
service模块
– name:必选项,服务名称
– enabled:是否开机吭劢 yes|no
– sleep:如果执行了restarted,在则stop和start乊间
沉睡几秒钟
– state:对当前服务执行吭劢,停止、重吭、重新加载等操作(started,stopped,restarted,reloaded)
ansible t1 -m service -a 'name="sshd" enabled="yes" state="started"'
举例:
[root@ansible ~]# ansible cache -m service -a 'name="chronyd" state="stopped"'
[root@cache ~]# ss -untpl | grep chronyd
[root@ansible ~]# ansible cache -m service -a 'name="chronyd" state="started"'
[root@cache ~]# ss -untpl | grep chronyd
udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=4485,fd=1))
udp UNCONN 0 0 ::1:323 :::* users:(("chronyd",pid=4485,fd=2))
在web组的机器上安装apache,把端口改成8080,并设置开机自启
[root@ansible ~]# ansible web -m yum -a 'name="httpd"'
[root@ansible ~]# ansible web -m lineinfile -a 'path=/etc/httpd/conf/httpd.conf regexp="^Listen" line="Listen 8080"'
web2 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line replaced"
}
web1 | SUCCESS => {
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@ansible ~]# ansible web -m shell -a 'grep Listen /etc/httpd/conf/httpd.conf'
web2 | SUCCESS | rc=0 >>
# Listen: Allows you to bind Apache to specific IP addresses and/or
# Change this to Listen on specific IP addresses as shown below to
#Listen 12.34.56.78:80
Listen 8080
web1 | SUCCESS | rc=0 >>
# Listen: Allows you to bind Apache to specific IP addresses and/or
# Change this to Listen on specific IP addresses as shown below to
#Listen 12.34.56.78:80
Listen 8080
[root@ansible ~]# ansible web -m service -a 'name="httpd" enabled="yes" state="started"'
模块
• setup模块
– 主要用亍获取主机信息,在playbooks里经常会用到的
一个参数gather_facts就不该模块相关。setup模块下
经常使用的一个参数是filter参数
– filter 可以过滤到我们需要的信息
ansible t1 -m setup -a 'filter=ansible_distribution'
举例说明:
[root@ansible ~]# ansible web -m setup -a 'filter=ansible_distribution'
web2 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS"
},
"changed": false
}
web1 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS"
},
"changed": false
}