天天看點

Ansible常用子產品Ad-Hoc用法

1、配置主機清單
[root@rocky8 ~]#cd /data/ansible/
[root@rocky8 ~]#ansible-config init --disabled > ansible.cfg   #生成初始化配置檔案
[root@rocky8 ansible]#vim ansible.cfg       #編輯配置檔案
[defaults]
inventory = /data/ansible/hosts             #設定主機清單檔案位置
[root@rocky8 ansible]#vim /data/ansible/hosts     #設定主機清單
[rocky]
10.0.0.28 ansible_connection=ssh ansible_ssh_port=6666 ansible_ssh_user=root ansible_ssh_password=redhat        #指定使用者和密碼
www.wang.org              #通用域名連接配接,需要在/etc/hosts解析

[ubuntu]
10.0.0.101
10.0.0.102

[centos]
10.0.0.7

[local]
10.0.0.18 ansible_connection=local    ##指定本地連接配接,無需ssh配置
[root@rocky8 ansible]#ansible --version          
ansible [core 2.12.2]
  config file = /data/ansible/ansible.cfg        #可以看到配置生效檔案      

1-1、Ansible配置檔案優先級:

ANSIBLE_CONFIG #環境變量,注意:指定目錄下的ansible.cfg檔案必須存在才能生效
./ansible.cfg #目前目錄下的ansible.cfg,一般一個項目對應一個專用配置檔案,推薦使用
~/.ansible.cfg #目前使用者家目錄下的.ansible.cfg
/etc/ansible/ansible.cfg #系統預設配置檔案      

1-2、配置檔案說明

[defaults]
#inventory = /etc/ansible/hosts #主機清單配置檔案
#library = /usr/share/my_modules/ #庫檔案存放目錄
#remote_tmp = $HOME/.ansible/tmp #臨時py指令檔案存放在遠端主機目錄
#local_tmp = $HOME/.ansible/tmp #本機的臨時指令執行目錄
#forks = 5 #預設并發數
#sudo_user = root #預設sudo 使用者
#ask_sudo_pass = True #每次執行ansible指令是否詢問ssh密碼
#ask_pass = True
#remote_port = 22
#host_key_checking = False #檢查對應伺服器的host_key,建議取消此行注釋,實作第一次連接配接自動信任目标主機
#log_path=/var/log/ansible.log #日志檔案,建議啟用
#module_name = command #預設子產品,可以修改為shell子產品
[privilege_escalation] #普通使用者提權配置
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False      
[root@rocky8 ansible]#export ANSIBLE_CONFIG=/opt     #通過環境變量ANSIBLE_CONFIG指定ansible配置檔案路徑
[root@rocky8 ansible]#cp ansible.cfg /opt/
[root@rocky8 ansible]#ansible --version
ansible [core 2.12.2]
  config file = /opt/ansible.cfg         #可以看到配置生效檔案,ansible.cfg檔案必須存在才能生效      

1-3、主機清單說明

