ansible常用模块

ansible的常用模块

ansible常用模块有:
ping
yum
template
copy
user
group
service
raw
command
shell
script

ping

主机如果在线,则回复pong

[root@localhost ~]# ansible all -m ping
192.168.111.142 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

command

command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能。

查看受控主机的家目录
[root@localhost ansible]# ansible all -m command -a 'hostname'
192.168.111.142 | CHANGED | rc=0 >>
localhost.localdomain
[root@localhost ansible]# ansible all -m command -a 'hostname' -o
192.168.111.142 | CHANGED | rc=0 | (stdout) localhost.localdomain
不能用管道符和重定向
[root@localhost ansible]# ansible all -m command -a "df -h|awk '{print $4}'"
192.168.111.142 | FAILED | rc=1 >>
df:无效选项 -- |
Try 'df --help' for more information.non-zero return code

shell

shell模块用于在受控机上执行受控机上的脚本,也可直接在受控机上执行命令。shell模块支持管道与重定向。

查看受控主机的家目录
[root@localhost ansible]# ansible all -m shell -a 'hostname'
192.168.111.142 | CHANGED | rc=0 >>
localhost.localdomain
[root@localhost ansible]# ansible all -m shell -a "df -h|awk '{print $4}'"
192.168.111.142 | CHANGED | rc=0 >>
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  1.7G   16G   10% /
devtmpfs                 476M     0  476M    0% /dev
tmpfs                    488M     0  488M    0% /dev/shm
tmpfs                    488M  7.7M  480M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/sdb1               1014M   33M  982M    4% /media
/dev/sda1               1014M  130M  885M   13% /boot
/dev/sdb5                 93M  1.6M   85M    2% /mnt
tmpfs                     98M     0   98M    0% /run/user/0

[root@localhost ~]# vim test.sh
[root@localhost ~]# chmod +x test.sh
[root@localhost ~]# ll
-rwxr-xr-x.  1 root  root       25 7月  17 17:53 test.sh
[root@localhost ~]# cat test.sh
#!/bin/bash

echo "hehe"
[root@localhost ansible]#  ansible all -m shell -a '/bin/bash /root/test.sh &> /tmp/test.log'
192.168.111.142 | CHANGED | rc=0 >>
[root@localhost ~]# ls /tmp/
abc
systemd-private-4ece19a832bd4ecd820bab8b3d2c5398-chronyd.service-A8POzz
systemd-private-5ce3f8ca0f11461a84c634200dbb185d-chronyd.service-9Q39uF
systemd-private-9e048261b1bf45d7bf339b9de9a1b9dc-chronyd.service-JsCiQC
systemd-private-bb2d23be42b7476d881d8fd0516cfe20-chronyd.service-hzXLiw
systemd-private-d3345e8f326a407b9d7dfec38dde3c4e-chronyd.service-PnwINY
test.log
[root@localhost ~]# cat /tmp/test.log
hehe
[root@localhost ~]# /bin/bash /root/test.sh
hehe

raw

raw模块用于在远程主机上执行命令,其支持管道符与重定向

[root@localhost ansible]# ansible all -m raw -a 'hostname'
192.168.111.142 | CHANGED | rc=0 >>
localhost.localdomain
Shared connection to 192.168.111.142 closed.
重定向
[root@localhost ansible]# ansible all -m raw -a 'echo "hell world" > /tmp/abc'
192.168.111.142 | CHANGED | rc=0 >>
Shared connection to 192.168.111.142 closed.
[root@localhost ~]# cat /tmp/abc
hell world
管道符
[root@localhost ansible]# ansible all -m raw -a 'ss -ant|grep ESTAB'
192.168.111.142 | CHANGED | rc=0 >>
ESTAB      0      0      192.168.111.142:22                 192.168.111.1:57515              
ESTAB      0      108    192.168.111.142:22                 192.168.111.141:42456              
ESTAB      0      0      192.168.111.142:22                 192.168.111.1:57514              
Shared connection to 192.168.111.142 closed.

script

script模块用于在受控机上执行主控机上的脚本
主控机

[root@localhost scripts]# vim test.sh
[root@localhost scripts]# cat test.sh
#!/bin/bash

useradd jerry
echo "radhat" |passwd --stdin jerry
echo "hello world" > ~jerry/abc
[root@localhost scripts]# 
[root@localhost scripts]# ansible all -m script -a '/scripts/test.sh'
192.168.111.142 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.111.142 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.111.142 closed."
    ], 
    "stdout": "useradd:用户“jerry”已存在\r\n更改用户 jerry 的密码 。\r\npasswd:所有的身份验证令牌已经成功更新。\r\n", 
    "stdout_lines": [
        "useradd:用户“jerry”已存在", 
        "更改用户 jerry 的密码 。", 
        "passwd:所有的身份验证令牌已经成功更新。"
    ]
}

被控机

[root@localhost ~]# cd /home/jerry/
[root@localhost jerry]# ls
abc
[root@localhost jerry]# cat abc
hello world

template

template模块用于生成一个模板,并可将其传输至远程主机上。

[root@localhost ansible]# ansible all -m template -a 'src=~/anaconda-ks.cfg dest=/tmp/hehe'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "de336ddcdf41f5ec8f335067824b07de269e46dc", 
    "dest": "/tmp/hehe", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "2ace396e91a0aa57cc737ad5d7cc65a7", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 1241, 
    "src": "/root/.ansible/tmp/ansible-tmp-1626542523.41-2818-133496622764090/source", 
    "state": "file", 
    "uid": 0
}
[root@localhost ~]# ls /tmp/
abc
hehe

[root@localhost ~]# head -3 /tmp/hehe
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
[root@localhost ansible]# head -3 ~/anaconda-ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512

