ansible常用模块
ansible常用模块使用详解
nsible常用模块有:
- 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模块了
ansible常用模块之ping
ping模块用于检查指定节点机器是否连通,主机如果在线,则回复pong
[root@ansible ~]# ansible 192.168.248.133 -m ping 192.168.248.133 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "ping": "pong" }
ansible常用模块之command
command模块用于在远程主机上执行命令,ansible默认就是使用command模块,不能使用管道符和重定向。
[root@ansible ~]# ansible 192.168.248.133 -m command -a 'ls /root' 192.168.248.133 | CHANGED | rc=0 >> anaconda-ks.cfg [root@ansible ~]# ansible 192.168.248.133 -m command -a 'echo "123" > /tmp/a' 192.168.248.133 | CHANGED | rc=0 >> 123 > /tmp/a [root@ansible ~]# ansible 192.168.248.133 -m command -a 'ls /tmp' 192.168.248.133 | CHANGED | rc=0 >> ansible_command_payload_lovox0nu
ansible常用模块之raw
raw模块用于在远程主机上执行命令,其支持管道符与重定向
[root@ansible ~]# ansible 192.168.248.133 -m raw -a 'echo "123" > /tmp/a' 192.168.248.133 | CHANGED | rc=0 >> Shared connection to 192.168.248.133 closed. [root@ansible ~]# ansible 192.168.248.133 -m command -a 'ls /tmp' 192.168.248.133 | CHANGED | rc=0 >> a ansible_command_payload_oqlgr2mo
ansible常用模块之shell
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。
root@ansible ~]# ansible 192.168.248.133 -m shell -a '/bin/bash /tmp/test.sh' 192.168.248.133 | CHANGED | rc=0 >> 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:75:47:f6 brd ff:ff:ff:ff:ff:ff inet 192.168.248.133/24 brd 192.168.248.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe75:47f6/64 scope link valid_lft forever preferred_lft forever
ansible常用模块之script
script模块用于在受控机上执行主控机上的脚本
[root@ansible scripts]# ansible 192.168.248.133 -m script -a '/root/scripts/test.sh &>/tmp/a' 192.168.248.133 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.248.133 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.248.133 closed." ], "stdout": "", "stdout_lines": [] } [root@ansible scripts]# ansible 192.168.248.133 -a ' cat /tmp/a' 192.168.248.133 | CHANGED | rc=0 >> hello world
ansible常用模块之template
#使用selinux作为模板 [root@ansible template]# cp /etc/selinux/config . [root@ansible template]# cat config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted #将设置好的模板传输到192.168.248.133上 [root@ansible ~]# ansible 192.168.248.133 -m template -a 'src=/root/template/config dest=/etc/selinux/' 192.168.248.133 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "checksum": "385caf4e178c9a1dfcdaac71738934c735201480", "dest": "/etc/selinux/config", "gid": 0, "group": "root", "md5sum": "293160d55f3a26d5bc687154d028eb47", "mode": "0644", "owner": "root", "size": 547, "src": "/root/.ansible/tmp/ansible-tmp-1609988188.5656092-2545-80155753315326/source", "state": "file", "uid": 0 } [root@ansible ~]# ansible 192.168.248.133 -a 'cat /etc/selinux/config' 192.168.248.133 | CHANGED | rc=0 >> # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
ansible常用模块之yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
- name:要管理的包名
- state:要进行的操作
state常用的值:
- latest:安装软件
- installed:安装软件
- present:安装软件
- removed:卸载软件
- absent:卸载软件
#安装 [root@ansible ~]# ansible 192.168.248.133 -m yum -a 'name=make state=present' 192.168.248.133 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "msg": "", "rc": 0, "results": [ "Installed: make-1:4.2.1-10.el8.x86_64" ] } #卸载 [root@ansible ~]# ansible 192.168.248.133 -m yum -a 'name=make state=absent' 192.168.248.133 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "msg": "", "rc": 0, "results": [ "Removed: make-1:4.2.1-10.el8.x86_64" ] }
ansible常用模块之copy
copy模块用于复制文件至远程受控机。
[root@ansible scripts]# ansible 192.168.248.133 -m copy -a 'src=/root/scripts/test.sh dest=/tmp/' 192.168.248.133 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "checksum": "9cd86f975cbb1f70646a73c86a95e7c7f1625e7f", "dest": "/tmp/test.sh", "gid": 0, "group": "root", "md5sum": "c708de2c5bbd87d1d29bba99612b184c", "mode": "0755", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 31, "src": "/root/.ansible/tmp/ansible-tmp-1609988605.236166-2658-117693293460021/source", "state": "file", "uid": 0 } [root@ansible scripts]# ansible 192.168.248.133 -a ' ls /tmp/' 192.168.248.133 | CHANGED | rc=0 >> a ansible_command_payload_s_chwiie test.sh
ansible常用模块之group
group模块用于在受控机上添加或删除组。
#创建组 [root@ansible scripts]# ansible 192.168.248.133 -m group -a 'name=xx gid=4000 state=present' 192.168.248.133 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 4000, "name": "xx", "state": "present", "system": false } [root@ansible scripts]# ansible 192.168.248.133 -a 'grep xx /etc/group ' 192.168.248.133 | CHANGED | rc=0 >> xx:x:4000: #删除组 [root@ansible scripts]# ansible 192.168.248.133 -m group -a 'name=xx state=absent' 192.168.248.133 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "name": "xx", "state": "absent" } [root@ansible scripts]# ansible 192.168.248.133 -a 'grep xx /etc/group ' 192.168.248.133 | FAILED | rc=1 >> non-zero return code
ansible常用模块之user
user模块用于管理受控机的用户帐号。
#创建一个test用户,无家目录,是系统用户,不能登录 [root@ansible scripts]# ansible 192.168.248.133 -m user -a 'name=test create_home=no system=yes shell=/sbin/nologin state=present' 192.168.248.133 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "comment": "", "create_home": false, "group": 990, "home": "/home/test", "name": "test", "shell": "/sbin/nologin", "state": "present", "system": true, "uid": 992 } [root@ansible scripts]# ansible 192.168.248.133 -a 'grep test /etc/passwd' 192.168.248.133 | CHANGED | rc=0 >> test:x:992:990::/home/test:/sbin/nologin #删除test用户 [root@ansible scripts]# ansible 192.168.248.133 -m user -a 'name=test state=absent' 192.168.248.133 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "force": false, "name": "test", "remove": false, "state": "absent" } [root@ansible scripts]# ansible 192.168.248.133 -a 'grep test /etc/passwd' 192.168.248.133 | FAILED | rc=1 >> non-zero return code
ansible常用模块之service
[root@ansible ~]# ansible 192.168.248.133 -m service -a 'name=httpd enabled=true state=started' ·192.168.248.133 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "enabled": true, "name": "httpd", "state": "started", "status": { ······ #查看 [root@php ~]# systemctl status httpd ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2021-01-07 10:05:56 CST; 2min 15s ago Docs: man:httpd.service(8)