ansible_ssh_host #将要連接配接的遠端主機名.與你想要設定的主機的别名不同的話,可通過此變量設定.
ansible_ssh_port #ssh端口号.如果不是預設的端口号,通過此變量設定.這種可以使用 ip:端口
192.168.1.100:2222
ansible_ssh_user #預設的 ssh 使用者名
ansible_ssh_pass #ssh 密碼(這種方式并不安全,我們強烈建議使用 --ask-pass 或 SSH 密鑰)
ansible_sudo_pass #sudo 密碼(這種方式并不安全,我們強烈建議使用 --ask-sudo-pass)
ansible_sudo_exe (new in version 1.8) #sudo 指令路徑(适用于1.8及以上版本)
ansible_connection #與主機的連接配接類型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前預設使用 paramiko.1.2 以後預設使用 'smart','smart' 方式會根據是否支援 ControlPersist, 來判斷'ssh' 方式是否可行.
ansible_ssh_private_key_file #ssh 使用的私鑰檔案.适用于有多個密鑰,而你不想使用 SSH 代理的情況.
ansible_shell_type #目标系統的shell類型.預設情況下,指令的執行使用 'sh' 文法,可設定為'csh' 或 'fish'.
ansible_python_interpreter #目标主機的 python 路徑.适用于的情況: 系統中有多個 Python,或者指令路徑不是"/usr/bin/python",比如 \*BSD, 或者 /usr/bin/python 不是 2.X 版本的
Python.之是以不使用 "/usr/bin/env" 機制,因為這要求遠端使用者的路徑設定正确,且要求 "python"可執行程式名不可為 python以外的名字(實際有可能名為python26).與
ansible_python_interpreter 的工作方式相同,可設定如 ruby 或 perl 的路徑....      
2、檢視幫助
[root@rocky8 ansible]#ansible-doc -l   #列出所有子產品
[root@rocky8 ansible]#ansible-doc ping           #檢視指定子產品幫助
[root@rocky8 ansible]#ansible-doc -s ping        #檢視指定子產品簡要幫助
- name: Try to connect to host, verify a usable python and return >
  ping:
      data:                  # Data to return for the `ping' retur>
                             # this parameter is
                             # set to `crash', the
                             # module will cause
                             # an exception.      

2-1、檢視指定的插件

[root@rocky8 ansible]#ansible-doc -t lookup -l
[root@rocky8 ansible]#ansible-doc -t connection -l      
3、Ad-Hoc用法
ansible <host-pattern> [-m module_name] [-a args]
--version #顯示版本
-m module #指定子產品,預設為command
-v #詳細過程 -vv -vvv更詳細
--list-hosts #顯示主機清單,可簡寫 --list
-C, --check #檢查,并不執行
-T, --timeout=TIMEOUT #執行指令的逾時時間,預設10s
-k, --ask-pass #提示輸入ssh連接配接密碼,預設Key驗證
-u, --user=REMOTE_USER #執行遠端執行的使用者,預設root
-b, --become #代替舊版的sudo實作通過sudo機制實作提升權限
--become-user=USERNAME #指定sudo的runas使用者,預設為root
-K, --ask-become-pass #提示輸入sudo時的密碼
-f FORKS, --forks FORKS #指定并發同時執行ansible任務的主機數
-i INVENTORY, --inventory INVENTORY #指定主機清單檔案      
[root@rocky8 ansible]#ansible all -a 'sleep 5' -f10    #并發10執行
[root@rocky8 ansible]#ansible "*" -m ping       #使用*通配符
[root@rocky8 ansible]#ansible 10.0.0.* -m ping    
[root@rocky8 ansible]#ansible rocky -m ping     #ping rocky組中的主機

# 或關系
[root@rocky8 ansible]#ansible 'rocky:centos' -m ping   #ping 在rocky組中的主機或在centos組中的主機
# 邏輯與
[root@rocky8 ansible]#ansible 'rocky:&ubuntu' -m ping  #ping 在rocky組中的主機并且在ubuntu組中的主機
# 邏輯非
[root@rocky8 ansible]#ansible 'all:!centos:!rocky' -m ping  #ping 所有主機,但不在centos組中和ubuntu組中的主機
# 綜合邏輯
[root@rocky8 ansible]#ansible 'rocky:ubuntu:¢os:!local' -m ping      

3-1、command

功能:在遠端主機執行指令,此為預設子產品,可忽略 -m 選項

注意:此指令不支援 $VARNAME < > | ; & 等,可能用shell子產品實作

注意:此子產品不具有幂等性

常見選項

chdir=dir #執行指令前,先切換至目錄dir
creates=file #當file不存在時才會執行
removes=file #當file存在時才會執行      
[root@rocky8 ansible]#ansible rocky -m command -a 'chdir=/etc cat os-release'      
[root@rocky8 ansible]#ansible rocky -m command -a 'chdir=/etc creates=/data/f1.txt cat issue'    #當/data/f1.txt不存在時才會執行 切換目錄 檢視issue
[root@rocky8 ansible]#ansible rocky -m command -a 'chdir=/etc removes=/data/f1.txt cat issue '   #當/data/f1.txt存在時才會執行 切換目錄 檢視issue      

