ansible常用模块

ansible常用模块


1. ansible常用模块使用详解

ansible常用模块raw、command、shell的区别:

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

2. ansible常用模块ping

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

[root@control ansible]# ansible all  -m  ping
192.168.56.130 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.56.131 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

3. ansible常用模块command

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

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

//查看受控主机的/tmp目录内容
[root@control ansible]# ansible server1  -m   command -a 'ls  /tmp'
192.168.56.130 | CHANGED | rc=0 >>
ansible_command_payload_2v_k6tp_
ks-script-99fkys6t
systemd-private-4e81050e3d834b08a7090aeb39e15698-chronyd.service-RWsuWi
systemd-private-4e81050e3d834b08a7090aeb39e15698-httpd.service-oF8K7U
vmware-root_930-2722763397
vmware-root_932-2722632322
vmware-root_934-2731086592
ware-root_935-3980298462
vmware-root_941-4022177618
vmware-root_943-4013723344
vmware-root_963-4256545027


//在受控主机的/opt目录下新建一个文件wy
[root@control ansible]# ansible server1  -m   command -a 'touch  /opt/wy'
192.168.56.130 | CHANGED | rc=0 >>



[root@control ansible]# ansible server1  -m   command -a 'ls  /opt/wy'
192.168.56.130 | CHANGED | rc=0 >>
/opt/wy



//command模块不支持管道符,不支持重定向
[root@control ansible]# ansible server1  -m   command -a 'echo  "wangyu"  >  /opt/wy'
192.168.56.130 | CHANGED | rc=0 >>
wangyu > /opt/wy


[root@control ansible]# ansible server1  -m   command -a 'cat  /opt/wy'
192.168.56.130 | CHANGED | rc=0 >>


[root@control ansible]# ansible server1  -m   command -a 'ps  -ef|grep  vstfpd'
192.168.56.130 | 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@control ansible]# ansible server1  -m  raw -a 'echo  "wangyu" > /opt/wy'
192.168.56.130 | CHANGED | rc=0 >>
Shared connection to 192.168.56.130 closed.


[root@control ansible]# ansible server1  -m  raw -a 'cat  /opt/wy'
192.168.56.130 | CHANGED | rc=0 >>
wangyu
Shared connection to 192.168.56.130 closed.



//支持管道符
[root@control ansible]# ansible server1  -m  raw -a 'cat  /opt/wy | grep -Eo w'
192.168.56.130 | CHANGED | rc=0 >>
w
Shared connection to 192.168.56.130 closed.

5. ansible常用模块shell

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

//使用shell模块在受控机上执行受控机上安装vim
[root@control ansible]# ansible server2  -m shell  -a 'yum   -y  install   vim'
192.168.56.131 | CHANGED | rc=0 >>
Last metadata expiration check: 1:53:11 ago on Sun Jul 18 17:21:53 2021.
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-15.el8         appstream      1.4 M
Installing dependencies:
 gpm-libs            x86_64      1.20.7-17.el8             appstream       39 k
 vim-common          x86_64      2:8.0.1763-15.el8         appstream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-15.el8         appstream       48 k

Transaction Summary
================================================================================
Install  4 Packages

Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/4): gpm-libs-1.20.7-17.el8.x86_64.rpm        492 kB/s |  39 kB     00:00    
(2/4): vim-filesystem-8.0.1763-15.el8.noarch.rp 1.9 MB/s |  48 kB     00:00    
(3/4): vim-enhanced-8.0.1763-15.el8.x86_64.rpm  2.2 MB/s | 1.4 MB     00:00    
(4/4): vim-common-8.0.1763-15.el8.x86_64.rpm    3.9 MB/s | 6.3 MB     00:01    
--------------------------------------------------------------------------------
Total                                           2.6 MB/s | 7.8 MB     00:03     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : vim-filesystem-2:8.0.1763-15.el8.noarch                1/4 
  Installing       : vim-common-2:8.0.1763-15.el8.x86_64                    2/4 
  Installing       : gpm-libs-1.20.7-17.el8.x86_64                          3/4 
  Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          3/4 
  Installing       : vim-enhanced-2:8.0.1763-15.el8.x86_64                  4/4 
  Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64                  4/4 
  Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64                    4/4 
  Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          1/4 
  Verifying        : vim-common-2:8.0.1763-15.el8.x86_64                    2/4 
  Verifying        : vim-enhanced-2:8.0.1763-15.el8.x86_64                  3/4 
  Verifying        : vim-filesystem-2:8.0.1763-15.el8.noarch                4/4 

