ansible 常用模块
一、ansible常用模块使用详解
ansible常用模块有:
- ping
- yum
- template
- copy
- user
- group
- serviceb
- raw
- command
- shell
- script
ansible常用模块raw、command、shell的区别:
- shell模块调用的/bin/sh指令执行
- command模块不是调用的shell的指令,所以没有bash的环境变量
- raw很多地方和shell类似,更多的地方建议使用shell和command模块。
二、ansible常用模块之ping
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@localhost ~]# ansible all -m ping
192.168.153.130 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
三、ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
在受控主机上创建文件
[root@localhost ansible]# ansible all -m command -a 'touch abc'
[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.153.130 | CHANGED | rc=0 >>
查看受控主机root家目录
[root@localhost ansible]# ansible all -m command -a 'ls /root'
192.168.153.130 | CHANGED | rc=0 >>
abc
anaconda-ks.cfg
command模块不支持管道符,不支持重定向
[root@localhost ansible]# ansible all -a "echo 'hello world' > /root/abc"
192.168.153.130 | CHANGED | rc=0 >>
hello world > /root/abc
[root@localhost ansible]# ansible all -a 'cat /root/abc'
192.168.153.130 | CHANGED | rc=0 >>
[root@localhost ansible]# ansible all -a 'ps -ef|grep vsftpd'
192.168.153.130 | FAILED | rc=1 >>
error: unsupported SysV 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
四、ansible常用模块之raw
raw模块用于在远程主机上执行命令,其支持管道符与重定向
支持重定向
[root@localhost ansible]# ansible all -m raw -a "echo 'hello world' > /root/abc"
192.168.153.130 | CHANGED | rc=0 >>
Shared connection to 192.168.153.130 closed.
[root@localhost ansible]# ansible all -m raw -a 'cat /root/abc'
192.168.153.130 | CHANGED | rc=0 >>
hello world
Shared connection to 192.168.153.130 closed.
支持管道符
[root@localhost ansible]# ansible all -m raw -a 'cat /root/abc|grep -Eo hello'
192.168.153.130 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.153.130 closed.
五、ansible常用模块之shell
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
查看受控机上的脚本
[root@localhost ~]# ls
anaconda-ks.cfg test.sh
使用shell模块在受控机上执行受控机上的脚本
[root@localhost ~]# ansible all -m shell -a '/bin/bash test.sh &> /root/abc'
192.168.153.130 | CHANGED | rc=0 >>
[root@localhost ~]# ansible all -m shell -a 'cat /root/abc'
192.168.153.130 | CHANGED | rc=0 >>
1
2
3
六、ansible常用模块之script
script模块用于在受控机上执行主控机上的脚本
[root@localhost ~]# ls
anaconda-ks.cfg test.sh
[root@localhost ~]# ansible all -m script -a '/root/test.sh &>/tmp/a'
192.168.153.130 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.153.130 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.153.130 closed."
],
"stdout": "",
"stdout_lines": []
}
查看受控机上的/tmp/a文件内容
[root@localhost ~]# ansible all -m shell -a 'cat /tmp/a'
192.168.153.130 | CHANGED | rc=0 >>
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
仓库标识 仓库名称
AppStream AppStream
BaseOS BaseOS
由此可见确是在受控机上执行了主控机上的脚本,且输出记录到了受控机上
七、ansible常用模块之template
template模块用于生成一个模板,并可将其传输至远程主机上。
下载一个163的yum源文件并开启此源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1572 100 1572 0 0 5257 0 --:--:-- --:--:-- --:--:-- 5257
[root@localhost yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
将设置好的163源传到受控主机
[root@localhost ~]# ansible all -m template -a 'src=/etc/yum.repos.d/CentOS7-Base-163.repo dest=/etc/yum.repos.d/163.repo'
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "60b8868e0599489038710c45025fc11cbccf35f2",
"dest": "/etc/yum.repos.d/163.repo",
"gid": 0,
"group": "root",
"md5sum": "5a3e688854d9ceccf327b953dab55b21",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 1462,
"src": "/root/.ansible/tmp/ansible-tmp-1626593912.4259317-520562-112765083028768/source",
"state": "file",
"uid": 0
}
查看受控机上是否有163源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
163.repo redhat.repo Redhat.repo
[root@localhost yum.repos.d]#
八、 ansible常用模块之yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
- name:要管理的包名
- state:要进行的操作
state常用的值:
- latest:安装软件
- installed:安装软件
- present:安装软件
- removed:卸载软件
- absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常。
在受控机上查询看nginx软件是否安装
[root@localhost ~]# rpm -qa|grep nginx
[root@localhost ~]#
在ansible主机上使用yum模块在受控机上安装nginx
[root@localhost ~]# ansible all -m yum -a 'name=nginx state=present'
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: nginx-mod-http-perl-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Installed: nginx-mod-http-xslt-filter-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Installed: nginx-mod-mail-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Installed: nginx-mod-stream-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Installed: nginx-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Installed: nginx-all-modules-1:1.14.1-9.module+el8.0.0+4108+af250afe.noarch",
"Installed: nginx-mod-http-image-filter-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64"
]
}
在ansible主机上使用yum模块在受控机上卸载nginx
[root@localhost ~]# ansible all -m yum -a 'name=nginx state=absent'
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Removed: nginx-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Removed: nginx-all-modules-1:1.14.1-9.module+el8.0.0+4108+af250afe.noarch",
"Removed: nginx-mod-http-image-filter-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Removed: nginx-mod-http-perl-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Removed: nginx-mod-http-xslt-filter-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Removed: nginx-mod-mail-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64",
"Removed: nginx-mod-stream-1:1.14.1-9.module+el8.0.0+4108+af250afe.x86_64"
]
}
查看受控机上是否安装了nginx
[root@localhost yum.repos.d]# rpm -qa|grep nginx
nginx-mod-http-xslt-filter-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
nginx-mod-stream-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
nginx-filesystem-1.14.1-9.module+el8.0.0+4108+af250afe.noarch
nginx-mod-http-perl-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
nginx-mod-mail-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
nginx-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
nginx-mod-http-image-filter-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
nginx-all-modules-1.14.1-9.module+el8.0.0+4108+af250afe.noarch
九、ansible常用模块之copy
copy模块用于复制文件至远程受控机。
[root@localhost ~]# ls
anaconda-ks.cfg test.sh
[root@localhost ~]# ansible all -m copy -a 'src=/root/test.sh dest=/root/123/abc'
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "6980b4dac486cf047d5225fec71bf7d2062cfa9a",
"dest": "/root/123/abc",
"gid": 0,
"group": "root",
"md5sum": "3bbe00927fa617c277c0d95df2de5843",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 638,
"src": "/root/.ansible/tmp/ansible-tmp-1626594547.5570693-540498-11456649841052/source",
"state": "file",
"uid": 0
}
[root@localhost ~]# ansible all -m shell -a 'ls /root/123'
192.168.153.130 | CHANGED | rc=0 >>
abc
十、ansible常用模块之group
group模块用于在受控机上添加或删除组。
受控机上添加一个系统组,其gid为400,组名为mysql
[root@localhost ~]# ansible all -m group -a 'name=mysql gid=400 state=present'
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 400,
"name": "mysql",
"state": "present",
"system": false
}
[root@localhost ~]# ansible all -m shell -a 'grep mysql /etc/group'
192.168.153.130 | CHANGED | rc=0 >>
mysql:x:400:
删除受控机上的mysql组
[root@localhost ~]# ansible all -m group -a 'name=mysql state=absent'
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@localhost ~]# ansible all -m shell -a 'grep mysql /etc/group'
192.168.153.130 | FAILED | rc=1 >>
non-zero return code
[root@localhost ~]#
十一、ansible常用模块之user
user模块用于管理受控机的用户帐号。
在受控机上添加一个系统用户,用户名为mysql,uid为300,设置其shell为/sbin/nologin,无家目录
[root@localhost ~]# ansible all -m user -a 'name=mysql uid=300 system=yes c
reate_home=no shell=/sbin/nologin state=present '
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 300,
"home": "/home/mysql",
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 300
}
[root@localhost ~]# ansible all -m shell -a 'grep mysql /etc/passwd'
192.168.153.130 | CHANGED | rc=0 >>
mysql:x:300:300::/home/mysql:/sbin/nologin
[root@localhost ~]# ansible all -a 'ls /home'
192.168.153.130 | CHANGED | rc=0 >>
admin
修改mysql用户的uid为360
[root@localhost ~]# ansible all -m user -a 'name=mysql uid=360 '
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"append": false,
"changed": true,
"comment": "",
"group": 300,
"home": "/home/mysql",
"move_home": false,
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"uid": 360
}
[root@localhost ~]# ansible all -m shell -a 'grep mysql /etc/passwd'
192.168.153.130 | CHANGED | rc=0 >>
mysql:x:360:300::/home/mysql:/sbin/nologin
/删除受控机上的mysql用户
[root@localhost ~]# ansible all -m user -a 'name=mysql state=absent '
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "mysql",
"remove": false,
"state": "absent"
}
[root@localhost ~]# ansible all -m shell -a 'grep mysql /etc/passwd'
192.168.153.130 | FAILED | rc=1 >>
non-zero return code
十二、ansible常用模块之service
service模块用于管理受控机上的服务。
查看受控机上的vsftpd服务是否启动
[root@localhost ~]# ansible all -m shell -a 'systemctl is-active nginx'
192.168.153.130 | FAILED | rc=3 >>
inactivenon-zero return code
启动受控机上的vsftpd服务
[root@localhost ~]# ansible all -m service -a 'name=nginx state=started'
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "nginx",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
查看受控机上的vsftpd服务是否开机自动启动
[root@localhost ~]# ansible all -m shell -a 'systemctl is-enabled nginx'
192.168.153.130 | FAILED | rc=1 >>
disablednon-zero return code
设置受控机上的vsftpd服务开机自动启动
[root@localhost ~]# ansible all -m service -a 'name=nginx enabled=yes'
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "nginx",
"status": {
停止受控机上的vsftpd服务
[root@localhost ~]# ansible all -m service -a 'name=nginx state=stopped'
192.168.153.130 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "nginx",
"state": "stopped",
"status": {