3-2、shell

功能:和command相似,用shell執行指令,支援各種符号,比如:*,$, > , 相當于增強版的command子產品

注意:此子產品不具有幂等性,建議能不能就用此子產品,最好使用專用子產品

常見選項

chdir=dir #執行指令前,先切換至目錄dir
creates=file #當file不存在時才會執行
removes=file #當file存在時才會      
[root@rocky8 ansible]#ansible rocky -m shell -a 'echo $HOSTNAME'
[root@rocky8 ansible]#ansible rocky -m shell -a 'echo redhat | passwd --stdin wang'      
[root@rocky8 ansible]#vim ansible.cfg
[defaults]
inventory = /data/ansible/hosts
module_name = shell              #修改shell為預設子產品      

3-3、script

功能:在遠端主機上運作ansible伺服器上的腳本(無需執行權限)

注意:此子產品不具有幂等性

常見選項

chdir=dir #執行指令前,先切換至目錄dir
cmd #指定ansible主機的指令
creates=file #當file不存在時才會執行
removes=file #當file存在時才會執行      
[root@rocky8 ansible]#ansible rocky -m script -a 'test.sh'      

3-4、copy

功能:複制ansible伺服器主要端或遠端的本機的檔案到遠端主機

注意: src=file 如果是沒指明路徑,則為目前目錄或目前目錄下的files目錄下的file檔案

常見選項

src #控制端的源檔案路徑
dest #被控端的檔案路徑
owner #屬主
group #屬組
mode #權限
backup #是否備份
validate #驗證成功才會執行copy
remote_src #no是預設值,表示src檔案在ansible主機,yes表示src檔案在遠端主機      
[root@rocky8 ansible]#ansible rocky -m copy -a 'src=/root/reset.sh dest=/opt/ owner=wang group=wang mode=777'
[root@rocky8 ansible]#ansible rocky -m copy  -a 'content=wang\nli\nzhang\n dest=/opt/test.log owner=wang group=wang'
      

3-5、get_url

功能: 用于将檔案從http、https或ftp下載下傳到被管理機節點上

常用參數如下

url #下載下傳檔案的URL,支援HTTP,HTTPS或FTP協定
dest #下載下傳到目标路徑(絕對路徑),如果目标是一個目錄,就用原檔案名,如果目标設定了名稱就用目标設定的名稱
owner #指定屬主
group #指定屬組
mode #指定權限
force #如果yes,dest不是目錄,将每次下載下傳檔案,如果内容改變替換檔案。如果no,則隻有在目标不在時才會下載下傳
checksum #對目标檔案在下載下傳後計算摘要,以確定其完整性
  #示例: checksum="sha256:D98291AC[...]B6DC7B97",
      checksum="sha256:http://example.com/path/sha256sum.txt"
url_username #用于HTTP基本認證的使用者名。 對于允許空密碼的站點,此參數可以不使用`url_password'
url_password #用于HTTP基本認證的密碼。 如果未指定`url_username'參數,則不會使用`url_password'參數
validate_certs #如果“no”,SSL證書将不會被驗證。 适用于自簽名證書在私有網站上使用timeout #URL請求的逾時時間,秒為機關      
[root@rocky8 ansible]#ansible rocky -m get_url -a 'url=http://nginx.org/download/nginx-1.22.0.tar.gz dest=/opt/ checksum="md5:9b1fb7dae677b22ce4b4e271a4de3501"'      

3-6、fetch

功能:從遠端主機提取檔案至ansible的主要端,copy相反,目前不支援目錄

常見選項

src #被控制端的源檔案路徑,隻支援檔案
dest #ansible控制端的目錄路徑      
[root@rocky8 ansible]#ansible rocky -m fetch -a 'src=/root/reset.sh dest=/opt'      

3-7、file

功能:設定檔案屬性,建立檔案,目錄和軟連結等

常見選項