Installed:
  gpm-libs-1.20.7-17.el8.x86_64         vim-common-2:8.0.1763-15.el8.x86_64    
  vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch

Complete!


//查看版本
[root@control ansible]# ansible server2  -m shell  -a 'yum  provides  vim'
192.168.56.131 | CHANGED | rc=0 >>
Last metadata expiration check: 1:55:35 ago on Sun Jul 18 17:21:53 2021.
vim-enhanced-2:8.0.1763-15.el8.x86_64 : A version of the VIM editor which includes recent enhancements
Repo        : @System
Matched from:
Provide    : vim = 8.0.1763-15.el8

vim-enhanced-2:8.0.1763-15.el8.x86_64 : A version of the VIM editor which includes recent enhancements
Repo        : appstream
Matched from:
Provide    : vim = 8.0.1763-15.el8

6. ansible常用模块script

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

[root@ansible ~]# ll /etc/ansible/scripts/
总用量 4
-rw-r--r--. 1 root root 61 9月   8 18:59 a.sh
[root@ansible ~]# ansible 192.168.56.130 -m script -a '/etc/ansible/scripts/a.sh &>/tmp/a'
192.168.56.130 | SUCCESS => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.56.130 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.56.130 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}


//查看受控机上的/tmp/a文件内容
[root@ansible ~]# ansible 192.168.56.130 -m shell -a 'cat /tmp/a'
192.168.56.130 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
....此处省略N行
jerry:x:1000:1000::/home/jerry:/bin/bash

//由此可见确是在受控机上执行了主控机上的脚本,且输出记录到了受控机上。因为此处 \
//的jerry用户是在受控机上才有的用户

7. ansible常用模块template

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

//下载一个163的yum源文件并开启此源
[root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@localhost ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo

//将设置好的163源传到受控主机
[root@control ansible]# cat  /etc/yum.repos.d/
cat: /etc/yum.repos.d/: Is a directory
[root@control ansible]# ls /etc/yum.repos.d/
CentOS-Base.repo  redhat.repo
[root@control ansible]# sed -i 's/\$releasever/8/g' /etc/yum.repos.d/CentOS-Base.repo
[root@control ansible]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS-Base.repo
[root@control ansible]#  ansible 192.168.56.130 -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/163.repo'
192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "aabdb29ff77e10a7417e552d159aa37237811750",
    "dest": "/etc/yum.repos.d/163.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "4315140128f9e051e21e0408b835c50a",
    "mode": "0644",
    "owner": "root",
    "size": 2395,
    "src": "/root/.ansible/tmp/ansible-tmp-1626618370.999911-928179-170574387499153/source",
    "state": "file",
    "uid": 0
}

//查看受控机上是否有163源
[root@server1 ~]# ls  /etc/yum.repos.d/
163.repo  CentOS-Base.repo  redhat.repo

8. ansible常用模块yum

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

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

state常用的值:

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

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

//在受控机上查询看vsftpd软件是否安装
[root@localhost ~]# rpm -qa|garep vsftpd
[root@localhost ~]#
[root@control ansible]# ansible server2  -m shell  -a 'yum   -y  install   vim'
192.168.56.131 | CHANGED | rc=0 >>
Last metadata expiration check: 1:53:11 ago on Sun Jul 18 17:21:53 2021.
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-15.el8         appstream      1.4 M
Installing dependencies:
 gpm-libs            x86_64      1.20.7-17.el8             appstream       39 k
 vim-common          x86_64      2:8.0.1763-15.el8         appstream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-15.el8         appstream       48 k

