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 格式输出。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/751418.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

练习实践:ubuntu18.04安装、配置Nginx+PHP环境,两种配置方式,多站点

参考来源&#xff1a; https://help.aliyun.com/document_detail/464753.html https://www.cnblogs.com/laosan007/p/12803287.html https://blog.csdn.net/qq_55364077/article/details/132207083 【安装同版本7.2的php】 需要知道对应php和nginx的安装版本 需要安装php-fpm…

stl之string

构造函数 void test1() {string s1;//不传参cout << s1 << endl;string s2("123456");cout << s2 << endl;string s3(s2);cout << s3 << endl;string s4(s2, 1, 5);cout << s4 << endl;string s5("123456&quo…

PHP 网络通信底层原理分析

大家好&#xff0c;我是码农先森。 引言 我们日常的程序开发大多数都是以业务为主&#xff0c;很少会接触到底层逻辑。对于我们程序员来说&#xff0c;了解程序的底层运行逻辑&#xff0c;更有助于提升我们对程序的理解。我相信大多数的人&#xff0c;每天基本上都是完成业务…

丝杆支撑座:滚珠丝杆稳定运行的守护者!

丝杆支撑座是丝杆和电机之间连接的重要组成部分&#xff0c;发挥着非常重要的功能。提到丝杆支撑座和滚珠丝杆&#xff0c;很多人都会想到支撑关系&#xff0c;但丝杆支撑座作为滚珠丝杆系统中至关重要的角色&#xff0c;其作用远不止于简单的支撑。 丝杆支撑座安装过程非常简单…

第30课 绘制原理图——放置网络标签

什么是网络标签&#xff1f; 我们在很多电路图中都能看到&#xff0c;为了让图纸更加简洁&#xff0c;并不是每一根导线都要确确实实地画出来。可以在导线悬空的一端添加一个名称标签&#xff0c;接着在另一根导线的悬空一端添加上一个同名的名称标签&#xff0c;那么就可以让…

【自监督-MIM】系列方法学习二

Masked image modeling 是一种训练深度学习模型的技术,尤其是在视觉领域,类似于自然语言处理中的掩码语言建模(Masked Language Modeling)。它通过在输入图像中随机遮挡(或称为掩码)部分区域,然后训练模型来预测这些被遮挡部分的内容,从而提高模型的视觉理解能力。 Ma…

IDEA无法输入中文,怎么破

1.导航栏处&#xff0c;点击help菜单&#xff0c;选择Edit Custom VM Options.. 2.编辑文件&#xff0c;在文件末尾添加&#xff1a; -Drecreate.x11.input.methodtrue 3.保存文件即可&#xff0c;如果还是不行&#xff0c;就关闭所有Idea程序&#xff0c;重新启动Idea

机器学习之集成学习

一&#xff1a;概念 顾名思义集成学习就是用多个其他的算法结合起来使用 对于“其他算法”有同类和同质的区别&#xff0c;同质指的是所用的算法都是同一类型的&#xff0c;比如决策树和神经网络&#xff0c;这种也叫基学习器。反之亦然&#xff0c;但一般使用的是同质的。 …

网络治理新模式:Web3时代的社会价值重构

随着Web3技术的崛起&#xff0c;传统的网络治理模式正在经历革新&#xff0c;这不仅仅是技术的进步&#xff0c;更是对社会价值观念的挑战和重构。本文将深入探讨Web3时代的网络治理新模式&#xff0c;其背后的技术基础、社会影响以及未来的发展方向。 1. 引言 Web3时代&#…

文件进行周期性备份后权限更改的解决方案--使用脚本和定时任务

这里写目录标题 背景现象解决方案原因分析面临的问题解决思路操作步骤每个文件夹权限分配表测试chmod和chown两个命令是否可行写脚本实现定时同步同时修改权限 异地同步改权限在NAS上生成SSH密钥对将NAS的公钥复制到Linux服务器在NAS上编写同步脚本在NAS上执行脚本&#xff0c;…

咖啡机器人如何实现定量出水?

咖啡机器人实现定量出水的关键在于流量控制系统的设计&#xff0c;其中霍尔式流量计和光电式流量计是常用的测量设备。这两种流量计均具有精确高、一致性强、多种高低流量控制等特点&#xff0c;能够满足咖啡机器人定量出水的需求。 对于霍尔式流量计&#xff0c;其利用霍尔效…

防近视台灯有效果吗?专业护眼台灯推荐!告诉你台灯怎么选

随着学业负担的加重和电子设备的广泛普及&#xff0c;近视问题在青少年群体中愈发凸显&#xff0c;近视率持续走高。导致近视的因素错综复杂&#xff0c;除了过度使用手机外&#xff0c;遗传因素、不良的用眼习惯、环境因素、营养不均衡以及学习压力等均为重要因素&#xff0c;…

【深海王国】小学生都能玩的语音模块?ASRPRO打造你的第一个智能语音助手(1)

Hi~ (o^^o)♪, 各位深海王国的同志们&#xff0c;早上下午晚上凌晨好呀~ 辛勤工作的你今天也辛苦啦(/≧ω) 今天大都督将为大家带来全新系列——小学生都能玩的语音模块&#xff0c;帮你一周内快速学会语音模块的使用方式&#xff0c;打造一个可用于智能家居、物联网领域的语音…

【SpringBoot3.x】自定义开发通用SDK

1. 前言 相信大家学习SpringBoot到现在&#xff0c;使用Maven构建项目时&#xff0c;会在pom.xml文件中引入各种各样的依赖&#xff0c;那么我们如何将自己常用的一些工具类库进行封装成starter或者SDK供其他项目使用呢&#xff0c;本博客就会带着大家一步一步创建自定义的SDK…

使用 MyFlash 实现 MySQL 数据闪回

文章目录 简介GithubMyFlash 限制MySQL 准备开启 binlogmysqlbinlog 安装 MyFlashflashback 选项生成回滚文件执行回滚操作操作示例 简介 MySQL中的Binlog&#xff08;Binary Log&#xff09;数据闪回&#xff0c;也称为Point-in-Time Recovery (PITR)&#xff0c;是一种强大的…

Ansible-综合练习-生产案例

斌的招儿 网上教程大多都是官网模板化的教程和文档&#xff0c;这里小斌用自己实际生产环境使用的例子给大家做一个详解。涉及到一整套ansible的使用&#xff0c;对于roles的使用&#xff0c;也仅涉及到tasks和files目录&#xff0c;方便大家快速上手并规范化管理。 0.环境配置…

私接路由器导致部分终端(电脑、手机等)无法上网问题分析

目录 【1】私接路由器场景 【2】进行网络基本配置&#xff0c;模拟终端可以正常上网 【2.1】Http-Server配置 【2.2】ISP配置 【2.3】R-hefa配置 【2.4】Client1配置 【2.5】PC配置 【2.6】测试验证上网是否正常 【3】私接路由器后再测试验证公司内网各终端访问外网是…

大模型AI技术实现语言规范练习

人工智能技术可以为语言规范练习提供多种有效的解决方案&#xff0c;帮助学习者更有效地掌握语言规范。以下是一些常见的应用场景。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 1. 智能纠错 利用自然语言处理技术&#xff0c;可以…

代码随想录-Day42

1049. 最后一块石头的重量 II 有一堆石头&#xff0c;用整数数组 stones 表示。其中 stones[i] 表示第 i 块石头的重量。 每一回合&#xff0c;从中选出任意两块石头&#xff0c;然后将它们一起粉碎。假设石头的重量分别为 x 和 y&#xff0c;且 x < y。那么粉碎的可能结果…

定制型汽车传感器在汽车中的应用

定制型汽车霍尔传感器在汽车中的应用及功能 曲轴和凸轮轴位置传感器&#xff1a; 这些传感器用于监测发动机的曲轴和凸轮轴的位置&#xff0c;帮助发动机管理系统精确控制点火时机和燃油喷射&#xff0c;提高发动机效率。 变速器控制系统&#xff1a; 在自动变速器中&#xf…
最新文章