path #在被控端建立的路徑
owner #屬主
group #屬組
mode #權限
state #狀态
=touch #建立檔案
=directory #建立目錄
=link #軟連結
=hard #硬連結
recurse #yes表示遞歸授權      
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/text.txt state=touch'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/text.txt state=absent'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/test state=directory'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/test state=absent'
[root@rocky8 ansible]#ansible all -m file -a 'src=/etc path=/opt/etclink state=link'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/etclink state=absent'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/test owner=wang'   #修改目錄本身的所有者為wang
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/test owner=wang recurse=yes' #遞歸修改目錄的所有者為wang      

3-8、stat

功能:檢查檔案或檔案系統的狀态

注意:對于Windows目标,請改用win_stat子產品

常見選項

path #檔案/對象的完整路徑(必須)      

常用的傳回值判斷

exists: 判斷是否存在
isuid: 調用使用者的ID與所有者ID是否比對      
[root@rocky8 ansible]#ansible 127.0.0.1 -m stat -a 'path=/etc/passwd'      

3-9、unarchive

功能:解包解壓縮

實作有兩種用法:

将ansible主機上的壓縮包傳到遠端主機後解壓縮至特定目錄,設定remote_src=no,此為預設值,可省略

将遠端本主機上或非ansible的其它主機的某個壓縮包解壓縮到遠端主機本機的指定路徑下,需要設定remote_src=yes

常見參數:

remote_src #和copy功能一樣且選項互斥,yes表示源檔案在遠端被控主機或其它非ansible的其它主機上,no表示檔案在ansible主機上,預設值為no, 此選項代替copy選項
copy #預設為yes,當copy=yes,拷貝的檔案是從ansible主機複制到遠端主機上,如果設定為copy=no,會在遠端主機上尋找src源檔案,此選項已廢棄
src #源路徑,可以是ansible主機上的路徑,也可以是遠端主機(被管理端或者第三方主機)上的路徑,如果是遠端主機上的路徑,則需要設定remote_src=yes
dest #遠端主機上的目标路徑
owner #預設遞歸
group #預設遞歸
mode #設定解壓縮後的檔案權限,預設遞歸
creates=/path/file #當絕對路徑/path/file不存在時才會執行      
[root@rocky8 ansible]#ansible rocky -m unarchive -a 'src=https://releases.ansible.com/ansible/ansible-2.9.7.tar.gz dest=/opt remote_src=yes'        #遠端主機下載下傳并解壓到遠端主機的opt下
[root@rocky8 ansible]#ansible all -m unarchive -a 'src=/opt/nginx-1.22.0.tar.gz dest=/opt/ owner=wang copy=no'   #解壓遠端主機的nginx到遠端主機的opt下      

3-10、archive

功能:打包壓縮儲存在被管理節點

常見選項

path #壓縮的檔案或目錄
dest #壓縮後的檔案
format #壓縮格式,支援gz,bz2,xz,tar,zip      
[root@rocky8 ansible]#ansible rocky -m archive -a 'path=/opt/ansible-2.9.7 dest=/opt/ansible.gz format=gz owner=wang'    #壓縮遠端主機的ansible-2-9-7目錄為ansible.gz 屬主是wang      

3-11、hostname

功能:管理主機名

常見選項

name #修改後的主機名稱      
[root@rocky8 ansible]#ansible www.wang.org -m hostname -a 'name=node38'      

3-12、cron

功能:計劃任務

支援時間:minute,hour,day,month,weekda

常見選項

name #描述腳本的作用
minute #分鐘
hour #小時
weekday #周
user #任務由哪個使用者運作;預設root
job #任務      
[root@rocky8 ansible]#ansible rocky -m cron -a 'hour=2 minute=20 weekday=1-5 name=test job="logger testlog" user=wang'
[root@rocky8 ansible]#ansible rocky -m cron -a 'hour=2 minute=20 weekday=1-5 name=test job="logger testlog" user=wang disabled=yes'
[root@rocky8 ansible]#ansible rocky -m cron -a 'hour=2 minute=20 weekday=1-5 name=test job="logger testlog" user=wang disabled=no'
[root@rocky8 ansible]#ansible rocky -m cron -a 'state=absent name=test user=wang'      