Transaction Summary
================================================================================
Install  4 Packages

Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/4): gpm-libs-1.20.7-17.el8.x86_64.rpm        492 kB/s |  39 kB     00:00    
(2/4): vim-filesystem-8.0.1763-15.el8.noarch.rp 1.9 MB/s |  48 kB     00:00    
(3/4): vim-enhanced-8.0.1763-15.el8.x86_64.rpm  2.2 MB/s | 1.4 MB     00:00    
(4/4): vim-common-8.0.1763-15.el8.x86_64.rpm    3.9 MB/s | 6.3 MB     00:01    
--------------------------------------------------------------------------------
Total                                           2.6 MB/s | 7.8 MB     00:03     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : vim-filesystem-2:8.0.1763-15.el8.noarch                1/4 
  Installing       : vim-common-2:8.0.1763-15.el8.x86_64                    2/4 
  Installing       : gpm-libs-1.20.7-17.el8.x86_64                          3/4 
  Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          3/4 
  Installing       : vim-enhanced-2:8.0.1763-15.el8.x86_64                  4/4 
  Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64                  4/4 
  Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64                    4/4 
  Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          1/4 
  Verifying        : vim-common-2:8.0.1763-15.el8.x86_64                    2/4 
  Verifying        : vim-enhanced-2:8.0.1763-15.el8.x86_64                  3/4 
  Verifying        : vim-filesystem-2:8.0.1763-15.el8.noarch                4/4 

Installed:
  gpm-libs-1.20.7-17.el8.x86_64         vim-common-2:8.0.1763-15.el8.x86_64    
  vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch

Complete!


//在ansible主机上使用yum模块在受控机上安装vim

//查看受控机上是否安装了vim
[root@control ansible]# ansible server2  -m shell  -a 'yum  provides  vim'
192.168.56.131 | CHANGED | rc=0 >>
Last metadata expiration check: 1:55:35 ago on Sun Jul 18 17:21:53 2021.
vim-enhanced-2:8.0.1763-15.el8.x86_64 : A version of the VIM editor which includes recent enhancements
Repo        : @System
Matched from:
Provide    : vim = 8.0.1763-15.el8

vim-enhanced-2:8.0.1763-15.el8.x86_64 : A version of the VIM editor which includes recent enhancements
Repo        : appstream
Matched from:
Provide    : vim = 8.0.1763-15.el8

9. ansible常用模块copy

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

[root@control ansible]# ls
1  ansible.cfg  hosts  ta

[root@ansible ~]#
[root@control ansible]#  ansible server1 -m copy -a 'src=/etc/ansible/ta dest=/opt'192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "549c112194cc148a33d8bdbd02c3e592f4a2d6d7",
    "dest": "/opt/ta",
    "gid": 0,
    "group": "root",
    "md5sum": "b9a03506c5a737fd66537411a6c19675",
    "mode": "0644",
    "owner": "root",
    "size": 51,
    "src": "/root/.ansible/tmp/ansible-tmp-1626618667.026803-937245-118071835788916/source",
    "state": "file",
    "uid": 0
}

[root@control ansible]#  ansible server1 -m shell -a 'ls /opt'
192.168.56.130 | CHANGED | rc=0 >>
hhh
ta
wy

10. ansible常用模块group

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

//在受控机上添加一个系统组,其gid为1100,组名为mysql
[root@control ansible]# ansible 192.168.56.130 -m group -a 'name=mysql gid=1100 state=present'
192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 1100,
    "name": "mysql",
    "state": "present",
    "system": false
}

[root@control ansible]# ansible 192.168.56.130 -m shell -a 'grep mysql /etc/group'
192.168.56.130 | CHANGED | rc=0 >>
mysql:x:1100:



//删除受控机上的mysql组
[root@control ansible]# ansible 192.168.56.130 -m group -a 'name=mysql state=absent'
192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "mysql",
    "state": "absent"
}

[root@control ansible]# ansible 192.168.56.130 -m shell -a 'grep mysql /etc/group'
192.168.56.130 | FAILED | rc=1 >>
non-zero return code

11. ansible常用模块user

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

