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/bashecho "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.pswp.cn/diannao/36187.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/36187.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/36187.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

程序員必會英文語句 – 前后端交流篇

很多程序員日常用不到說英語的場景&#xff0c;或者遇到不會的英文單詞直接一查就可以了。但也有很多程序員面試的時候要求來一場英文的表述&#xff0c;最近的工作呢&#xff0c;需要和外國人的后端開發交流&#xff0c;所以我整理了一下我日常用到的英文語句&#xff0c;也許…

Mybatis-Plus的筆記

Mybatis-Plus其實是Mybatis的升級版&#xff0c;他簡化了原先mybatis需要手動寫CURD語句轉而繼承BaseMapper來實現。具體變化如下&#xff1a; 1&#xff0c;MyBatis-Plus簡介&#xff1a;MP&#xff0c;是mybatis的增強工具&#xff0c;是基于mybatis上開發的。 特點&#xf…

智駕未來,一觸即達——探索全新加油App的無限可能

一、引言 隨著科技的飛速發展&#xff0c;智能出行已成為現代生活的重要組成部分。為了滿足廣大駕駛者的需求&#xff0c;我們傾力打造了一款全新的加油App&#xff0c;旨在為您的駕駛旅程提供前所未有的便捷與智能體驗。 二、產品概述 我們的加油App不僅是一款導航工具&…

windows如何看是否支持多核并行

在Windows中查看是否支持多核并行處理&#xff0c;可以通過以下幾種方法&#xff1a; 使用任務管理器&#xff1a; 右鍵點擊任務欄空白處選擇“任務管理器”。 切換到“性能”標簽頁。 查看“處理器”一欄&#xff0c;如果看到多個處理器核心&#xff0c;并且每個核心旁邊顯…

每日一道算法題 有效括號序列

題目 有效括號序列_牛客題霸_牛客網 (nowcoder.com) Python 1長度必須為偶數 2就像開心消消樂一樣&#xff0c;一左一右就消掉。 class Solution:def isValid(self , s: str) -> bool:# write code here# flag[(),{},[]]# for _ in range(len(s)//2):# for i in fl…

以HMO模式為核心,平安健康穩健前行

自2014年成立以來&#xff0c;平安健康始終聚焦解決“看病難、看病貴、看病遠”的痛點&#xff0c;通過科技手段優化醫療服務流程&#xff0c;降低用戶就醫成本。經過數年的耕耘&#xff0c;平安健康已成功轉型為一站式健康管理平臺&#xff0c;打通了醫療、藥品、康復等多個環…

力扣每日一題 6/27 字符串 貪心

博客主頁&#xff1a;誓則盟約系列專欄&#xff1a;IT競賽 專欄關注博主&#xff0c;后期持續更新系列文章如果有錯誤感謝請大家批評指出&#xff0c;及時修改感謝大家點贊&#x1f44d;收藏?評論? 2734.執行子串操作后的字典序最小字符串【中等】 題目&#xff1a; 給你一…

Java中的異常處理:Checked與Unchecked的區別

Java中的異常處理&#xff1a;Checked與Unchecked的區別 大家好&#xff0c;我是免費搭建查券返利機器人省錢賺傭金就用微賺淘客系統3.0的小編&#xff0c;也是冬天不穿秋褲&#xff0c;天冷也要風度的程序猿&#xff01; 異常處理概述 在Java編程中&#xff0c;異常處理是一…

MySQL定位CPU利用率過高的SQL方法

前言 當mysql CPU告警利用率過高的時候&#xff0c;我們應該怎么定位是哪些SQL導致的呢&#xff0c;本文將介紹一下定位的方法。 本文所使用的方法&#xff0c;前提是你可以登錄到Mysql所在的服務器&#xff0c;執行命令查看進程&#xff0c;當然讓數據庫管理員登錄執行也可以…

科研所文件數據很關鍵,外發圖紙如何控制?

圖紙是科研所整個科研周期中最重要的資料類型之一。這些圖紙主要用于描述和記錄研究過程中的各種設計、實驗裝置、設備或產品原型等。 首先&#xff0c;科研所在進行新技術、新產品或新方法的研發時&#xff0c;通常需要進行詳細的設計和規劃。在這個過程中&#xff0c;科研人員…

小區物業管理收費系統源碼小程序