3-13、yum和apt

功能:管理軟體包

yum 管理軟體包,隻支援RHEL,CentOS,fedora,不支援Ubuntu其它版本

apt 子產品管理 Debian 相關版本的軟體包

yum常見選項

name #軟體包名稱
state #狀态
  =present #安裝,此為預設值
  =absent #删除
  =latest #最新版
list #列出指定包
enablerepo #啟用哪個倉庫安裝
disablerepo #不使用哪些倉庫的包
exclude #排除指定的包
validate #是否檢驗,預設為yes      
[root@rocky8 ansible]#ansible rocky -m yum -a 'name=httpd state=present'
[root@rocky8 ansible]#ansible rocky -m yum -a 'name=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/8/x86_64/zabbix-agent2-5.0.24-1.el8.x86_64.rpm state=present disable_gpg_check=yes'    #安裝zabbix agent rpm包
[root@rocky8 ansible]#ansible rocky -m yum -a 'name=nginx state=present enablerepo=epel      #啟用epel源進行安裝
[root@rocky8 ansible]#ansible rocky -m yum -a 'name=nginx state=absent'   #解除安裝nginx

[root@rocky8 ansible]#ansible rocky -m yum -a 'name=* state=latest exclude=kernel*,foo*'      #更新除kernel和foo開頭以外的所有包
[root@rocky8 ansible]#ansible ubuntu -m apt -a 'name=sl'
[root@rocky8 ansible]#ansible localhost -m yum -a 'list=tree'   #檢視tree包      

3-14、yum_repository

功能: 此子產品實作yum的倉庫配置管理

常見選項

name #倉庫id
description #倉庫描述名稱,對應配置檔案中的name=
baseurl #倉庫的位址
gpgcheck #驗證開啟
gpgkey #倉庫公鑰路徑
state      
[root@rocky8 ansible]#ansible rocky -m yum_repository -a 'name=zibbix description="zibbipos" file=zibbixfile baseurl=https://mirrors.aliyun.com/zabbix/zabbix/6.0/rhel/$releasever/$basearch gpgcheck=no'      

3-15、service

此子產品和sytemd功能相似,選項很多相同

功能:管理服務

常見選項

name #服務名稱
state #服務狀态
=started #啟動
=stopped #停止
=restarted #重新開機
=reloaded #重載
enabled #開啟自啟動
daemon_reload #加載新的配置檔案,适用于systemd子產品      
[root@rocky8 ansible]#ansible rocky -m service -a 'name=httpd state=started enabled=yes'
[root@rocky8 ansible]#ansible rocky -m service -a 'name=httpd state=stopped'
[root@rocky8 ansible]#ansible rocky -m service -a 'name=httpd state=reloaded'
[root@rocky8 ansible]#ansible rocky -m service -a 'name=httpd state=stopped enabled=no'      

3-16、user

功能:管理使用者

常見選項

name #建立的名稱
uid #指定uid
group #指定基本組
shell #登入shell類型預設/bin/bash
create_home #是否建立家目錄,預設會建立家目錄,no不建立
password #設定對應的密碼,必須是加密後的字元串才行,否則不生效
system #yes表示系統使用者
groups #附加組
append #追加附加組使用,yes表示增加新的附加組
state #absen删除
remove #yes表示删除使用者時将家目錄一起删除
generate_ssh_key #建立私鑰
ssh_keyu_bits #私鑰位數
ssh_key_file #私鑰檔案路徑      
[root@rocky8 ansible]#ansible rocky -m user -a 'name=user1 comment=test uid=2048 home=/data/user1 group=root'
[root@rocky8 ansible]#ansible all -m user -a 'name=nn comment=test uid=1088 group=1088 groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nn non_unique=yes'
[root@rocky8 ansible]#ansible all -m user -a 'name=nn state=absent remove=yes'   #remove=yes表示删除使用者及家目錄等資料,預設remove=no

