- A+
所属分类:linux技术
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.rn", "stderr_lines": [ "Shared connection to 192.168.222.137 closed." ], "stdout": "1rn2rn3rn4rn5rn6rn7rn8rn9rn10rn", "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 [::]:*