ansible的常用模块

ansible常用模块

一、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模块了

1.ansible常用模块之ping

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

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

2.ansible常用模块之command

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

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

//查看受控主机的/tmp目录内容
[root@ansible ~]# ansible 192.168.111.142 -a 'ls /tmp'
192.168.111.142 | CHANGED | rc=0 >>
ansible_command_payload_z973ukjx
systemd-private-ac56c8c9ec59454cbb39a024c91beb72-chronyd.service-S24EYh
vmware-root_897-3979643105

//在受控主机的/tmp目录下新建一个文件test
[root@ansible ~]# ansible 192.168.111.142 -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.111.142 | CHANGED | rc=0 >>

//查看一下是否创建成功
[root@ansible ~]# ansible 192.168.111.142 -a 'ls /tmp'
192.168.111.142 | CHANGED | rc=0 >>
ansible_command_payload_62jkggxf
systemd-private-ac56c8c9ec59454cbb39a024c91beb72-chronyd.service-S24EYh
test
vmware-root_897-3979643105

//command模块不支持管道符,不支持重定向
[root@ansible ~]# ansible 192.168.111.142 -a "echo 'hello world' > /tmp/test"
192.168.111.142 | CHANGED | rc=0 >>
hello world > /tmp/test

[root@ansible ~]# ansible 192.168.111.142 -a 'cat /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>

[root@ansible ~]# ansible 192.168.111.142 -a 'ps -ef | grep httpd'
192.168.111.142 | FAILED | rc=1 >>
error: garbage 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

3.ansible常用模块之raw

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

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

[root@ansible ~]# ansible 192.168.111.142 -a 'cat /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>
hello world

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

4.ansible常用模块之shell

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

//查看受控机上的脚本
[root@localhost ~]# ll /scripts/
total 4
-rw-r--r-- 1 root root 43 Oct 23 19:00 test.sh

//使用shell模块在受控机上执行受控机上的脚本
[root@ansible ~]# ansible 192.168.111.142 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>

[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'cat /tmp/test'
192.168.111.142 | CHANGED | rc=0 >>
123456
123
456

5.ansible常用模块之script

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

[root@ansible ~]# ll /scripts/
total 4
-rw-r--r-- 1 root root 48 Oct 23 19:06 a.sh

[root@ansible ~]# ansible 192.168.111.142 -m script -a '/scripts/a.sh &> /tmp/a'
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": "",
    "stdout_lines": []
}

//查看受控机上的/tmp/a文件内容
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'cat /tmp/a'
192.168.111.142 | CHANGED | rc=0 >>
123
456
hello world

6.ansible常用模块之template

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

[root@ansible ~]# ansible 192.168.111.142 -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/centos.repo'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
    "dest": "/etc/yum.repos.d/centos.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "ed031c350da2532e6a8d09a4d9b05278",
    "mode": "0644",
    "owner": "root",
    "size": 1653,
    "src": "/root/.ansible/tmp/ansible-tmp-1666530145.7581205-29420421110479/source",
    "state": "file",
    "uid": 0
}

//查看受控机上是否有163源
[root@localhost ~]# ls /etc/yum.repos.d/
centos.repo

7.ansible常用模块之yum

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

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

state常用的值:

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

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

//在受控机上查询看vsftpd软件是否安装
[root@localhost ~]# rpm -qa|grep httpd
[root@localhost ~]# 

//在ansible主机上使用yum模块在受控机上安装httpd
[root@ansible ~]# ansible 192.168.111.142 -m yum -a 'name=httpd state=present'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: mailcap-2.1.48-3.el8.noarch",
        "Installed: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
        "Installed: apr-1.6.3-12.el8.x86_64",
        "Installed: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64",
        "Installed: centos-logos-httpd-85.8-2.el8.noarch",
        "Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64"
    ]
}

//查看受控机上是否安装了httpd
[root@localhost ~]# rpm -qa|grep httpd
httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
centos-logos-httpd-85.8-2.el8.noarch
httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64

8.ansible常用模块之copy

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

