ansible常用模块详解

一、Ansible

1.1 简介

Ansible是自动化运维工具,能实现跨主机对应用编排管理部署。

Ansible能批量配置、部署、管理上千台主机,是应用级别的跨主机编排工具。

比如以前需要切换到每个主机上执行的一或多个操作,使用Ansible只需在固定的一台Ansible控制节点上去完成所有主机的操作。

1.2 工作原理

基于模块工作,通过模块实现在被控制节点上执行相应的命令操作。

1.3 Ansible的特性

1.3.1 特性一:Agentless,即无Agent的存在

1)无客户端agent存在,不需要在被控制的节点上安装额外的客户端应用;

2)通过ssh协议与被控制节点通信。

1.3.2 特性二:幂等性

所谓幂等性,指的是无论执行多少次同样的运算,结果都是相同的,即一条命令,任意多次执行所产生的影响均与一次执行的影响相同。

Ansible的很多模块具有幂等性,如果多次模块操作的状态没有发生改变,则不会重复执行。

1.4 Ansible的基本组件

  • INVENTORY:Ansible管理主机的清单 /etc/anaible/hosts 需要管理的服务清单

  • MODULES:Ansible执行命令的功能模块,多数为内置核心模块,也可自定义

  • PLUGINS:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等,该功能不常用

  • API:供第三方程序调用的应用程序编程接口

二、Ansible环境安装部署

角色IP安装工具
管理端192.168.10.10ansible
被管理端1192.168.10.20无需安装
被管理端2192.168.10.30无需安装

2.1 安装ansible

在管理端安装 ansible

#先安装 epel 源
yum install -y epel-release 
​
#yum安装ansible
yum install -y ansible

2.2 配置远程主机清单

vim /etc/ansible/hosts
​
     
[web]           #配置组名
192.168.10.20           #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)
192.168.10.30
#配置密钥对验证
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
​
yum install -y sshpass
sshpass -p '123123' ssh-copy-id -o StrictHostKeyChecking=no root@192.168.10.20
sshpass -p '123123' ssh-copy-id -o StrictHostKeyChecking=no root@192.168.10.30  
或
[root@localhost yum.repos.d]# ssh-keygen #生成密钥
​
[root@localhost .ssh]# ssh-copy-id  -i id_rsa.pub 192.168.10.20
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.10.20's password: 
​
Number of key(s) added: 1
​
Now try logging into the machine, with:   "ssh '192.168.10.20'"
and check to make sure that only the key(s) you wanted were added.
​
[root@localhost .ssh]# ssh-copy-id  -i id_rsa.pub 192.168.10.30
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.10.30's password: 
​
Number of key(s) added: 1
​
Now try logging into the machine, with:   "ssh '192.168.10.30'"
and check to make sure that only the key(s) you wanted were added.
​
​
​
​

三、Ansible的模块(重点)

3.1 ansible的命令格式

#ansible命令格式
ansible 组名 -m 模块名 -a '参数'
​
#-a 用于向模块传递参数
#查看当前系统中的ansible模块
ansible-doc -l
​
#查看特定模块的摘要信息
ansible-doc -s <module_name>

3.2 Command模块

功能:在远程主机执行命令,此为默认模块,可忽略 -m 选项。

注意:此命令不支持 $VARNAME < > | ; & 等,即不支持管道符、重定向符号。

注意:此模块不具有幂等性

