【ansible常用模块】

ansible常用模块

1. ansible常用模块使用详解

ansible常用模块有:

  • ping
  • yum
  • template
  • copy
  • user
  • group
  • service
  • raw
  • command
  • shell
  • script

ansible常用模块rawcommandshell的区别:

  • shell模块调用的/bin/sh指令执行
  • command模块不是调用的shell的指令,所以没有bash的环境变量
  • raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了

2. ansible常用模块之ping

ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong

[root@lch http]# ansible all -m ping
[WARNING]: Found both group and host with same name: apache
apache | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.200.5 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

3. ansible常用模块之command

command模块用于在远程主机上执行命令,ansible默认就是使用command模块。

command模块有一个缺陷就是不能使用管道符和重定向功能。


//在受控主机的/tmp目录下新建一个文件test
[root@lch http]# ansible apache -a 'touch /tmp/test'
apache | CHANGED | rc=0 >>[root@lch http]# ansible apache -a 'ls /tmp'
apache | CHANGED | rc=0 >>
ansible_command_payload_kmjhutfk
ks-script-236q3535
test
vmware-root_960-2999133023
vmware-root_962-2990678749
vmware-root_990-2999657286
//查看受控主机的/tmp目录内容
[root@ansible ~]# ansible 172.16.103.129 -a 'ls /tmp'
172.16.103.129 | SUCCESS | rc=0 >>
ansible_Xs1oym
systemd-private-fa034beb13644acfb2aadc35bfe64d46-chronyd.service-cVTNsE
systemd-private-fa034beb13644acfb2aadc35bfe64d46-vgauthd.service-XAgkCm
systemd-private-fa034beb13644acfb2aadc35bfe64d46-vmtoolsd.service-rwqet5
//command模块不支持管道符,不支持重定向
[root@lch http]# ansible all -a 'ps -ef|grep vsftpd'
apache | 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

4. ansible常用模块之raw

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

//支持重定向
[root@lch http]# ansible apache -m raw -a 'echo "hello world" > /tmp/test'
apache | CHANGED | rc=0 >>
Shared connection to apache closed.