yum

yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个:

  • name:要管理的包名
  • state:要进行的操作

state常用的值:

  • latest:安装软件
  • installed:安装软件
  • present:安装软件
  • removed:卸载软件
  • absent:卸载软件
    在主控机上安装vsftpd
[root@localhost ansible]# ansible all -m yum -a 'name=vsftpd state=present'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "vsftpd"
        ]
    }, 
    [root@localhost ansible]# ansible all -m shell -a 'rpm -qa|grep vsftpd' 
192.168.111.142 | CHANGED | rc=0 >>
vsftpd-3.0.2-29.el7_9.x86_64

 [root@localhost ansible]# ansible all -m yum -a 'name=vsftpd state=installed'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "vsftpd"
        ]
    }, 

    [root@localhost ansible]# ansible all -m shell -a 'rpm -qa|grep vsftpd' 
192.168.111.142 | CHANGED | rc=0 >>
vsftpd-3.0.2-29.el7_9.x86_64

卸载vsftpd

[root@localhost ansible]# ansible all -m yum -a 'name=vsftpd state=removed'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "removed": [
            "vsftpd"
        ]
    }, 
    [root@localhost ansible]# ansible all -m shell -a 'rpm -qa|grep vsftpd' 
192.168.111.142 | FAILED | rc=1 >>
non-zero return code

[root@localhost ansible]# ansible all -m yum -a 'name=vsftpd state=absent'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "removed": [
            "vsftpd"
        ]
    }, 
[root@localhost ansible]# ansible all -m shell -a 'rpm -qa|grep vsftpd' 
192.168.111.142 | FAILED | rc=1 >>
non-zero return code

copy

copy模块用于复制文件至远程受控机。

[root@localhost ansible]# ansible all -m copy -a 'src=/etc/ansible/inventory dest=/tmp/inventory'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "dc79df9a101bba64bb4634d56d51c27167833406", 
    "dest": "/tmp/inventory", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "edaa631cba9043e14db641a94b37e239", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 30, 
    "src": "/root/.ansible/tmp/ansible-tmp-1626545850.01-3372-121921925944058/source", 
    "state": "file", 
    "uid": 0
}
[root@localhost ansible]# cat inventory
[webservers]
192.168.111.142

[root@localhost ~]# cat /tmp/inventory
[webservers]
192.168.111.142

group

group模块用于在受控机上添加或删除组。
创建组

[root@localhost ~]# ansible all -m group -a 'name=test state=present gid=5000'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 5000, 
    "name": "test", 
    "state": "present", 
    "system": false
}
[root@localhost ~]# grep test /etc/group
test:x:5000:

[root@localhost ~]# ansible all -m group -a 'name=test state=present gid=900'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 900, 
    "name": "test", 
    "state": "present", 
    "system": false
}
[root@localhost ~]# grep test /etc/group
test:x:900:

[root@localhost ~]# ansible all -m group -a 'name=test state=present gid=900 system=yes'
192.168.111.142 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "gid": 900, 
    "name": "test", 
    "state": "present", 
    "system": true
}

删除组

[root@localhost ~]# ansible all -m group -a 'name=test state=absent'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "test", 
    "state": "absent"
}

user

user模块用于管理受控机的用户帐号。
添加一个系统用户

[root@localhost ~]# ansible all -m user -a 'name=httpd system=yes shell=/sbin/nologin create_home=no state=present'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": false, 
    "group": 995, 
    "home": "/home/httpd", 
    "name": "httpd", 
    "shell": "/sbin/nologin", 
    "state": "present", 
    "system": true, 
    "uid": 997
}
[root@localhost ~]# grep httpd /etc/group
httpd:x:995:
[root@localhost ~]# id httpd
uid=997(httpd) gid=995(httpd) 组=995(httpd)
[root@localhost ~]# grep httpd /etc/passwd
httpd:x:997:995::/home/httpd:/sbin/nologin
[root@localhost ~]# ll /home
总用量 0
drwx------. 2  2001  2000  62 7月  15 18:16 hehe
drwx------. 2 jerry jerry  73 7月  18 01:13 jerry
drwx------. 3 tom   tom   122 7月  16 03:03 tom

修改uid

[root@localhost ~]# ansible all -m user -a 'name=httpd uid=956'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "append": false, 
    "changed": true, 
    "comment": "", 
    "group": 995, 
    "home": "/home/httpd", 
    "move_home": false, 
    "name": "httpd", 
    "shell": "/sbin/nologin", 
    "state": "present", 
    "uid": 956
}

service

service模块用于管理受控机上的服务。
安装

[root@localhost ~]# ansible all -m yum -a 'name=vsftpd state=present'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "vsftpd"
        ]
    }, 
[root@localhost ~]# rpm -qa|grep vsftpd
vsftpd-3.0.2-29.el7_9.x86_64

启动

[root@localhost ~]# ansible all -m service -a 'name=vsftpd state=started'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "vsftpd", 
    "state": "started", 
[root@localhost ~]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2021-07-18 02:51:53 CST; 1min 18s ago

设置开机自启

[root@localhost ~]# ansible all -m service -a 'name=vsftpd state=stopped enabled=yes'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "enabled": true, 
    "name": "vsftpd", 
    "state": "stopped", 
[root@localhost ~]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 日 2021-07-18 02:54:57 CST; 58s ago
 Main PID: 6155 (code=killed, signal=TERM)

重启

[root@localhost ~]# ansible all -m service -a 'name=vsftpd state=restarted enabled=yes'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "enabled": true, 
    "name": "vsftpd", 
    "state": "started", 
[root@localhost ~]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2021-07-18 02:58:15 CST; 36s ago

你可能感兴趣的:(ansible常用模块)