文章目录
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@cxr ~]# ansible all -m ping
web.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3. ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。
//查看受控主机的/tmpm目录内容
[root@cxr ~]# ansible web.example.com -a 'ls /tmp'
web.example.com | CHANGED | rc=0 >>
anaconda.log
ansible_command_payload_a7gwlx8h
dbus.log
ifcfg.log
ks-script-c7b5auy9
packaging.log
//在受控主机的/tmp目录下新建一个文件test
[root@cxr ~]# ansible web.example.com -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.
web.example.com | CHANGED | rc=0 >>
[root@cxr ~]# ansible web.example.com -a 'ls /tmp'
web.example.com | CHANGED | rc=0 >>
anaconda.log
ansible_command_payload_pvrjsmw1
dbus.log
ifcfg.log
ks-script-c7b5auy9
packaging.log
program.log
sensitive-info.log
storage.log
systemd-private-db333c9769414b7aa4ef97064790be8d-bluetooth.service-CQ9N93
systemd-private-db333c9769414b7aa4ef97064790be8d-bolt.service-oGCV40
systemd-private-db333c9769414b7aa4ef97064790be8d-chronyd.service-3JFVlN
systemd-private-db333c9769414b7aa4ef97064790be8d-colord.service-KXiNIz
systemd-private-db333c9769414b7aa4ef97064790be8d-fwupd.service-1DEzuM
systemd-private-db333c9769414b7aa4ef97064790be8d-geoclue.service-bE8MR2
systemd-private-db333c9769414b7aa4ef97064790be8d-ModemManager.service-xHx1Zk
systemd-private-db333c9769414b7aa4ef97064790be8d-rtkit-daemon.service-rSpyaC
test
tracker-extract-files.0
vmware-root_1014-2965448048
vmware-root_1015-4281777838
vmware-root_1017-4248221863
vmware-root_997-4257200444
//command模块不支持管道符,不支持重定向
[root@cxr ~]# ansible web.example.com -a 'echo "hello world" > /tmp/test'
web.example.com | CHANGED | rc=0 >>
hello world > /tmp/test
[root@cxr ~]# ansible web.example.com -a 'cat /tmp/test'
web.example.com | CHANGED | rc=0 >>
[root@cxr ~]# ansible web.example.com -a 'ps -ef | grep vsftpd'
web.example.com | 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模块用于在远程主机上执行命令,其支持管道符与重定向
//支持重定向
[root@cxr ~]# ansible web.example.com -m raw -a 'echo "hello world" > /tmp/test'
web.example.com | CHANGED | rc=0 >>
Shared connection to web.example.com closed.
[root@cxr ~]# ansible web.example.com -a 'cat /tmp/test'
web.example.com | CHANGED | rc=0 >>
hello world
//支持管道符
[root@cxr ~]# ansible web.example.com -m raw -a 'cat /tmp/test | grep -Eo hello'
web.example.com | CHANGED | rc=0 >>
hello
Shared connection to web.example.com closed.
5. ansible常用模块之壳
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
//查看受控机上的脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# vim /scripts/test.sh
[root@localhost ~]# cat /scripts/test.sh
#!/bin/bash
nohup sleep 7000 &
//使用shell模块在受控机上执行受控机上的脚本
[root@cxr ~]# ansible web.example.com -m shell -a '/bin/bash /scripts/test.sh'
web.example.com | CHANGED | rc=0 >>
[root@cxr ~]# ansible web.example.com -m shell -a 'ps -ef | grep sleep'
web.example.com | CHANGED | rc=0 >>
root 79516 1 0 03:54 ? 00:00:00 sleep 7000
6. ansible常用模块之script
script模块用于在受控机上执行主控机上的脚本
[root@cxr ~]# ansible web.example.com -m script -a '/etc/ansible/scripts/a.sh &>/tmp/a'
web.example.com | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to web.example.com closed.\r\n",
"stderr_lines": [
"Shared connection to web.example.com closed."
],
"stdout": "",
"stdout_lines": []
}
//查看受控机上的/tmp/a文件内容
[root@cxr ~]# ansible web.example.com -m shell -a 'cat /tmp/a'
web.example.com | CHANGED | rc=0 >>
hello world
//由此可见确是在受控机上执行了主控机上的脚本,且输出记录到了受控机上。因为此处 \
//的jerry用户是在受控机上才有的用户
7. ansible常用模块之template
template模块用于生成一个模板,并可将其传输至远程主机上。
//下载一个163的yum源文件并开启此源
[root@cxr 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 2668 0 --:--:-- --:--:-- --:--:-- 2664
[root@cxr yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@cxr yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
//将设置好的163源传到受控主机
[root@cxr yum.repos.d]# ansible web.example.com -m template -a 'src=/etc/yum.repos.d/CentOS7-Base-163.repo dest=/etc/yum.repos.d/163.repo'
web.example.com | 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-1653466931.047998-828529-270432011705769/source",
//查看受控机上是否有163源
[root@localhost ~]# ls /etc/yum.repos.d/
163.repo epel-modular.repo epel-testing-modular.repo redhat.repo
CentOS-Base.repo epel.repo epel-testing.repo
8. ansible常用模块之yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
- name:要管理的包名
- state:要进行的操作
state常用的值:
- latest:安装软件
- installed:安装软件
- present:安装软件
- removed:卸载软件
- absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常。
[root@localhost ~]# rpm -qa | grep vsftpd
[root@localhost ~]#
//在ansible主机上使用yum模块在受控机上安装vsftpd
[root@cxr ~]# ansible web.example.com -m yum -a 'name=vsftpd state=present'
web.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"
]
}
//查看受控机上是否安装了vsftpd
[root@localhost ~]# rpm -qa|grep vsftpd
vsftpd-3.0.3-34.el8.x86_64
9. ansible常用模块之copy
copy模块用于复制文件至远程受控机。
[root@cxr ~]# ls /etc/ansible/scripts/
a.sh
[root@cxr ~]# ansible web.example.com -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts'
web.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "62a1e17a8ab950e59e56563ebf327d9a14520575",
"dest": "/scripts/a.sh",
"gid": 0,
"group": "root",
"md5sum": "85239384c0243e32ef2e37e8cd3b3114",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:default_t:s0",
"size": 32,
"src": "/root/.ansible/tmp/ansible-tmp-1653467934.0774693-860355-151553900819453/source",
"state": "file",
"uid": 0
}
[root@cxr ~]# ansible web.example.com -m shell -a 'ls /scripts'
web.example.com | CHANGED | rc=0 >>
a.sh
test.sh
10. ansible常用模块之group
group模块用于在受控机上添加或删除组。
//在受控机上添加一个系统组,其gid为306,组名为mysql
[root@cxr ~]# ansible web.example.com -m group -a 'name=mysql gid=306 state=present'
web.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": false
}
[root@cxr ~]# ansible web.example.com -m shell -a 'grep mysql /etc/group'
web.example.com | CHANGED | rc=0 >>
mysql:x:306:
//删除受控机上的mysql组
[root@cxr ~]# ansible web.example.com -m group -a 'name=mysql state=absent'
web.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mysql",
"state": "absent"
}
[root@cxr ~]# ansible web.example.com -m shell -a 'grep mysql /etc/group'
web.example.com | FAILED | rc=1 >>
non-zero return code
11. ansible常用模块之user
user模块用于管理受控机的用户帐号。
//在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@cxr ~]# ansible web.example.com -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nolongin state=present'
web.example.com | 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/nolongin",
"state": "present",
"system": true,
"uid": 306
}
[root@cxr ~]# ansible web.example.com -m shell -a 'grep mysql /etc/passwd'
web.example.com | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nolongin
[root@cxr ~]# ansible web.example.com -m shell -a 'ls /home'
web.example.com | CHANGED | rc=0 >>
cxr
//修改mysql用户的uid为366
[root@cxr ~]# ansible web.example.com -m user -a 'name=mysql uid=366'
web.example.com | 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/nolongin",
"state": "present",
"uid": 366
}
[root@cxr ~]# ansible web.example.com -m shell -a 'grep mysql /etc/passwd'
web.example.com | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nolongin
//删除受控机上的mysql用户
[root@cxr ~]# ansible web.example.com -m user -a 'name=mysql state=absent'
web.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "mysql",
"remove": false,
"state": "absent"
}
[root@cxr ~]# ansible web.example.com -m shell -a 'grep mysql /etc/passwd'
web.example.com | FAILED | rc=1 >>
non-zero return code
12. ansible常用模块之service
service模块用于管理受控机上的服务。
//查看受控机上的vsftpd服务是否启动
[root@cxr ~]# ansible web.example.com -m shell -a 'systemctl is-active vsftpd'
web.example.com | FAILED | rc=3 >>
inactivenon-zero return code
//启动受控机上的vsftpd服务
[root@cxr ~]# ansible web.example.com -m service -a 'name=vsftpd state=started'
web.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "vsftpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
....此处省略N行
//查看受控机上的vsftpd服务是否启动
[root@cxr ~]# ansible web.example.com -m shell -a 'systemctl is-active vsftpd'
web.example.com | CHANGED | rc=0 >>
active
//查看受控机上的vsftpd服务是否开机自动启动
[root@cxr ~]# ansible web.example.com -m shell -a 'systemctl is-enabled vsftpd'
web.example.com | FAILED | rc=1 >>
disablednon-zero return code
//设置受控机上的vsftpd服务开机自动启动
[root@cxr ~]# ansible web.example.com -m service -a 'name=vsftpd enabled=yes'
web.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "vsftpd",
"status": {
....此处省略N行
//查看受控机上的vsftpd服务是否开机自动启动
[root@cxr ~]# ansible web.example.com -m shell -a 'systemctl is-enabled vsftpd'
web.example.com | CHANGED | rc=0 >>
enabled
//停止受控机上的vsftpd服务
[root@cxr ~]# ansible web.example.com -m service -a 'name=vsftpd state=stopped'
web.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "vsftpd",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "Wed 2022-05-25 05:42:33 EDT",
....此处省略N行
}
//查看受控机上的vsftpd服务是否开机自动启动
[root@cxr ~]# ansible web.example.com -m shell -a 'systemctl is-enabled vsftpd'
web.example.com | CHANGED | rc=0 >>
enabled
//停止受控机上的vsftpd服务
root@cxr ~]# ansible web.example.com -m service -a 'name=vsftpd state=stopped'
web.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"name": "vsftpd",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "Wed 2022-05-25 05:42:33 EDT",
...此处省略N行
[root@cxr ~]# ansible web.example.com -m shell -a 'systemctl is-active vsftpd'
web.example.com | FAILED | rc=3 >>
inactivenon-zero return code
[root@cxr ~]# ansible web.example.com -m shell -a 'ss -antl'
web.example.com | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*