[root@rocky8 ansible]#ansible localhost -m debug -a "msg={{ 'redhat' | password_hash('sha512') }}"       #生成redhat加密的密碼
[root@rocky8 ansible]#ansible localhost -m debug -a "msg={{ 'redhat' | password_hash('sha512','salt') }}"      #加鹽生成redhat加密的密碼
[root@rocky8 ansible]#ansible rocky -m user -a 'name=nnn system=yes shell=/sbin/nologin password="6$salt$TXGPxOwUfTSW0I8V9dD9QXd4BCkZrvltTEmMoayMhN4MePeBDuYpasD6QdwDwKRi5xyDzI5ihtXMaGufO5jrW/"'   #用上面建立的密碼建立使用者

[root@rocky8 ansible]#ansible rocky -m user -a 'name=test generate_ssh_key=yes ssh_key_bits=4096 ssh_key_file=.ssh/id_rsa'       #建立使用者test,并生成4096bit的私鑰      

3-17、group

功能:管理組

常見選項

name #指定組名稱
gid #指定gid
state
  =present #建立,預設
  =absent #删除
system #是否是系統組      
[root@rocky8 ansible]#ansible all -m group -a 'name=nn gid=1088'
[root@rocky8 ansible]#ansible all -m group -a 'name=nn state=absent'      

3-18、lineinfile

功能:相當于sed,主要用于修改一行的檔案内容

常見選項

path #被控端檔案的路徑
regexp #正則比對文法格式,表示被替換的内容
line #替換為的内容
state #absent表示删除
insertafter #插入到替換内容前面,如和regexp同時存在,隻在沒找到與regexp比對時才使用
insertafter
insertbefore #插入到替換内容後面,如和regexp同時存在,隻在沒找到與regexp比對時才使用
insertafter
backrefs #支援後面引用,yes和no
backup #修改前先備份
create #如果檔案不存在,則建立,預設不存在會出錯
mode #指定權限
owner #指定使用者
group #指定組
#注意
regexp參數 :使用正規表達式比對對應的行,當替換文本時,如果有多行文本都能被比對,則隻有最後面被比對到的那行文本才會被替換,當删除文本時,如果有多行文本都能被比對,這麼這些行都會被删除。      

注意: 如果想進行多行比對進行替換需要使用replace子產品

[root@rocky8 ansible]#ansible rocky -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 8080'"        #修改監聽端口
[root@rocky8 ansible]#ansible rocky -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=enforcing'"       #修改SELinux 
[root@rocky8 ansible]#ansible rocky -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"'
[root@rocky8 ansible]#ansible rocky -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 line="DNS4=223.5.5.5"'
[root@rocky8 ansible]#ansible rocky -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 insertafter="^DNS3" line="GATEWAY=10.0.0.1"'    ##給主機增加一個網關,但需要增加到DNS3行下面
[root@rocky8 ansible]#ansible rocky -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="GATEWAY=10.0.0.1" state=absent'      

3-19、replace

該子產品有點類似于sed指令,主要也是基于正則進行比對和替換,建議使用

功能: 多行修改替換

常見選項

path #被控端檔案的路徑
regexp #正則比對文法格式,表示被替換的内容
replace #替換為的内容
after #插入到替換内容前面,
before #插入到替換内容後面
backup #修改前先備份
mode #指定權限
owner #指定使用者
group #指定組      
[root@rocky8 ansible]#ansible rocky -m replace -a 'path=/etc/fstab regexp="^(UUID.*)" replace="#\1"'
[root@rocky8 ansible]#ansible rocky -m replace -a 'path=/etc/fstab regexp="^#(UUID.*)" replace="\1"'      

3-20、selinux

功能: 該子產品管理 SELInux 政策

常見選項

policy #指定SELINUXTYPE=targeted
state #指定SELINUX=disabled      
[root@rocky8 ansible]#ansible www.wang.org -m selinux -a 'policy=targeted state=permissive'     #需要重新開機方能生效
[root@rocky8 ansible]#ansible www.wang.org -m selinux -a 'policy=targeted state=disabled'       #需要重新開機方能生效      

