一、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.10 | ansible |
被管理端1 | 192.168.10.20 | 無需安裝 |
被管理端2 | 192.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_reload | yes:重啟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 | 指定掛載狀態,可選值為 mounted 、unmounted 或 absent 。 |
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 | 指定打包文件的格式,可以是 zip 、tar 、gz 或 bzip2 。默認為 tar 格式。 |
remove | 指定是否在打包文件之后,刪除源目錄或文件。可選值為 yes 或 no 。默認為 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 格式輸出。