ansible常用模块
1. ansible常用模块使用详解
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@ansible ~]# ansible all -m ping
10.10.10.135| SUCCESS => {
"changed": false,
"ping": "pong"
}
3. ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。
//查看受控主机的/tmp目录内容
[root@JLin ~]# ansible 192.168.23.117 -a 'ls /tmp'
192.168.23.117 | SUCCESS | rc=0 >>
ansible_Xs1oym
systemd-private-fa034beb13644acfb2aadc35bfe64d46-chronyd.service-cVTNsE
systemd-private-fa034beb13644acfb2aadc35bfe64d46-vgauthd.service-XAgkCm
systemd-private-fa034beb13644acfb2aadc35bfe64d46-vmtoolsd.service-rwqet5
//在受控主机的/tmp目录下新建一个文件test
[root@ansible ~]# ansible 10.10.10.135 -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.
10.10.10.135| SUCCESS | rc=0 >>
[root@ansible ~]# ansible 10.10.10.135-a 'ls /tmp'
10.10.10.135 | SUCCESS | rc=0 >>
ansible_7YD229
systemd-private-fa034beb13644acfb2aadc35bfe64d46-chronyd.service-cVTNsE
systemd-private-fa034beb13644acfb2aadc35bfe64d46-vgauthd.service-XAgkCm
systemd-private-fa034beb13644acfb2aadc35bfe64d46-vmtoolsd.service-rwqet5
test
//command模块不支持管道符,不支持重定向
[root@ansible ~]# ansible 10.10.10.135 -a "echo 'hello world' > /tmp/test"
10.10.10.135 | SUCCESS | rc=0 >>
hello world > /tmp/test
[root@ansible ~]# ansible 10.10.10.135 -a 'cat /tmp/test'
10.10.10.135 | SUCCESS | rc=0 >>
[root@ansible ~]# ansible 10.10.10.135 -a 'ps -ef|grep vsftpd'
10.10.10.135 | 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
4. ansible常用模块之raw
raw模块用于在远程主机上执行命令,其支持管道符与重定向
//支持重定向
[root@ansible ~]# ansible 10.10.10.135-m raw -a 'echo "hello world" > /tmp/test'
10.10.10.135 | SUCCESS | rc=0 >>
Shared connection to 10.10.10.135 closed.
[root@ansible ~]# ansible 10.10.10.135 -a 'cat /tmp/test'
10.10.10.135 | SUCCESS | rc=0 >>
hello world
//支持管道符
[root@ansible ~]# ansible 10.10.10.135 -m raw -a 'cat /tmp/test|grep -Eo hello'
10.10.10.135| SUCCESS | rc=0 >>
hello
Shared connection to 10.10.10.135closed.
5. ansible常用模块之shell
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
//使用shell模块在受控机上执行受控机上的脚本
[root@ansible ~]# ansible 10.10.10.135 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
10.10.10.135| SUCCESS | rc=0 >>
[root@ansible ~]# ansible 10.10.10.135 -m shell -a 'cat /tmp/test'
10.10.10.135| SUCCESS | rc=0 >>
6. ansible常用模块之script
script模块用于在受控机上执行主控机上的脚本
7. ansible常用模块之template
[root@localhost ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@ansible yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@ansible ~]# ansible 10.10.10.135 -m template -a 'src=/etc/yum.repos.d/CentOS8-Base-2111.repo dest=/etc/yum.repos.d/163.repo'
10.10.10.135 | SUCCESS => {
"changed": true,
"checksum": "60b8868e0599489038710c45025fc11cbccf35f2",
"dest": "/etc/yum.repos.d/163.repo",
"gid": 0,
"group": "root",
"md5sum": "5a3e688854d9ceccf327b953dab55b21",
"mode": "0644",
"owner": "root",
"size": 1462,
"src": "/root/.ansible/tmp/ansible-tmp-1536311319.27-78101453778196/source",
"state": "file",
"uid": 0
}
8. ansible常用模块之yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
- name:要管理的包名
- state:要进行的操作
state常用的值:
- latest:安装软件
- installed:安装软件
- present:安装软件
- removed:卸载软件
- absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常
//在受控机上查询看vsftpd软件是否安装
[root@localhost ~]# rpm -qa|grep vsftpd
[root@localhost ~]#
//在ansible主机上使用yum模块在受控机上安装vsftpd
[root@ansible ~]# ansible 10.10.10.135 -m yum -a 'name=vsftpd state=present'
10.10.10.135 | SUCCESS => {
"changed": true,
//查看受控机上是否安装了vsftpd
[root@localhost ~]# rpm -qa|grep vsftpd
vsftpd-3.0.3-34.el8.x86_64
9. ansible常用模块之copy
copy模块用于复制文件至远程受控机。
[root@ansible ~]# ls /etc/ansible/scripts/ a.sh [root@ansible ~]# ansible 10.10.10.135 -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts/' 10.10.10.135| SUCCESS => { "changed": true, "checksum": "83f66f804c195247885b013912cf9dc649f36391", "dest": "/scripts/a.sh", "gid": 0, "group": "root", "md5sum": "a63e880a932bba1160f329836cbfd730", "mode": "0644", "owner": "root", "size": 61, "src": "/root/.ansible/tmp/ansible-tmp-1536406467.26-35192956264311/source", "state": "file", "uid": 0 } [root@ansible ~]# ansible 10.10.10.135 -m shell -a 'ls /scripts/' 10.10.10.135 | SUCCESS | rc=0 >> a.sh test.sh
10. ansible常用模块之group
group模块用于在受控机上添加或删除组。
[root@ansible ~]# ansible 10.10.10.135 -m group -a 'name=mysql gid=1000 state=present'
192.168.23.117 | SUCCESS => {
"changed": true,
"gid": 1000,
"name": "mysql",
"state": "present",
"system": false
}
[root@ansible ~]# ansible 10.10.10.135-m shell -a 'grep mysql /etc/group'
10.10.10.135| SUCCESS | rc=0 >>
mysql:x:1000:
//删除受控机上的mysql组
[root@ansible ~]# ansible 10.10.10.135 -m group -a 'name=mysql state=absent'
10.10.10.135 | SUCCESS => {
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@ansible ~]# ansible 10.10.10.135 -m shell -a 'grep mysql /etc/group'
10.10.10.135 | FAILED | rc=1 >>
non-zero return code
11. ansible常用模块之user
user模块用于管理受控机的用户帐号。
[root@ansible ~]# ansible 10.10.10.135 -m user -a 'name=mysql uid=1000 system=yes create_home=no shell=/sbin/nologin state=present'
10.10.10.135 | SUCCESS => {
"changed": true,
"comment": "",
"create_home": false,
"group": 1000,
"home": "/home/mysql",
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 1000
}
[root@ansible ~]# ansible 10.10.10.135 -m shell -a 'grep mysql /etc/passwd'
10.10.10.135 | SUCCESS | rc=0 >>
mysql:x:1000:100::/home/mysql:/sbin/nologin
[root@ansible ~]# ansible 10.10.10.135m shell -a 'ls /home'
10.10.10.135 | SUCCESS | rc=0 >>
jerry
//修改mysql用户的uid为555
[root@ansible ~]# ansible 10.10.10.135 -m user -a 'name=mysql uid=555'
10.10.10.135| SUCCESS => {
"append": false,
"changed": true,
"comment": "",
"group": 1000,
"home": "/home/mysql",
"move_home": false,
"name": "mysql",
"shell": "/sbin/nologin",
"state": "present",
"uid": 555
}
[root@ansible~]# ansible 10.10.10.135 -m shell -a 'grep mysql /etc/passwd'
10.10.10.135 | SUCCESS | rc=0 >>
mysql:x:555:1000::/home/mysql:/sbin/nologin
//删除受控机上的mysql用户
[root@ansible ~]# ansible 10.10.10.135 -m user -a 'name=mysql state=absent'
10.10.10.135 | SUCCESS => {
"changed": true,
"force": false,
"name": "mysql",
"remove": false,
"state": "absent"
}
[root@ansible ~]# ansible 10.10.10.135 -m shell -a 'grep mysql /etc/passwd'
10.10.10.135 | FAILED | rc=1 >>
non-zero return code
12. ansible常用模块之service
service模块用于管理受控机上的服务。
[root@locahost httpd]# ansible all -m shell -a 'systemctl is-active vsftpd'
web01.example.com | FAILED | rc=3 >>
inactivenon-zero return code
//查看受控机上的vsftpd服务是否启动
[root@localhost 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@localhost httpd]# ansible all -m service -a 'name=vsftpd state=stoppped'
//停止服务
[root@localhost httpd]# ansible all -m shell -a 'systemctl is-active vsftpd'
web01.example.com | CHANGED | rc=0 >>
active
[root@lochost 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 [::]:*