Ansible常用模块
1. Ansible常用模块使用详解
ansible常用模块有:
ping
yum
template
copy
user
group
service
raw
command
shell
script
ansible常用模块raw、command、shell的区别:
shell模块调用的/bin/sh指令执行
command模块不是调用的shell的指令,所以没有bash的环境变量
raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了
2. Ansible常用模块之ping
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@syb ansible]# ansible all -m ping
192.168.200.153 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3. Ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。
//查看受控主机的/tmp目录内容
[root@syb ansible]# ansible 192.168.200.153 -a 'ls /tmp'
192.168.200.153 | CHANGED | rc=0 >>
ansible_command_payload_9owbicy6
ks-script-6graoaxo
vmware-root_1017-4248221863
vmware-root_948-2688554130
vmware-root_969-4281777807
vmware-root_970-2956993747
vmware-root_971-4290232077
vmware-root_974-2965579094
vmware-root_975-4281646728
vmware-root_976-2966103375
vmware-root_982-2965972296
vmware-root_990-2999657286
//在受控主机的/tmp目录下新建一个文件test
[root@syb ansible]# ansible 192.168.200.153 -a 'touch /tmp/test'
[WARNING]: Consider using the file module with state=touch rather than running
'touch'. If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
192.168.200.153 | CHANGED | rc=0 >>
//查看受管主机是否创建
[root@syb ~]# ls /tmp/
ks-script-6graoaxo vmware-root_971-4290232077
test vmware-root_974-2965579094
vmware-root_1017-4248221863 vmware-root_975-4281646728
vmware-root_948-2688554130 vmware-root_976-2966103375
vmware-root_969-4281777807 vmware-root_982-2965972296
vmware-root_970-2956993747 vmware-root_990-2999657286
//command模块不支持管道符,不支持重定向
[root@syb ansible]# ansible 192.168.200.153 -a "echo 'hello world' > /tmp/test"
192.168.200.153 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@syb ansible]# ansible 192.168.200.153 -a 'cat /tmp/test'
192.168.200.153 | CHANGED | rc=0 >>
[root@syb ansible]# ansible 192.168.200.153 -a 'ps -ef | grep vsftpd'
192.168.200.153 | FAILED | rc=1 >>
error: garbage option
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).non-zero return code
4. Ansible常用模块之raw
raw模块用于在远程主机上执行命令,其支持管道符与重定向
//支持重定向
[root@syb ansible]# ansible 192.168.200.153 -m raw -a 'echo "hello world"> /tmp/test'
192.168.200.153 | CHANGED | rc=0 >>
Shared connection to 192.168.200.153 closed.
//在受管主机上查看
[root@syb ~]# cat /tmp/test
hello world
//支持管道符
[root@syb ansible]# ansible 192.168.200.153 -m raw -a 'cat /tmp/test | grep -Eo hello'
192.168.200.153 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.200.153 closed.
5. Ansible常用模块之shell
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
//写一个脚本
[root@syb ~]# ls
anaconda-ks.cfg
[root@syb ~]# vim test.sh
[root@syb ~]# cat test.sh
#!bin/bash
echo "syb"
[root@syb ~]# chmod +x test.sh //给脚本执行权限
[root@syb ~]# ll
总用量 8
-rw-------. 1 root root 1352 7月 7 08:47 anaconda-ks.cfg
-rwxr-xr-x 1 root root 22 7月 18 14:15 test.sh
//使用shell模块在受控机上执行受控机上的脚本
[root@syb ansible]# ansible 192.168.200.153 -m shell -a '/bin/bash /root/test.sh &> /tmp/test.log'
192.168.200.153 | CHANGED | rc=0 >>
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'cat /tmp/test.log'
192.168.200.153 | CHANGED | rc=0 >>
syb
6. Ansible常用模块之script
script模块用于在受控机上执行主控机上的脚本
[root@syb ansible]# mkdir /scripts
[root@syb ansible]# cd /scripts/
[root@syb scripts]# vim test.sh
[root@syb scripts]# cat test.sh
#!/bin/bash
useradd natasha
echo "123" | passwd --stdin natasha
echo "hello world" >~natasha/abc
[root@syb scripts]# chmod +x test.sh
[root@syb ansible]# ansible 192.168.200.153 -m script -a '/scripts/test.sh'
192.168.200.153 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.200.153 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.200.153 closed."
],
"stdout": "更改用户 natasha 的密码 。\r\npasswd:所有的身份验证令牌已经成功更新。\r\n",
"stdout_lines": [
"更改用户 natasha 的密码 。",
"passwd:所有的身份验证令牌已经成功更新。"
]
}
//查看受控机natasha用户家目录下/abc文件
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'cat /home/natasha/abc'
192.168.200.153 | CHANGED | rc=0 >>
hello world
7. Ansible常用模块之template
template模块用于生成一个模板,并可将其传输至远程主机上
//将家目录下anaconda-ks.cfg传送到node1主机上去
[root@syb ~]# ansible 192.168.200.153 -m template -a 'src=~/anaconda-ks.cfg dest=/tmp/syb'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "63941aae5dedfa541037591f2f7be672831aa767",
"dest": "/tmp/syb",
"gid": 0,
"group": "root",
"md5sum": "d7b9239a0fc325ac76f7435166c9dbd6",
"mode": "0644",
"owner": "root",
"size": 1352,
"src": "/root/.ansible/tmp/ansible-tmp-1626594074.460147-198639-167042762481302/source",
"state": "file",
"uid": 0
}
//在受管主机上查看
[root@syb ~]# ls /tmp/
syb vmware-root_971-4290232077
test vmware-root_974-2965579094
test.log vmware-root_975-4281646728
vmware-root_1017-4248221863 vmware-root_976-2966103375
vmware-root_948-2688554130 vmware-root_982-2965972296
vmware-root_969-4281777807 vmware-root_990-2999657286
8. Ansible常用模块之yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
name:要管理的包名
state:要进行的操作
state常用的值:
latest:安装软件
installed:安装软件
present:安装软件
removed:卸载软件
absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常。
//在受控机上查询看vsftpd软件是否安装
[root@syb ~]# rpm -qa | grep vsftpd
[root@syb ~]#
//在ansible主机上使用yum模块在受控机上安装vsftpd
[root@syb ansible]# ansible 192.168.200.153 -m yum -a 'name=vsftpd state=present'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: vsftpd-3.0.3-31.el8.x86_64"
]
}
//查看受控机上是否安装了vsftpd
[root@syb ~]# rpm -qa | grep vsftpd
vsftpd-3.0.3-31.el8.x86_64
9. Ansible常用模块之copy
copy模块用于复制文件至远程受控机。
[root@syb ansible]# ansible 192.168.200.153 -m copy -a 'src=/etc/ansible/inventory dest=/tmp/inventory'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "164ff065d5320bf6566555608abece6094074bf2",
"dest": "/tmp/inventory",
"gid": 0,
"group": "root",
"md5sum": "a54eacb3d4d6ba606ee25fcb9daf81ef",
"mode": "0644",
"owner": "root",
"size": 65,
"src": "/root/.ansible/tmp/ansible-tmp-1626594624.2457044-215671-69455196739862/source",
"state": "file",
"uid": 0
}
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'ls /tmp/inventory'
192.168.200.153 | CHANGED | rc=0 >>
/tmp/inventory
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'cat /tmp/inventory'
192.168.200.153 | CHANGED | rc=0 >>
[webservers]
192.168.200.153 ansible_user=root ansible_passwd=1
10.Ansible常用模块之group
group模块用于在受控机上添加或删除组。
//在受控机上添加一个系统组,其gid为520,组名为mysql
[root@syb ansible]# ansible 192.168.200.153 -m group -a 'name=mysql gid=520 state=present'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 520,
"name": "mysql",
"state": "present",
"system": false
}
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'grep mysql /etc/group'
192.168.200.153 | CHANGED | rc=0 >>
mysql:x:520:
//删除受控机上的mysql组
[root@syb ansible]# ansible 192.168.200.153 -m group -a 'name=mysql gid=520 state=absent'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'grep mysql /etc/group'192.168.200.153 | FAILED | rc=1 >>
non-zero return code
11. ansible常用模块之user
user模块用于管理受控机的用户帐号。
//在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@syb ansible]# ansible 192.168.200.153 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 306,
"home": "/home/mysql",
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 306
}
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'grep mysql /etc/passwd'
192.168.200.153 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'ls /home'
192.168.200.153 | CHANGED | rc=0 >>
admin
bys
natasha
syb
test
//修改mysql用户的uid为366
[root@syb ansible]# ansible 192.168.200.153 -m user -a 'name=mysql uid=366'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"append": false,
"changed": true,
"comment": "",
"group": 306,
"home": "/home/mysql",
"move_home": false,
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"uid": 366
}
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'grep mysql /etc/passwd'
192.168.200.153 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
//删除受管主机上的mysql用户
[root@syb ansible]# ansible 192.168.200.153 -m user -a 'name=mysql state=absent'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "mysql",
"remove": false,
"state": "absent"
}
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'grep mysql /etc/passwd'
192.168.200.153 | FAILED | rc=1 >>
non-zero return code
12. Ansible常用模块之service
service模块用于管理受控机上的服务。
//查看受控机上的vsftpd服务是否启动
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'systemctl is-active vsftpd'
192.168.200.153 | FAILED | rc=3 >>
inactivenon-zero return code
//启动受控机上的vsftpd服务
[root@syb ansible]# ansible 192.168.200.153 -m service -a 'name=vsftpd state=started'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "vsftpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
......此处省略n行
}
//查看受控机上的vsftpd服务是否启动
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'systemctl is-active vsftpd'
192.168.200.153 | CHANGED | rc=0 >>
active
//查看受控机上的vsftpd服务是否开机自动启动
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'systemctl is-enabled vsftpd'
192.168.200.153 | FAILED | rc=1 >>
disablednon-zero return code
//设置受控机上的vsftpd服务开机自动启动
[root@syb ansible]# ansible 192.168.200.153 -m service -a 'name=vsftpd enabled=yes'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "vsftpd",
"status": {
"ActiveEnterTimestamp": "Sun 2021-07-18 16:10:51 CST",
"ActiveEnterTimestampMonotonic": "8220815853",
......此处省略n行
}
//查看受控机上的vsftpd服务是否开机自动启动
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'systemctl is-enabled vsftpd'
192.168.200.153 | CHANGED | rc=0 >>
enabled
//停止受控机上的vsftpd服务
[root@syb ansible]# ansible 192.168.200.153 -m service -a 'name=vsftpd state=stopped'
192.168.200.153 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "vsftpd",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "Sun 2021-07-18 16:10:51 CST",
"ActiveEnterTimestampMonotonic": "8220815853",
......此处省略n行
}
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'systemctl is-active vsftpd'
192.168.200.153 | FAILED | rc=3 >>
inactivenon-zero return code
[root@syb ansible]# ansible 192.168.200.153 -m shell -a 'ss -antl'
192.168.200.153 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*