Ansible常用模塊 Ansible常用模塊詳解 ansible常用模塊有: ping yum template copy user group service raw command shell script file ansible常用模塊raw、command、shell的區別: shell ...
Ansible常用模塊
目錄
Ansible常用模塊詳解
ansible常用模塊有:
ping
yum
template
copy
user
group
service
raw
command
shell
script
file
ansible常用模塊raw、command、shell的區別:
- shell模塊調用的/bin/sh指令執行
- command模塊不是調用的shell的指令,所以沒有bash的環境變數
- raw很多地方和shell類似,更多的地方建議使用shell和command模塊。但是如果是使用老版本python,需要用到raw,又或者是客戶端是路由器,因為沒有安裝python模塊,那就需要使用raw模塊了
ansible常用模塊之ping
//將受控主機加入ansible清單
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg hosts roles
[root@ansible ansible]# touch inventory
[root@ansible ansible]# ls
ansible.cfg hosts inventory roles
[root@ansible ansible]# vim ansible.cfg
#inventory = /etc/ansible/hosts //取消註釋並修改為下麵這樣
inventory = /etc/ansible/inventory
[root@ansible ansible]# vim inventory
[root@ansible ansible]# cat inventory
[web]
192.168.222.137
[root@ansible ansible]# ansible all -m ping
192.168.222.137 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
ansible常用模塊之command
command模塊用於在遠程主機上執行命令,ansible預設就是使用command模塊。
command模塊有一個缺陷就是不能使用管道符和重定向功能。
//查看受控主機的/tmp目錄內容
[root@ansible ansible]# ansible 192.168.222.137 -a 'ls /tmp'
192.168.222.137 | CHANGED | rc=0 >>
a
ansible_command_payload_pgp9m_c8
systemd-private-ddc74647b1614d9f97693ed56992353e-nginx.service-LcMflb
vmware-root_901-3988228452
vmware-root_903-3979774182
//在受控主機的/tmp目錄下新建一個文件test
[root@ansible ansible]# ansible 192.168.222.137 -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.222.137 | CHANGED | rc=0 >>
[root@ansible ansible]# ansible 192.168.222.137 -a 'ls /tmp'
192.168.222.137 | CHANGED | rc=0 >>
a
ansible_command_payload_l91ypv4n
systemd-private-ddc74647b1614d9f97693ed56992353e-nginx.service-LcMflb
test
vmware-root_901-3988228452
vmware-root_903-3979774182
//command模塊不支持管道符,不支持重定向
[root@ansible ansible]# ansible 192.168.222.137 -a "echo 'hello world' > /tmp/test"
192.168.222.137 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible ansible]# ansible 192.168.222.137 -a 'cat /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
[root@ansible ansible]# ansible 192.168.222.137 -a 'ps -ef|grep vsftpd'
192.168.222.137 | 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@ansible ansible]# ansible 192.168.222.137 -m raw -a 'echo "hello world" > /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
Shared connection to 192.168.222.137 closed.
[root@ansible ansible]# ansible 192.168.222.137 -a 'cat /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
hello world
[root@ansible ansible]# ansible 192.168.222.137 -m raw -a 'cat /tmp/test|grep -Eo hello'
192.168.222.137 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.222.137 closed.
ansible常用模塊之shell
shell模塊用於在受控機上執行受控機上的腳本,亦可直接在受控機上執行命令。
shell模塊亦支持管道與重定向。
//查看受控機上的腳本
[root@nginx ~]# mkdir /scripts/
[root@nginx ~]# cd /scripts/
[root@nginx scripts]# vim test.sh
[root@nginx scripts]# cat test.sh
#!/bin/bash
my_arry=$(seq 1 10)
for i in ${my_arry[@]};do
echo $i
done
[root@nginx scripts]# chmod +x test.sh
[root@nginx scripts]# cd
[root@nginx ~]# ll /scripts/
total 4
-rwxr-xr-x 1 root root 76 Oct 24 02:33 test.sh
//使用shell模塊在主控機上執行受控機上的腳本
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'cat /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
1
2
3
4
5
6
7
8
9
10
ansible常用模塊之script
script模塊用於在受控機上執行主控機上的腳本
[root@ansible ansible]# mkdir -p /scripts
[root@ansible ansible]# vim /scripts/test.sh
[root@ansible ansible]# cat /scripts/test.sh
#!/bin/bash
my_arry=$(seq 1 10)
for i in ${my_arry[@]};do
echo $i
done
[root@ansible ansible]# ansible 192.168.222.137 -m script -a '/scripts/test.sh'
192.168.222.137 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.222.137 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.222.137 closed."
],
"stdout": "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n",
"stdout_lines": [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10"
]
}
ansible常用模塊之template
template模塊用於生成一個模板,並可將其傳輸至遠程主機上。
//將設置好的阿裡源傳到受控主機
[root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# rm -rf *
[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
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 3574 0 --:--:-- --:--:-- --:--:-- 3574
[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 yum.repos.d]# cd
[root@ansible ~]# ansible nginx -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo'
192.168.222.137 | 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-1666511143.7368824-130351-128775339422969/source",
"state": "file",
"uid": 0
}
//查看受控主機上面
[root@nginx ~]# ls /etc/yum.repos.d/
CentOS-Base.repo
ansible常用模塊之yum
yum模塊用於在指定節點機器上通過yum管理軟體,其支持的參數主要有兩個
-
name:要管理的包名
-
state:要進行的操作
state常用的值: -
latest:安裝軟體
-
installed:安裝軟體
-
present:安裝軟體
-
removed:卸載軟體
-
absent:卸載軟體
若想使用yum來管理軟體,請確保受控機上的yum源無異常。
//在受控機上查詢看vsftpd軟體是否安裝
[root@nginx ~]# rpm -qa|grep vsftpd
[root@nginx ~]#
//在ansible主機上使用yum模塊在受控機上安裝vsftpd
[root@ansible ansible]# ansible 192.168.222.137 -m yum -a 'name=vsftpd state=present'
192.168.222.137 | 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"
]
}
//查看受控機上是否安裝了vsftpd
[root@nginx ~]# rpm -qa|grep vsftpd
vsftpd-3.0.3-34.el8.x86_64
ansible常用模塊之copy
copy模塊用於複製文件至遠程受控機。
[root@ansible ansible]# ls xbz
ssh ssh.sh
[root@ansible ansible]# ansible 192.168.222.137 -m copy -a 'src=/etc/ansible/xbz/ssh.sh dest=/xbz/'192.168.222.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/xbz/ssh.sh",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1666551783.8760731-1363195-260276005042864/source",
"state": "file",
"uid": 0
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'ls /xbz/'
192.168.222.137 | CHANGED | rc=0 >>
ssh.sh
ansible常用模塊之group
group模塊用於在受控機上添加或刪除組。
//在受控機上添加一個系統組,其gid為306,組名為mysql
[root@ansible ansible]# ansible 192.168.222.137 -m group -a 'name=mysql gid=306 state=present'
192.168.222.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": false
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/group'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:306:
//刪除受控機上的mysql組
[root@ansible ansible]# ansible 192.168.222.137 -m group -a 'name=mysql state=absent'
192.168.222.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/group'
192.168.222.137 | FAILED | rc=1 >>
non-zero return code
ansible常用模塊之user
user模塊用於管理受控機的用戶帳號。
//在受控機上添加一個系統用戶,用戶名為mysql,uid為306,設置其shell為/sbin/nologin,無家目錄
[root@ansible ansible]# ansible 192.168.222.137 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.222.137 | 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@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'ls /home'
192.168.222.137 | CHANGED | rc=0 >>
nginx
//修改mysql用戶的uid為366
[root@ansible ansible]# ansible 192.168.222.137 -m user -a 'name=mysql uid=366'
192.168.222.137 | 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@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
//刪除受控機上的mysql用戶
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
[root@ansible ansible]# ansible 192.168.222.137 -m user -a 'name=mysql state=absent'
192.168.222.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "mysql",
"remove": false,
"state": "absent"
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | FAILED | rc=1 >>
non-zero return code
ansible常用模塊之service
service模塊用於管理受控機上的服務。
//查看受控機上的vsftpd服務是否啟動
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-active vsftpd'
192.168.222.137 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ansible]# ansible 192.168.222.137 -m service -a 'name=vsftpd state=started'
192.168.222.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "vsftpd",
"state": "started",
"status": {
"ActiveState": "inactive",
//查看受控機上的vsftpd服務是否啟動
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-active vsftpd'
192.168.222.137 | CHANGED | rc=0 >>
active
//查看受控機上的vsftpd服務是否開機自動啟動
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-enabled vsftpd'
192.168.222.137 | FAILED | rc=1 >>
disablednon-zero return code
//設置受控機上的vsftpd服務開機自動啟動
[root@ansible ansible]# ansible 192.168.222.137 -m service -a 'name=vsftpd enabled=yes'
192.168.222.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
//查看受控機上的vsftpd服務是否開機自動啟動
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-enabled vsftpd'
192.168.222.137 | CHANGED | rc=0 >>
enabled
//停止受控機上的vsftpd服務
[root@ansible ansible]# ansible 192.168.222.137 -m service -a 'name=vsftpd state=stopped'
192.168.222.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-active vsftpd'
192.168.222.137 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'ss -antl'
192.168.222.137 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*