[root@lch http]# ansible apache -a 'cat /tmp/test'
apache | CHANGED | rc=0 >>
hello world
//支持管道符
[[root@lch http]# ansible apache -m raw -a 'cat /tmp/test|grep -Eo hello'
apache | CHANGED | rc=0 >>
hello
Shared connection to apache closed.

5. ansible常用模块之shell

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

[root@lch http]# ansible all -m shell -a 'ps -ef|grep sleep'
apache | CHANGED | rc=0 >>
root        3264    3199  0 12:58 pts/1    00:00:00 /bin/sh -c /usr/libexec/platform-python /root/.ansible/tmp/ansible-tmp-1654405089.8795831-1984-47979258920347/AnsiballZ_command.py && sleep 0
root        3280    3279  0 12:58 pts/1    00:00:00 /bin/sh -c ps -ef|grep sleep
root        3282    3280  0 12:58 pts/1    00:00:00 grep sleep
[root@lch http]# ansible all -m shell -a 'echo "hehe">>/tmp/test'
apache | CHANGED | rc=0 >>

[root@lch http]# ansible all -m shell -a 'cat /tmp/test'
apache | CHANGED | rc=0 >>
hello world
hehe
//使用shell模块在受控机上执行受控机上的脚本
[root@apache ~]# mkdir /scripts
[root@apache ~]# cd /scripts/
[root@apache scripts]# ls
[root@apache scripts]# vi test.sh
#!/bin/bash

sleep 7000 &
[root@lch http]# ansible apache -m shell -a '/bin/bash /scripts/test.sh'
apache | CHANGED | rc=0 >>
[root@lch http]# ansible all -m shell -a 'ps -ef|grep sleep'
apache | CHANGED | rc=0 >>
root        4036    3879  0 13:10 pts/1    00:00:00 /bin/sh -c /usr/libexec/platform-python /root/.ansible/tmp/ansible-tmp-1654405814.3025467-2186-172640688754537/AnsiballZ_command.py && sleep 0
root        4052    4051  0 13:10 pts/1    00:00:00 /bin/sh -c ps -ef|grep sleep
root        4054    4052  0 13:10 pts/1    00:00:00 grep sleep
[root@apache ~]# ps -ef|grep sleep
root        4066    1599  0 13:10 pts/0    00:00:00 grep --color=auto sleep
[root@apache ~]# vi /scripts/test.sh
#!/bin/bash

nohup sleep 7000 &
~
[root@lch http]# ansible all -m shell -a 'ps -ef|grep sleep'
apache | CHANGED | rc=0 >>
root        4132    3879  0 13:10 pts/1    00:00:00 /bin/sh -c /usr/libexec/platform-python /root/.ansible/tmp/ansible-tmp-1654405854.09366-2204-196832307029409/AnsiballZ_command.py && sleep 0
root        4148    4147  0 13:10 pts/1    00:00:00 /bin/sh -c ps -ef|grep sleep
root        4150    4148  0 13:10 pts/1    00:00:00 grep sleep

6. ansible常用模块之script

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

[root@lch ansible]# vim script.sh
[root@lch ansible]# chmod +x script.sh 
[root@lch ansible]# ll
总用量 32
-rw-r--r-- 1 root root 20025 7月  17 10:58 ansible.cfg
-rw-r--r-- 1 root root  1016 6月  23 08:12 hosts
-rw-r--r-- 1 root root    18 7月  18 14:41 inventory
drwxr-xr-x 2 root root     6 7月  16 11:32 mulu
drwxr-xr-x 2 root root     6 6月  23 08:12 roles
-rwxr-xr-x 1 root root    19 7月  18 16:13 script.sh
[root@lch ansible]# ansible all -m script -a"/etc/ansible/script.sh"   //在受控制主机上执行控制节点的脚本
192.168.65.128 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.65.128 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.65.128 closed."
    ], 
    "stdout": "文件系统                 容量  已用  可用 已用% 挂载点\r\n/dev/mapper/centos-root   50G  4.6G   45G   10% /\r\ndevtmpfs                 2.0G     0  2.0G    0% /dev\r\ntmpfs                    2.0G     0  2.0G    0% /dev/shm\r\ntmpfs                    2.0G   13M  2.0G    1% /run\r\ntmpfs                    2.0G     0  2.0G    0% /sys/fs/cgroup\r\n/dev/sda1               1014M  170M  845M   17% /boot\r\n/dev/mapper/centos-home   24G   35M   24G    1% /home\r\ntmpfs                    407M   12K  407M    1% /run/user/42\r\ntmpfs                    407M     0  407M    0% /run/user/0\r\n", 
    "stdout_lines": [
        "文件系统                 容量  已用  可用 已用% 挂载点", 
        "/dev/mapper/centos-root   50G  4.6G   45G   10% /", 
        "devtmpfs                 2.0G     0  2.0G    0% /dev", 
        "tmpfs                    2.0G     0  2.0G    0% /dev/shm", 
        "tmpfs                    2.0G   13M  2.0G    1% /run", 
        "tmpfs                    2.0G     0  2.0G    0% /sys/fs/cgroup", 
        "/dev/sda1               1014M  170M  845M   17% /boot", 
        "/dev/mapper/centos-home   24G   35M   24G    1% /home", 
        "tmpfs                    407M   12K  407M    1% /run/user/42", 
        "tmpfs                    407M     0  407M    0% /run/user/0"
    ]
}

7. ansible常用模块之template

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

[root@lch http]# ansible all -m shell -a 'rm -rf /etc/yum.repos.d/*'
apache | CHANGED | rc=0 >>
//删除受管机的源
[root@apache ~]# ls /etc/yum.repos.d/
[root@apache ~]# 
[root@lch http]# mkdir files
[root@lch http]# ls
ansible.cfg  files  inventory
[root@lch http]# cd files/
[root@lch files]# cp /etc/yum.repos.d/CentOS-Base.repo .
[root@lch files]# ls
CentOS-Base.repo
[root@lch files]# cd ..
[root@lch http]# ansible all -m template -a 'src=files/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo owner=root group=root mode=0644'
apache | 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-1654406280.704396-2275-252237326801674/source",
    "state": "file",
    "uid": 0
}
[root@apache ~]# ls /etc/yum.repos.d/
CentOS-Base.repo

8. ansible常用模块之yum

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

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

state常用的值:

  • latest:安装软件
  • installed:安装软件
  • present:安装软件
  • removed:卸载软件
  • absent:卸载软件

若想使用yum来管理软件,请确保受控机上的yum源无异常。

[root@lch http]# ansible all -m yum -a 'name=vsftpd state=present'
apache | 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"
    ]
}
[root@apache ~]# dnf list all|grep vsftpd
vsftpd.x86_64                                          3.0.3-34.el8                                           @AppStream
[root@lch http]# ansible all -m yum -a 'name=vsftpd state=absent' //卸载
apache | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Removed: vsftpd-3.0.3-34.el8.x86_64"
    ]

9. ansible常用模块之copy

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