便捷、透明、智能化的新體驗 一款基于FastAdminUniApp開發的一款物業收費管理小程序。包含房產管理、收費標準、家屬管理、抄表管理、在線繳費、業主公告、統計報表、業主投票、可視化大屏等功能。為物業量身打造的小區收費管理系統&#xff0c;貼合物業工作場景&#xff0c;輕…

怎樣求解一個系統的穩態輸出

要求解一個系統的穩態輸出&#xff0c;需要根據系統的類型&#xff08;如線性時不變系統、非線性系統等&#xff09;、輸入信號的性質&#xff08;如階躍信號、正弦信號等&#xff09;以及系統的描述方法&#xff08;如微分方程、狀態空間模型等&#xff09;。這里主要介紹線性…

數字黃金 vs 全球計算機:比特幣與以太坊現貨 ETF 對比

撰文&#xff1a;Andrew Kang 編譯&#xff1a;J1N&#xff0c;Techub News 本文來源香港Web3媒體&#xff1a;Techub News 比特幣現貨 ETF 的通過為許多新買家打開了進入加密貨幣市場的大門&#xff0c;讓他們可以在投資組合中配置比特幣。但以太坊現貨 ETF 的通過&#xf…

AI從業者怎么做Science?清華大學AIR周浩:從文本生成到蛋白質設計的跨界探索

近日&#xff0c;北京智源大會「AI for Science」分論壇上&#xff0c;清華大學智能產業研究院副研究員周浩以「面向科學發現的生成式人工智能」為主題展開演講&#xff0c; HyperAI超神經在不違原意的前提下&#xff0c;對周浩教授的深度分享進行了整理匯總。 周浩教授演講現場…

遠程過程調用(RPC)

Hi~&#xff01;這里是奮斗的小羊&#xff0c;很榮幸您能閱讀我的文章&#xff0c;誠請評論指點&#xff0c;歡迎歡迎 ~~ &#x1f4a5;&#x1f4a5;個人主頁&#xff1a;奮斗的小羊 &#x1f4a5;&#x1f4a5;所屬專欄&#xff1a;C語言 &#x1f680;本系列文章為個人學習…

數字AI化銀行數字化轉型實戰手冊銀行數字化轉型大客戶營銷銷售講師培訓師唐興通談存量客戶理財金融科技與場景化

推動銀行數字化轉型的五個關鍵因素 推動銀行數字化轉型的五個關鍵因素&#xff1a; 客戶體驗。為客戶提供便利和個性化是數字化轉型的關鍵因素。銀行應開發和實施創新的數字渠道&#xff0c;例如移動應用程序、網上銀行、聊天機器人等&#xff0c;以方便獲取金融服務并提高客戶…

基于yolo的物體識別坐標轉換

一、模型簡介: 1.1、小孔成像模型簡圖如下:不考慮實際相機中存在的場曲、畸變等問題 相對關系為: 為了表述與研究的方便,我們將像面至于小孔之前,且到小孔的距離仍然是焦距f,這樣的模型與原來的小孔模型是等價的 相對關系為: 二、坐標系簡介: **世界坐標系(world coo…

2021-2024高校畢業生的就業趨勢和變化分析

一、不同行業、地區和學歷層次的高校畢業生就業情況差異 行業差異&#xff1a; 教育培訓行業&#xff1a;受“雙減”政策影響&#xff0c;教育培訓機構吸納畢業生的數量明顯下降&#xff0c;畢業生面臨重新選擇。互聯網領域&#xff1a;互聯網企業的業務優化調整力度加大&…

徹底解決 macos中chrome應用程序 的 無法更新 Chrome 彈窗提示 mac自定義參數啟動 chrome.app

mac系統中的chrome app應用在每次打開是都會提示一個 “無法更新 Chrome Chrome 無法更新至最新版本&#xff0c;因此您未能獲得最新的功能和安全修復程序。” &#xff0c; 然而最新的chrome 程序似乎在某些情況下居然會出現 輸入和顯示不一致的情況&#xff0c;暫時不想升…

You編程__封裝ElementPlus通用組件(會持續更新...)

YOU編程__封裝ElementPlus通用組件&#xff08;會持續更新…&#xff09; 1、通用表格組件 CommonTable.vue <template><div><el-form :model"query" inline class"query-form"><el-form-item><el-input v-model"query…