[root@ansible ~]# ls /scripts/
a.sh
[root@ansible ~]# ansible 192.168.111.142 -m copy -a 'src=/scripts/a.sh dest=/scripts/a'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "91bb6c7a9d3e47e330dd408c5209d8c36226e014",
    "dest": "/scripts/a/a.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "d92a6bbb430fd85b35bfc1f0a8035bdc",
    "mode": "0644",
    "owner": "root",
    "size": 48,
    "src": "/root/.ansible/tmp/ansible-tmp-1666530391.1743026-219581542309073/source",
    "state": "file",
    "uid": 0
}

[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'ls /scripts/a'
192.168.111.142 | CHANGED | rc=0 >>
a.sh

9.ansible常用模块之group

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

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

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

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

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

10.ansible常用模块之user

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

//在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@ansible ~]# ansible 192.168.111.142 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.111.142 | 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 192.168.111.142 -m shell -a 'grep mysql /etc/passwd'
192.168.111.142 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin

[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'ls /home'
192.168.111.142 | CHANGED | rc=0 >>

//修改mysql用户的uid为366
[root@ansible ~]# ansible 192.168.111.142 -m user -a 'name=mysql uid=366'
192.168.111.142 | 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 192.168.111.142 -m shell -a 'grep mysql /etc/passwd'
192.168.111.142 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin

//删除受控机上的mysql用户
[root@ansible ~]# ansible 192.168.111.142 -m user -a 'name=mysql state=absent'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "mysql",
    "remove": false,
    "state": "absent"
}
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'grep mysql /etc/passwd'
192.168.111.142 | FAILED | rc=1 >>
non-zero return code

11.ansible常用模块之service

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

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

//启动受控机上的httpd服务
[root@ansible ~]# ansible 192.168.111.142 -m service -a 'name=httpd state=started'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "httpd",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "inactive",
        ...............
}

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

//查看受控机上的httpd服务是否开机自动启动
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'systemctl is-enabled httpd'
192.168.111.142 | FAILED | rc=1 >>
disablednon-zero return code

//设置受控机上的httpd服务开机自动启动
[root@ansible ~]# ansible 192.168.111.142 -m service -a 'name=httpd enabled=yes'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
..................
}

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


//停止受控机上的httpd服务
[root@ansible ~]# ansible 192.168.111.142 -m service -a 'name=httpd state=stopped'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "httpd",
    "state": "stopped",
    "status": {
        "ActiveEnterTimestamp": "Sun 2022-10-23 21:17:04 CST",
..............
}

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

[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'ss -anlt'
192.168.111.142 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*     

12.ansible常用模块之file

file 模块可以帮助我们完成一些对文件的基本操作。比如,创建文件或目录、删除文件或目录、修改文件权限等

force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软
链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group:定义文件/目录的属组
mode:定义文件/目录的权限
owner:定义文件/目录的属主
path:必选项,定义文件/目录的路径
recurse:递归的设置文件的属性,只对目录有效
src:要被链接的源文件的路径,只应用于state=link的情况
dest:被链接到的路径,只应用于state=link的情况
state:
=directory:如果目录不存在,创建目录
=file:即使文件不存在,也不会被创建
=link:创建软链接
=hard:创建硬链接
=touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
=absent:删除目录、文件或者取消链接文件
//创建文件
[root@ansible ~]# ansible 192.168.111.142 -m file -a 'path=/tmp/abc state=touch owner=root group=root'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/tmp/abc",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
}

//创建目录
[root@ansible ~]# ansible 192.168.111.142 -m file -a 'path=/tmp/CBA state=directory mode=0755'
192.168.111.142 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/tmp/CBA",
    "size": 6,
    "state": "directory",
    "uid": 0
}

//查看
[root@ansible ~]# ansible 192.168.111.142 -m shell -a 'ls -l /tmp/'
192.168.111.142 | CHANGED | rc=0 >>
total 0
drwxr-xr-x  2 root root  6 Oct 23 21:25 CBA
-rw-r--r--  1 root root  0 Oct 23 21:24 abc
drwx------  2 root root 41 Oct 23 21:26 ansible_command_payload_8kzca8ed
drwx------  3 root root 17 Oct 23 21:02 systemd-private-bf7fe2a05a0f43fdab39626e122a4453-chronyd.service-tvBXFi
drwx------  2 root root  6 Oct 23 21:02 vmware-root_894-2730693566
drwx------. 2 root root  6 Oct 23 21:00 vmware-root_903-3979774182
posted @ 2022-10-24 00:05  事愿人为  阅读(529)  评论(0编辑  收藏  举报