[root@lch http]# ansible all -m copy -a 'content="xixi" dest=/tmp/test'
apache | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "1daf4eb81fe0f76949323544174e6e4900a39d39",
    "dest": "/tmp/test",
    "gid": 0,
    "group": "root",
    "md5sum": "de156e39c8481df78050021b1ffcd425",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_tmp_t:s0",
    "size": 4,
    "src": "/root/.ansible/tmp/ansible-tmp-1654406526.8546662-2370-113565950678472/source",
    "state": "file",
    "uid": 0
}
[root@apache ~]# cat /tmp/test
xixi[root@apache ~]#

[root@lch http]#  ansible all -m copy -a 'content="xixi\n" dest=/tmp/test'
apache | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "238da442f5b09e4de53ed2ccc88aaa4f261edd68",
    "dest": "/tmp/test",
    "gid": 0,
    "group": "root",
    "md5sum": "7b3b4019404af03d48fcf969bb81f516",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_tmp_t:s0",
    "size": 5,
    "src": "/root/.ansible/tmp/ansible-tmp-1654406605.8528-2398-21550100816730/source",
    "state": "file",
    "uid": 0
}
xixi[root@apache ~]# cat /tmp/test
xixi
[root@lch http]# ansible all -m copy -a 'src=inventory dest=/tmp/inventory owner=root group=root mode=0644'
apache | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "37f4b40d912f9a1b931475097c74082152c6f7f4",
    "dest": "/tmp/inventory",
    "gid": 0,
    "group": "root",
    "md5sum": "60f06b34a4820d75fa8f44397a0d3184",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 15,
    "src": "/root/.ansible/tmp/ansible-tmp-1654406631.6810453-2427-218794820681598/source",
    "state": "file",
    "uid": 0
}
[root@apache ~]# ll /tmp
total 8
-rw-r--r--. 1 root root 15 Jun  5 13:23 inventory
-rw-r--r--. 1 root root  5 Jun  5 13:23 test
drwx------. 2 root root  6 Jun  5 11:58 vmware-root_960-2999133023

10. ansible常用模块之group

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

[root@lch http]# ansible all -m group -a 'name=liu gid=333 state=present'  //创建
apache | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 333,
    "name": "liu",
    "state": "present",
    "system": false
}
[root@apache ~]# grep liu /etc/group
liu:x:333:
[root@lch http]# ansible all -m group -a 'name=liu gid=333 state=absent'  //删除
apache | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "liu",
    "state": "absent"
}
[root@apache ~]# grep liu /etc/group
[root@apache ~]# 

11. ansible常用模块之user

user模块用于管理受控机的用户帐号。

[root@lch http]#  ansible all -m user -a 'name=liu uid=333 system=yes create_home=no shell=/sbin/nologin state=present' //创建用户
apache | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 333,
    "home": "/home/liu",
    "name": "liu",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 333
}
[root@lch http]# ansible all -m user -a 'name=liu uid=444'  
//修改uid
apache | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "append": false,
    "changed": true,
    "comment": "",
    "group": 333,
    "home": "/home/liu",
    "move_home": false,
    "name": "liu",
    "shell": "/sbin/nologin",
    "state": "present",
    "uid": 444
}
[root@apache ~]# id liu
uid=444(liu) gid=333(liu) groups=333(liu)
[root@lch http]# ansible all -m user -a 'name=liu state=absent'  //删除
apache | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "liu",
    "remove": false,
    "state": "absent"
}
[root@apache ~]# id liu
id: ‘liu’: no such user

12. ansible常用模块之service

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


[root@lch httpd]# ansible all -m shell -a 'systemctl is-active vsftpd'
apache | FAILED | rc=3 >>
inactivenon-zero return code
 
//查看受控机上的vsftpd服务是否启动
 
[root@lch httpd]# ansible all -m service -a 'name=vsftpd state=started'
//启动受控机上的vsftpd服务
 
[root@lch httpd]# ansible all -m shell -a 'systemctl is-enabled vsftpd'
//查看受控机上的vsftpd服务是否开机自动启动
 
 
[root@lch httpd]# ansible all -m service -a 'name=vsftpd state=stoppped'
//停止服务
 
 
[root@lch httpd]# ansible all -m shell -a 'systemctl is-active vsftpd'
apache | CHANGED | rc=0 >>
active
[root@lch httpd]# ansible all -m shell -a 'ss -ant'
apache | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port  Peer Address:Port Process
LISTEN 0      128          0.0.0.0:22         0.0.0.0:*           
ESTAB  0      0      192.168.200.5:22   192.168.200.8:49778       
ESTAB  0      0      192.168.200.5:22   192.168.200.1:60028      
LISTEN 0      32                 *:21               *:*           
LISTEN 0      128             [::]:22            [::]:*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值