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模块了
ansible常用模块之ping
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@zyy ~]# ansible all -m ping
web01.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能
[root@zyy httpd]# ansible all -a 'date'
web01.example.com | CHANGED | rc=0 >>
Wed May 25 19:58:12 CST 2022
在受管机创建文件
[root@zyy httpd]# ansible all -a 'touch /tmp/hehe'
[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.
web01.example.com | CHANGED | rc=0 >>
[root@zyy httpd]# ansible all -a 'ls /tmp'
web01.example.com | CHANGED | rc=0 >>
abc
ansible_command_payload_73yiz860
hehe
vmware-root_892-2722239036
vmware-root_899-3988097379
vmware-root_900-2722108090
vmware-root_909-4021653354
//command模块不支持管道符,不支持重定向
[root@zyy httpd]# ansible all -a "echo 'hello world' > /tmp/hehe"
web01.example.com | CHANGED | rc=0 >>
hello world > /tmp/hehe
[root@zyy httpd]# ansible all -a 'cat /tmp/hehe'
web01.example.com | CHANGED | rc=0 >>
[root@zyy httpd]# ansible all -a 'ps aux|grep abc'
web01.example.com | FAILED | rc=1 >>
error: unsupported option (BSD syntax)
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
ansible常用模块之raw
raw模块用于在远程主机上执行命令,其支持管道符与重定向
[root@zyy httpd]# ansible all -m raw -a 'echo "hello world" > /tmp/hehe'
web01.example.com | CHANGED | rc=0 >>
Shared connection to web01.example.com closed.
[root@zyy httpd]# ansible all -a 'cat /tmp/hehe'
web01.example.com | CHANGED | rc=0 >>
hello world
[root@zyy httpd]# ansible all -m raw -a 'cat /tmp/hehe|grep -Eo hello'
web01.example.com | CHANGED | rc=0 >>
hello
Shared connection to web01.example.com closed.
ansible常用模块之shell
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
[root@zyy httpd]# ansible all -m shell -a 'ps -ef|grep sleep'
web01.example.com | CHANGED | rc=0 >>
root 4145 4056 0 20:11 pts/1 00:00:00 /bin/sh -c /usr/libexec/platform-python /root/.ansible/tmp/ansible-tmp-1653480664.7405403-2218-89536767504353/AnsiballZ_command.py && sleep 0
root 4165 4164 0 20:11 pts/1 00:00:00 /bin/sh -c ps -ef|grep sleep
root 4167 4165 0 20:11 pts/1 00:00:00 grep sleep
[root@zyy httpd]# ansible all -m shell -a 'echo "hehe">>/tmp/hehe'
web01.example.com | CHANGED | rc=0 >>
[root@zyy httpd]# ansible all -m shell -a 'cat /tmp/hehe'
web01.example.com | CHANGED | rc=0 >>
hello world
hehe
[root@xinzhizhu ~]# vi /scripts/test.sh
#!/bin/bash
sleep 7000 &
[root@zyy httpd]# ansible all -m shell -a '/bin/bash /scripts/test.sh'
web01.example.com | CHANGED | rc=0 >>
[root@zyy httpd]# ansible all -m shell -a 'ps -ef|grep sleep'
web01.example.com | CHANGED | rc=0 >>
root 4770 4445 0 20:16 pts/1 00:00:00 /bin/sh -c /usr/libexec/platform-python /root/.ansible/tmp/ansible-tmp-1653481015.3230307-2357-83542280979181/AnsiballZ_command.py && sleep 0
root 4790 4789 0 20:16 pts/1 00:00:00 /bin/sh -c ps -ef|grep sleep
root 4792 4790 0 20:16 pts/1 00:00:00 grep sleep
[root@xinzhizhu ~]# ps -ef|grep sleep
root 4809 1563 0 20:18 pts/0 00:00:00 grep --color=auto sleep
//后台运行脚本
[root@xinzhizhu ~]# vi /scripts/test.sh
#!/bin/bash
nohup sleep 7000 &
~
[root@zyy httpd]# ansible all -m shell -a 'ps -ef|grep sleep'
web01.example.com | CHANGED | rc=0 >>
root 4926 1 0 20:19 ? 00:00:00 sleep 7000
root 5029 4816 0 20:19 pts/1 00:00:00 /bin/sh -c /usr/libexec/platform-python /root/.ansible/tmp/ansible-tmp-1653481168.3716774-2416-81332417135819/AnsiballZ_command.py && sleep 0
root 5049 5048 0 20:19 pts/1 00:00:00 /bin/sh -c ps -ef|grep sleep
root 5051 5049 0 20:19 pts/1 00:00:00 grep sleep
//放前面
ansible常用模块之script
script模块用于在受控机上执行主控机上的脚本
[root@ansible ~]# ll /etc/ansible/scripts/
总用量 4
-rw-r--r--. 1 root root 61 9月 8 18:59 a.sh
[root@ansible ~]# ansible 172.16.103.129 -m script -a '/etc/ansible/scripts/a.sh &>/tmp/a'
172.16.103.129 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.16.103.129 closed.\r\n",
"stderr_lines": [
"Shared connection to 172.16.103.129 closed."
],
"stdout": "",
"stdout_lines": []
}
//查看受控机上的/tmp/a文件内容
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'cat /tmp/a'
172.16.103.129 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
....此处省略N行
jerry:x:1000:1000::/home/jerry:/bin/bash
//由此可见确是在受控机上执行了主控机上的脚本,且输出记录到了受控机上。因为此处 \
//的jerry用户是在受控机上才有的用户
ansible常用模块之template
template模块用于生成一个模板,并可将其传输至远程主机上。
[root@zyy httpd]# ansible all -m shell -a 'rm -rf /etc/yum.repos.d/*'
//删除受管机的源
web01.example.com | CHANGED | rc=0 >>
[root@xinzhizhu ~]# ls /etc/yum.repos.d/
[root@xinzhizhu ~]#
[root@zyy files]# cp /etc/yum.repos.d/CentOS-Base.repo .
[root@zyy files]# ls
CentOS-Base.repo
[root@zyy files]# ls
CentOS-Base.repo
[root@zyy files]# cd ..
[root@zyy httpd]# ansible all -m template -a 'src=files/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo owner=root group=root mode=0644'
web01.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
"dest": "/etc/yum.repos.d/CentOS-Base.repo",
"gid": 0,
"group": "root",
"md5sum": "ed031c350da2532e6a8d09a4d9b05278",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 1653,
"src": "/root/.ansible/tmp/ansible-tmp-1653482369.9070268-2489-215516778686436/source",
"state": "file",
"uid": 0
//给受管机安装yum源
[root@xinzhizhu ~]# ls /etc/yum.repos.d/
CentOS-Base.repo
ansible常用模块之yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
- name:要管理的包名
- state:要进行的操作
state常用的值:
- latest:安装软件
- installed:安装软件
- present:安装软件
- removed:卸载软件
- absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常。
[root@zyy httpd]# ansible all -m yum -a 'name=vsftpd state=present'
web01.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: vsftpd-3.0.3-34.el8.x86_64"
]
}
[root@xinzhizhu ~]# dnf list all|grep vsftpd
vsftpd.x86_64 3.0.3-34.el8 @AppStream
//安装
[root@zyy httpd]# ansible all -m yum -a 'name=vsftpd state=absent'
//卸载
ansible常用模块之copy
copy模块用于复制文件至远程受控机。
[root@zyy httpd]# ansible all -m copy -a 'content="xixi" dest=/tmp/hehe'
[root@xinzhizhu ~]# cat /tmp/hehe
xixi[root@xinzhizhu ~]#
[root@zyy httpd]# ansible all -m copy -a 'content="xixi\n" dest=/tmp/hehe'
xixi[root@xinzhizhu ~]# cat /tmp/hehe
xixi
[root@zyy httpd]# ansible all -m copy -a 'src=inventory dest=/tmp/inventory owner=root group=root mode=0644'
[root@xinzhizhu ~]# ll /tmp/
total 12
-rw-r--r--. 1 root root 1 May 25 19:49 abc
drwx------. 2 root root 41 May 25 20:15 ansible_command_payload_6bxg_u2_
-rw-r--r--. 1 root root 5 May 25 20:52 hehe
-rw-r--r--. 1 root root 76 May 25 20:56 inventory
ansible常用模块之group
group模块用于在受控机上添加或删除组。
[root@zyy httpd]# ansible all -m group -a 'name=xixi gid=333 state=present'
web01.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 333,
"name": "xixi",
"state": "present",
"system": false
}
[root@xinzhizhu ~]# grep xixi /etc/group
xixi:x:333:
//创建
[root@zyy httpd]# ansible all -m group -a 'name=xixi gid=333 state=absent'
web01.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "xixi",
"state": "absent"
[root@xinzhizhu ~]# grep xixi /etc/group
[root@xinzhizhu ~]#
//删除
ansible常用模块之user
user模块用于管理受控机的用户帐号。
[root@zyy httpd]# ansible all -m user -a 'name=xixi uid=333 system=yes create_home=no shell=/sbin/nologin state=present'
//创建用户
[root@zyy httpd]# ansible all -m user -a 'name=xixi uid=444'
//修改uid
[root@zyy httpd]# ansible all -m user -a 'name=xixi state=absent'
//删除
[root@xinzhizhu ~]# id xixi
uid=333(xixi) gid=333(xixi) groups=333(xixi)
[root@xinzhizhu ~]# id xixi
uid=444(xixi) gid=333(xixi) groups=333(xixi)
[root@xinzhizhu ~]# id xixi
id: ‘xixi’: no such user
ansible常用模块之service
service模块用于管理受控机上的服务
[root@zyy httpd]# ansible all -m shell -a 'systemctl is-active vsftpd'
web01.example.com | FAILED | rc=3 >>
inactivenon-zero return code
//查看受控机上的vsftpd服务是否启动
[root@zyy httpd]# ansible all -m service -a 'name=vsftpd state=started'
//启动受控机上的vsftpd服务
[root@zyy httpd]# ansible all -m shell -a 'systemctl is-enabled vsftpd'
//查看受控机上的vsftpd服务是否开机自动启动
[root@zyy httpd]# ansible all -m service -a 'name=vsftpd state=stoppped'
//停止服务
[root@zyy httpd]# ansible all -m shell -a 'systemctl is-active vsftpd'
web01.example.com | CHANGED | rc=0 >>
active
[root@zyy httpd]# ansible all -m shell -a 'ss -ant'
web01.example.com | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
ESTAB 0 0 192.168.80.13:22 192.168.80.20:33432
ESTAB 0 0 192.168.80.13:22 192.168.80.1:55893
LISTEN 0 32 *:21 *:*
LISTEN 0 128 [::]:22 [::]:*