3.2.1 基本格式与例子
#基本格式
ansible <组名/IP> [-m command] -a '[参数] 命令'
​
[root@localhost .ssh]# ansible web -a 'hostname'
192.168.10.20 | CHANGED | rc=0 >>
localhost.localdomain
192.168.10.30 | CHANGED | rc=0 >>
localhost.localdomain
[root@localhost .ssh]# ansible web -a "touch /opt/ky15.txt"
#此处的  警告是  让你用  file  模块 专业的模块
[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.10.30 | CHANGED | rc=0 >>
​
192.168.10.20 | CHANGED | rc=0 >>
​
[root@localhost .ssh]# ansible web -a "ls /opt/ky15.txt"
192.168.10.30 | CHANGED | rc=0 >>
/opt/ky15.txt
192.168.10.20 | CHANGED | rc=0 >>
/opt/ky15.txt
[root@localhost .ssh]# ansible web -a "echo hello > /opt/hello.log"
192.168.10.20 | CHANGED | rc=0 >>
hello > /opt/hello.log
192.168.10.30 | CHANGED | rc=0 >>
hello > /opt/hello.log
​

3.3 shell模块

功能:和command模块类似,在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令。

注意:此模块不具有幂等性

注意:此模块支持管道符号等功能

3.3.1 基本格式与例子
ansible <组/IP/all> -m shell -a ' ' 
​
[root@localhost .ssh]# ansible web -m shell -a "ifconfig |awk 'NR==2 {print \$2}'"
#shell模块支持管道符
192.168.10.20 | CHANGED | rc=0 >>
192.168.10.20
192.168.10.30 | CHANGED | rc=0 >>
192.168.10.30
[root@localhost .ssh]# ansible web  -a "ifconfig |awk 'NR==2 {print \$2}'"
#默认的command模块无法使用管道符
192.168.10.20 | FAILED | rc=1 >>
NR==2 {print $2}: 未知的主机
ifconfig: `--help' gives usage information.non-zero return code
^C [ERROR]: User interrupted execution
​

3.4 cron模块

功能:在远程主机定义crontab任务计划。

3.4.1 基本格式与例子
#基本格式
ansible <组/IP/all> -m cron -a ' '
​
[root@localhost .ssh]# ansible web -m cron -a 'minute=30 hour="8,20"  weekday="1-5"  job="/usr/bin/cp  -f /var/log/message /opt" name="cxk"'
#周一到周五早八点半和晚八点半 执行 复制/var/log/messages 到 /opt
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "cxk"
    ]
}
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "cxk"
    ]
}
#查看web组
​
[root@localhost opt]# crontab -l
#Ansible: cxk
30 8,20 * * 1-5 /usr/bin/cp  -f /var/log/message /opt
​

如何删除

[root@localhost .ssh]# ansible web -m cron -a 'name="cxk" state=absent'
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}
#切换到web组
[root@localhost opt]# crontab -l
无任务

3.5 user模块

功能:在远程主机管理用户账户

3.5.1 基本格式与例子
ansible <组/IP/all> -m user -a ' '
​
ansible web -m user -a 'name="cxk"'
ansible web -m command -a 'tail -n1 /etc/passwd'
​
[root@localhost .ssh]# ansible web -m command -a 'tail -n1 /etc/passwd'
192.168.10.20 | CHANGED | rc=0 >>
cxk:x:1001:1001::/home/cxk:/bin/bash
192.168.10.30 | CHANGED | rc=0 >>
cxk:x:1002:1002::/home/cxk:/bin/bash
​
​

3.6 group模块

功能:在远程主机进行用户组管理的模块

3.6.1 基本格式与例子
ansible <组/IP/all> -m group -a ' '
​

name:用户名,必选参数

state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除

system=yes|no:是否为系统账号

gid:组id

[root@localhost .ssh]# ansible web -m group -a 'name=wyf gid=110 system=yes'
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 110, 
    "name": "wyf", 
    "state": "present", 
    "system": true
}
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 110, 
    "name": "wyf", 
    "state": "present", 
    "system": true
}
[root@localhost .ssh]# ansible web -m group -a 'name=wyf gid=110 system=yes state=absent'
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "wyf", 
    "state": "absent"
}
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "wyf", 
    "state": "absent"
}
​

3.7 copy模块 (重点)

功能:从ansible服务器主控端复制文件到远程主机

注意:src=file 如果是没指明路径,则为当前目录或当前目录下的files目录下的file文件

3.7.1 基本格式和常用参数
#基本格式
ansible < > -m copy -a 'src=   dest=   [owner= ] [mode=]   '
​
常用参数功能注意事项
src指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录如果源是目录则目标也要是目录
dest指出复制文件的目标及位置,使用绝对路径如果源是目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容
mode指出复制时,目标文件的权限
owner指出复制时,目标文件的属主
group指出复制时,目标文件的属组
content指出复制到目标主机上的内容不能与src一起使用
[root@localhost .ssh]# ansible web -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'
​
[root@localhost .ssh]# ansible web -a 'ls -l /opt'
192.168.10.20 | CHANGED | rc=0 >>
总用量 8
-rw-r--r--. 1 root root   6 6月  24 14:14 cxk.txt
-rw-r-----. 1 root root 541 6月  24 15:37 fstab.bak
-rw-r--r--. 1 root root   0 6月  24 15:03 ky15.txt
drwxr-xr-x. 2 root root   6 3月  26 2015 rh
192.168.10.30 | CHANGED | rc=0 >>
总用量 8
-rw-r--r--. 1 root root   6 6月  24 14:14 cxk.txt
-rw-r-----. 1 root root 541 6月  24 15:37 fstab.bak
-rw-r--r--. 1 root root   0 6月  24 15:03 ky15.txt
drwxr-xr-x. 2 root root   6 3月  26 2015 rh
[root@localhost .ssh]# ansible web -a 'cat /opt/fstab.bak'
192.168.10.20 | CHANGED | rc=0 >>
​
#
# /etc/fstab
# Created by anaconda on Thu Apr 25 18:05:01 2024
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=6a6b9aa4-d7dd-4a07-a45e-a59fd506806d /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
192.168.10.30 | CHANGED | rc=0 >>
​
#
# /etc/fstab
# Created by anaconda on Thu Apr 25 18:05:01 2024
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=6a6b9aa4-d7dd-4a07-a45e-a59fd506806d /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
​
[root@localhost .ssh]# ansible web -m copy -a 'content="nihao" dest=/opt/hello.txt'
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "23fcf96d70494b81c5084c0da6a6e8d84a9c5d20", 
    "dest": "/opt/hello.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "194ce5d0b89c47ff6b30bfb491f9dc26", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 5, 
    "src": "/root/.ansible/tmp/ansible-tmp-1719214807.0-38084-170640363511566/source", 
    "state": "file", 
    "uid": 0
}
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "23fcf96d70494b81c5084c0da6a6e8d84a9c5d20", 
    "dest": "/opt/hello.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "194ce5d0b89c47ff6b30bfb491f9dc26", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 5, 
    "src": "/root/.ansible/tmp/ansible-tmp-1719214807.0-38086-99980657837519/source", 
    "state": "file", 
    "uid": 0
}
[root@localhost .ssh]# ansible web -a 'cat /opt/hello.txt'
192.168.10.30 | CHANGED | rc=0 >>
nihao
192.168.10.20 | CHANGED | rc=0 >>
nihao
​

3.8 file模块

功能:在远程主机管理文件属性、创建软链接等

3.8.1 常用参数
#基本格式
ansible < > -m file -a ''
​
常用参数功能
path指定远程服务器的路径,也可以写成"dest",“name”
state状态,可以将值设定为directory表示创建目录,设定为touch表示创建文件,设定为link表示创建软链接,设定为hard表示创建硬连接,设定为absent表示删除目录文件或链接
mode文件复制到远程并设定权限,默认file=644,directory=755
owner文件复制到远程并设定属主,默认为root
group文件复制到远程并设定属组,默认为root
recurese递归修改
src指的是目标主机上的源文件。与copy模块不同。
[root@localhost ~]# ansible web -m file -a 'owner=cxk group=root mode=644 path=/opt/fstab.bak'
#修改文件的属主属组权限
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "cxk", 
    "path": "/opt/fstab.bak", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 541, 
    "state": "file", 
    "uid": 1001
}
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "cxk", 
    "path": "/opt/fstab.bak", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 541, 
    "state": "file", 
    "uid": 1002
}
​
[root@localhost ~]# ansible web -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'
#软连接 state=link
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/fstab.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 14, 
    "src": "/opt/fstab.bak", 
    "state": "link", 
    "uid": 0
}
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/fstab.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 14, 
    "src": "/opt/fstab.bak", 
    "state": "link", 
    "uid": 0
}
[root@localhost ~]# ansible web -a 'ls -l /opt'
192.168.10.30 | CHANGED | rc=0 >>
总用量 12
-rw-r--r--. 1 root root   6 6月  24 14:14 cxk.txt
-rw-r--r--. 1 cxk  root 541 6月  24 15:37 fstab.bak
lrwxrwxrwx. 1 root root  14 6月  25 20:50 fstab.link -> /opt/fstab.bak
-rw-r--r--. 1 root root   5 6月  24 15:40 hello.txt
-rw-r--r--. 1 root root   0 6月  24 15:03 ky15.txt
drwxr-xr-x. 2 root root   6 3月  26 2015 rh
192.168.10.20 | CHANGED | rc=0 >>
总用量 16
-rw-r--r--.  1 root root     6 6月  24 14:14 cxk.txt
-rw-r--r--.  1 cxk  root   541 6月  24 15:37 fstab.bak
lrwxrwxrwx.  1 root root    14 6月  25 20:50 fstab.link -> /opt/fstab.bak
-rw-r--r--.  1 root root     5 6月  24 15:40 hello.txt
-rw-r--r--.  1 root root     0 6月  24 15:03 ky15.txt
drwxr-xr-x. 38 7161 31415 4096 6月  25 15:46 mysql-5.7.20
drwxr-xr-x.  2 root root     6 3月  26 2015 rh
​
#创建一个空文件,state=touch
ansible web -m file -a "path=/opt/abc.txt state=touch"
​
#创建一个空目录,state=directory
ansible web -m file -a "path=/data state=directory"
​
#删除一个文件,state=absent
ansible web -m file -a "path=/opt/abc.txt state=absent" 
​
ansible web -a 'removes=/opt/abc.txt ls ./'
​

3.9 hostname模块

功能:用于管理远程主机上的主机名

#修改主机名
ansible web -m hostname -a "name=mysql01"
​

3.10 ping模块

功能:测试远程主机的连通性。

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

3.11 yum/apt 模块

功能:在远程主机上安装与卸载软件包

常用参数功能
name需要安装的服务名
state=present(缺省值)/absent状态,abasent表示卸载服务
ansible web -m yum -a 'name=httpd'                  #安装服务
#卸载服务
ansible web -m yum -a 'name=httpd state=absent'     
​
​

3.12 service/systemd 模块

功能:用于管理远程主机上的管理服务的运行状态。

常用参数功能
name指定需要控制的服务名称
state指定服务状态,其值可以为stopped、started、reloaded、restarted、status
enabled指定服务是否为开机启动,yes为启动,no为不启动
daemon_reloadyes:重启systemd服务,让unit文件生效
#先安装服务
ansible web -m yum -a 'name=httpd'
​
#启动httpd服务
ansible web -m service -a 'enabled=true name=httpd state=started'
#查看web服务器httpd运行状态
ansible web -a 'systemctl status httpd'         
​

3.13 script 模块

功能:在远程主机执行shell脚本。

注意:script模块不具有幂等性,所以建议用剧本来执行。

 #在本地写一个脚本
 vim test.sh
 #!/bin/bash
 echo "hello ansible from script" > /opt/test2.txt、
​
 chmod +x test.sh                              #给脚本执行权限
​
 ansible web -m script -a '/opt/test.sh'      #远程运行本地脚本
 ansible web -a 'cat /opt/test2.txt'   #查看生成的文件内容
​

3.14 mount 模块

功能:在远程主机挂载目录/设备文件

常用参数功能
src指定要挂载的设备或分区路径。
path指定要挂载到的目标路径。
fstype指定要挂载的文件系统类型。
state指定挂载状态,可选值为 mountedunmountedabsent
opts指定挂载选项,例如挂载选项或参数。
ansible web -m mount -a 'src=/dev/sr0 path=/mnt state=mounted fstype=iso9660'
#使用 Ansible 的 mount 模块将设备 /dev/sr0 的内容挂载到目标路径 /mnt。
#文件系统类型为 iso9660,并将该设备标记为已挂载状态
​

3.15 archive 模块

功能:在远程主机压缩文件。

常用参数功能
path指定要打包的源目录或文件的路径。
dest指定打包文件的输出路径。
format指定打包文件的格式,可以是 ziptargzbzip2。默认为 tar格式。
remove指定是否在打包文件之后,删除源目录或文件。可选值为 yesno。默认为 no,即不删除源目录或文件。
ansible web -m archive -a "path=/etc/yum.repos.d/ dest=/opt/repo.zip format=zip"
#在web主机上使用archive模块创建一个名为repo.zip的ZIP格式压缩文件
源文件路径为/etc/yum.repos.d/
输出路径为/opt/repo.zip
​
#remove参数的使用,压缩后删除源文件
ansible web -m archive -a "path=/opt/test2.txt,/opt/123.txt dest=/opt/abc123.tar.gz format=gz remove=yes"
​

3.16 unarchive 模块

功能:将本地或远程主机的压缩包在远程主机解压缩 。

常用参数功能
copy指定是否将打包文件复制到远程节点以进行解压缩。
remote_src(已弃用)改用 copy 参数。
src指定要解压缩的打包文件路径,可以是本地路径或远程路径。
dest指定要将文件解压缩到的目标目录。
creates指定一个文件路径,如果该文件已经存在,则不进行解压缩操作。
remote_tmp用于制定远程节点上的临时目录。默认为 /tmp
#copy参数
copy参数的可选值为 `yes` 或 `no`。
默认为 `yes`,即先将文件从控制节点复制到远程节点,然后在远程节点上进行解压缩。
如果已经将文件分发到了目标节点并想要提高效率,可以将该值设置为 `no`。
反效果的参数为 `remote_src`。
​
#现在ansible主机建立压缩包
tar cf test.tar.gz test.sh 
​
#将 ansible 主机的压缩文件拷贝到到远程主机并解压,修改文件所属组和用户
ansible web -m unarchive -a "src=/opt/test.tar.gz dest=/root copy=yes"
​

3.17 replace 模块

功能:在远程主机修改文件内容 。

类似于sed命令,主要也是基于正则进行匹配和替换。

常用参数功能
path指定需要处理的文件路径
regexp用于匹配需要替换内容的正则表达式
replace用于替换匹配内容的字符串
after在哪个字符串之后进行替换,默认为空
before在哪个字符串之前进行替换,默认为空
backup是否备份文件,选项为 yes 或 no
#在服务器的主机下创建测试文件
vim /opt/test.txt
11 22 33 44 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
#匹配 33 并修改为 ccc
ansible web -m replace -a "path=/opt/test.txt regexp='33' replace='cc'"
[root@localhost ~]# ansible web -m replace -a "path=/opt/test.txt regexp='33' replace='cc'"
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "1 replacements made"
}
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "1 replacements made"
}
​
​
#查看
ansible web -a "cat /opt/test.txt"
[root@localhost ~]# ansible web -a "cat /opt/test.txt"
192.168.10.30 | CHANGED | rc=0 >>
11 22 cc 44 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
192.168.10.20 | CHANGED | rc=0 >>
11 22 cc 44 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
​
​
#匹配到任意一个或多个开头的行增加注释
ansible web -m replace -a "path=/opt/test.txt regexp='^(.*)' replace='#\1'"
[root@localhost ~]# ansible web -m replace -a "path=/opt/test.txt regexp='^(.*)' replace='#\1'"
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "4 replacements made"
}
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "4 replacements made"
}
[root@localhost ~]# ansible web -a "cat /opt/test.txt"
192.168.10.20 | CHANGED | rc=0 >>
#11 22 cc 44 55 66
#aa bb cc dd ee ff
#1a 2b 3c 4d 5e 6f
#
192.168.10.30 | CHANGED | rc=0 >>
#11 22 cc 44 55 66
#aa bb cc dd ee ff
#1a 2b 3c 4d 5e 6f
#
​
#取消注释
ansible web -m replace -a "path=/opt/test.txt regexp='^#(.*)' replace='\1'"
[root@localhost ~]# ansible web -m replace -a "path=/opt/test.txt regexp='^#(.*)' replace='\1'"
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "4 replacements made"
}
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "4 replacements made"
}
[root@localhost ~]# ansible web -a "cat /opt/test.txt"
192.168.10.30 | CHANGED | rc=0 >>
11 22 cc 44 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
192.168.10.20 | CHANGED | rc=0 >>
11 22 cc 44 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
​
#匹配以 a 开头的后面有一个或者多个字符的行,并在前面添加 # 注释
ansible web -m replace -a "path=/opt/test.txt regexp='^(a.*)' replace='#\1'"
[root@localhost ~]# ansible web -m replace -a "path=/opt/test.txt regexp='^(a.*)' replace='#\1'"
192.168.10.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "1 replacements made"
}
192.168.10.30 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "1 replacements made"
}
[root@localhost ~]# ansible web -a "cat /opt/test.txt"
192.168.10.20 | CHANGED | rc=0 >>
11 22 cc 44 55 66
#aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
192.168.10.30 | CHANGED | rc=0 >>
11 22 cc 44 55 66
#aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
​
​
​
​

3.18 setup 模块

功能:使用facts组件获取远程主机的系统信息(facts信息)

常用参数功能
filter指定需要过滤的条件,仅返回满足条件的主机信息,默认为空
ansible web -m setup                #获取mysql组主机的facts信息
​
ansible web -m setup -a 'filter=*ipv4'    #使用filter可以筛选指定的facts信息
[root@localhost ~]# ansible web -m setup -a 'filter=*ipv4'
192.168.10.20 | SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "192.168.10.20", 
            "alias": "ens33", 
            "broadcast": "192.168.10.255", 
            "gateway": "192.168.10.2", 
            "interface": "ens33", 
            "macaddress": "00:0c:29:e7:d4:80", 
            "mtu": 1500, 
            "netmask": "255.255.255.0", 
            "network": "192.168.10.0", 
            "type": "ether"
        }, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
​
​
facts信息

主机的各种信息,包括硬件、操作系统、网络等。

运行命令后,会返回一个包含主机 facts 信息的 JSON 格式输出。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值