3-21、reboot

功能: 重新開機

常見選項

msg #重新開機提示
pre_reboot_delay #重新開機前延遲時間的秒數
post_reboot_delay #重新開機後延遲時間的秒數後,再驗證系統正常啟動
reboot_timeout #重新開機後延遲時間再執行測試成功與否的指令
test_command #執行測試成功與否的指令      
[root@rocky8 ansible]#ansible rocky -m reboot -a 'msg="this host will be reboot"'      

3-22、mount

功能: 挂載和解除安裝檔案系統

常見選項

src #源裝置路徑,或網絡位址
path #挂載至本地哪個路徑下
fstype #裝置類型; nfs
opts #挂載的選項
state #挂載還是解除安裝
  =present #永久挂載,但沒有立即生效
  =absent #解除安裝臨時挂載,并删除永久挂載
  =mounted #永久和臨時挂載
  =unmounted #臨時解除安裝      
[root@rocky8 ansible]#ansible www.wang.org -m mount -a 'src="/dev/vdb2" path=/mnt/vdo fstype=ext4 state=present'
[root@rocky8 ansible]#ansible www.wang.org -m mount -a 'path=/mnt/vdo fstype=ext4  state=absent'      

3-23、setup

功能: setup 子產品來收集主機的系統資訊,這些 facts 資訊可以直接以變量的形式使用,但是如果主機較多,會影響執行速度

可以使用 gather_facts: no 來禁止 Ansible 收集 facts 資訊

常見選項

filter #指定過濾條件      
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_nodename'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_hostname'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_domain'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_memtotal_mb'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_memory_mb'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_memfree_mb'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_os_family'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_distribution'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_distribution_major_version'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_distribution_version'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_processor_vcpus'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_all_ipv4_address'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_architecture'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_uptime_seconds'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_processor*'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_env'      
[root@rocky8 ansible]#ansible all -m setup -a 'filter=ansible_python_version'
[root@rocky8 ansible]#ansible 10.0.0.101 -m setup -a 'filter=ansible_all_ipv4_addresses'
[root@rocky8 ansible]#ansible 10.0.0.101 -m setup -a 'filter=ansible_all_ipv4_addresses'      

3-24 debug

功能: 此子產品可以用于輸出資訊,并且通過 msg 定制輸出的資訊内容,功能類似于echo指令

注意: msg後面的變量有時需要加 " " 引起來

常見選項

msg #指定指令輸出的資訊
var #指定變量名,和msg互斥
verbosity #詳細度      
[root@rocky8 ansible]#ansible rocky -m debug     #debug 子產品預設輸出Hello world      

3-25、sysctl

功能: 修改核心參數

常見選項

name #核心參數
value #指定值
state #是否儲存在sysctl.conf檔案中,預設present
sysctl_set #使用sysctl -w 驗證值生效      
[root@rocky8 ansible]#ansible rocky -m sysctl -a 'name=net.ipv4.ip_forward value=1 state=present'      

3-26、pam_limits

- name: Change Limit /etc/security/limit.conf
  pam_limits:
  domain: "*"
    limit_type: "{{ item.limit_type }}"
    limit_item: "{{ item.limit_item }}"
    value: "{{ item.value }}"
  loop:
    - { limit_type: 'soft', limit_item: 'nofile',value: '100000' }
    - { limit_type: 'hard', limit_item: 'nofile',value: '10000' }      

3-27、apt_repository

repo #倉庫資訊
state #添加或删除
update_cache #是否apt update,預設yes
filename #倉庫檔案,預設放在/etc/apt/sources.list.d/file.list      

3-28、apt_key 子產品

url #key路徑
state #添加或删除      

3-29、其它子產品

nginx_status_info
nginx_status_facts
mysql_db #需要安裝MySQL-python包
mysql_user #需要安裝MySQL-python包
redis
mongodb
postgresql
haproxy
git      

繼續閱讀