//在受控机上添加一个系统用户,用户名为yy,uid为1121,设置其shell为/sbin/nologin,无家目录
[root@control ansible]# ansible server1 -m user -a 'name=yy uid=1121 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 990,
    "home": "/home/yy",
    "name": "yy",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 1121
}

[root@control ansible]# ansible 192.168.56.130 -m shell -a 'grep yy /etc/passwd'
192.168.56.130 | CHANGED | rc=0 >>
yy:x:1121:990::/home/yy:/sbin/nologin


[root@control ansible]# ansible server1 -m shell -a 'ls /home'
192.168.56.130 | CHANGED | rc=0 >>
tom
yst


//修改mysql用户的uid为1300
[root@control ansible]# ansible 192.168.56.130 -m user -a 'name=yy uid=1300'
192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "append": false,
    "changed": true,
    "comment": "",
    "group": 990,
    "home": "/home/yy",
    "move_home": false,
    "name": "yy",
    "shell": "/sbin/nologin",
    "state": "present",
    "uid": 1300
}

[root@control ansible]# ansible 192.168.56.130 -m shell -a 'grep yy /etc/passwd'
192.168.56.130 | CHANGED | rc=0 >>
yy:x:1300:990::/home/yy:/sbin/nologin


//删除受控机上的mysql用户
[root@control ansible]# ansible 192.168.56.130 -m user -a 'name=yy state=absent'
192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "yy",
    "remove": false,
    "state": "absent"
}

[root@control ansible]# ansible 192.168.56.130 -m shell -a 'grep yy /etc/passwd'
192.168.56.130 | FAILED | rc=1 >>
non-zero return code

12. ansible常用模块service

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

//查看受控机上的vsftpd服务是否启动
[root@control ansible]# ansible 192.168.56.130 -m shell -a 'systemctl is-active vsftpd'
192.168.56.130 | FAILED | rc=3 >>
inactivenon-zero return code


//启动受控机上的vsftpd服务
[root@control ansible]# ansible 192.168.56.130 -m service -a 'name=vsftpd state=started'
192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "vsftpd",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "inactive",
        "After": "basic.target system.slice network
        ....此处省略N行
}

//查看受控机上的vsftpd服务是否启动
[root@ansible ~]# ansible 192.168.56.130 -m shell -a 'systemctl is-active vsftpd'
192.168.56.130 | SUCCESS | rc=0 >>
active


//查看受控机上的vsftpd服务是否开机自动启动
[root@control ansible]#  ansible 192.168.56.130 -m shell -a 'systemctl is-active vsftpd'
192.168.56.130 | CHANGED | rc=0 >>
active


//设置受控机上的vsftpd服务开机自动启动
[root@control ansible]# ansible 192.168.56.130 -m service -a 'name=vsftpd enabled=yes'
192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "vsftpd",
    "status": {
        "ActiveEnterTimestamp": "Sun 2021-07-18 22:43:09 CST",
        "ActiveEnterTimestampMonotonic": "37807023023",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "active",

        ....此处省略N行
}

//查看受控机上的vsftpd服务是否开机自动启动
[root@control ansible]# ansible 192.168.56.130 -m shell -a 'systemctl is-enabled vsftpd'
192.168.56.130 | CHANGED | rc=0 >>
enabled



//停止受控机上的vsftpd服务
[root@control ansible]# ansible 192.168.56.130 -m service -a 'name=vsftpd state=stopped'
192.168.56.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "vsftpd",
    "state": "stopped",
    "status": {
        "ActiveEnterTimestamp": "Sun 2021-07-18 22:43:09 CST",

        ....此处省略N行
}

[root@control ansible]# ansible 192.168.56.130 -m shell -a 'systemctl is-active vsftpd'
192.168.56.130 | FAILED | rc=3 >>
inactivenon-zero return code


[root@control ansible]# ansible 192.168.56.130 -m shell -a 'ss -antl'
192.168.56.130 | CHANGED | rc=0 >>
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*       
LISTEN    0         128                      *:80                     *:*       
LISTEN    0         128                   [::]:22                  [::]:* 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值