Ansible常用模組
阿新 • • 發佈:2020-09-21
Ansible Ad-hoc模式常用模組#
引用攔連結:https://www.cnblogs.com/yanjieli/p/10969143.html
ansible-doc 常用命令
# ansible-doc -h Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin] -j 以json格式顯示所有模組資訊 -l 列出所有的模組 -s 檢視模組常用引數 # 直接跟模組名,顯示模組所有資訊 [root@ansible ~]# ansible-doc -j [root@ansible ~]# ansible-doc -l [root@ansible ~]# ansible-doc -l |wc -l #統計所有模組個數,ansible2.8共計2834個模組 2834
命令相關的模組#
command#
ansible
預設的模組,執行命令,注意:shell中的"<"
,">"
,"|"
,";"
,"&"
,"$"
等特殊字元不能在command
模組中使用,如果需要使用,則用shell
模組
# 檢視模組引數 [root@ansible ~]# ansible-doc -s command # 在192.168.1.31伺服器上面執行ls命令,預設是在當前使用者的家目錄/root [root@ansible ~]# ansible 192.168.1.31 -a 'ls' # chdir 先切換工作目錄,再執行後面的命令,一般情況下在編譯時候使用 [root@ansible ~]# ansible 192.168.1.31 -a 'chdir=/tmp pwd' 192.168.1.31 | CHANGED | rc=0 >> /tmp # creates 如果creates的檔案存在,則執行後面的操作 [root@ansible ~]# ansible 192.168.1.31 -a 'creates=/tmp ls /etc/passwd' #tmp目錄存在,則不執行後面的ls命令 192.168.1.31 | SUCCESS | rc=0 >> skipped, since /tmp exists [root@ansible ~]# ansible 192.168.1.31 -a 'creates=/tmp11 ls /etc/passwd' # tmp11檔案不存在,則執行後面的ls命令 192.168.1.31 | CHANGED | rc=0 >> /etc/passwd # removes 和creates相反,如果removes的檔案存在,才執行後面的操作 [root@ansible ~]# ansible 192.168.1.31 -a 'removes=/tmp ls /etc/passwd' #tmp檔案存在,則執行了後面的ls命令 192.168.1.31 | CHANGED | rc=0 >> /etc/passwd [root@ansible ~]# ansible 192.168.1.31 -a 'removes=/tmp11 ls /etc/passwd' #tmp11檔案不存在,則沒有執行後面的ls命令 192.168.1.31 | SUCCESS | rc=0 >> skipped, since /tmp11 does not exist
shell#
專門用來執行shell
命令的模組,和command
模組一樣,引數基本一樣,都有chdir,creates,removes
等引數
# 檢視模組引數 [root@ansible ~]# ansible-doc -s shell [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'mkdir /tmp/test' [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'ls /tmp' #執行下面這條命令,每次執行都會更新檔案的時間戳 [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'cd /tmp/test && touch 1.txt && ls' 192.168.1.31 | CHANGED | rc=0 >> 1.txt # 由於有時候不想更新檔案的建立時間戳,則如果存在就不執行creates [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'creates=/tmp/test/1.txt cd /tmp/test && touch 1.txt && ls' 192.168.1.31 | SUCCESS | rc=0 >> skipped, since /tmp/test/1.txt exists
script#
用於在被管理機器上面執行shell
指令碼的模組,指令碼無需在被管理機器上面存在
# 檢視模組引數 [root@ansible ~]# ansible-doc -s script # 編寫shell指令碼 [root@ansible ~]# vim ansible_test.sh #!/bin/bash echo `hostname` # 在所有被管理機器上執行該指令碼 [root@ansible ~]# ansible all -m script -a '/root/ansible_test.sh' 192.168.1.32 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.1.32 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.1.32 closed." ], "stdout": "linux.node02.com\r\n", "stdout_lines": [ "linux.node02.com" ] } ......
檔案相關的模組#
file#
用於對檔案的處理,建立,刪除,許可權控制等
# 檢視模組引數 [root@ansible ~]# ansible-doc -s file path #要管理的檔案路徑 recurse #遞迴 state: directory #建立目錄,如果目標不存在則建立目錄及其子目錄 touch #建立檔案,如果檔案存在,則修改檔案 屬性 absent #刪除檔案或目錄 mode #設定檔案或目錄許可權 owner #設定檔案或目錄屬主資訊 group #設定檔案或目錄屬組資訊 link #建立軟連線,需要和src配合使用 hard #建立硬連線,需要和src配合使用 # 建立目錄 [root@ansible ~]# ansible 192.168.1.31 -m file -a 'path=/tmp/test1 state=directory' # 建立檔案 [root@ansible ~]# ansible 192.168.1.31 -m file -a 'path=/tmp/test2 state=touch' # 建立軟連結(src表示原始檔,path表示目標檔案) [root@ansible ~]# ansible 192.168.1.31 -m file -a 'src=/tmp/test1 path=/tmp/test3 state=link' # 刪除檔案 [root@ansible ~]# ansible 192.168.1.31 -m file -a 'path=/tmp/test2 state=absent' # 建立檔案時同時設定許可權等資訊 [root@ansible ~]# ansible 192.168.1.31 -m file -a 'path=/tmp/test4 state=directory mode=775 owner=root group=root'
copy#
用於管理端複製檔案到遠端主機,並可以設定許可權,屬組,屬主等
# 檢視模組引數 [root@ansible ~]# ansible-doc -s copy src #需要copy的檔案的源路徑 dest #需要copy的檔案的目標路徑 backup #對copy的檔案進行備份 content #直接在遠端主機被管理檔案中新增內容,會覆蓋原檔案內容 mode #對copy到遠端的檔案設定許可權 owner #對copy到遠端的檔案設定屬主 group #對copy到遠端檔案設定屬組 # 複製檔案到遠端主機並改名 [root@ansible ~]# ansible 192.168.1.31 -m copy -a 'dest=/tmp/a.sh src=/root/ansible_test.sh' # 複製檔案到遠端主機,並備份遠端檔案,安裝時間資訊備份檔案(當更新檔案內容後,重新copy時用到) [root@ansible ~]# ansible 192.168.1.31 -m copy -a 'dest=/tmp/a.sh src=/root/ansible_test.sh backup=yes' # 直接在遠端主機a.sh中新增內容 [root@ansible ~]# ansible 192.168.1.31 -m copy -a 'dest=/tmp/a.sh content="#!/bin/bash\n echo `uptime`"' # 複製檔案到遠端主機,並設定許可權及屬主與屬組 [root@ansible ~]# ansible 192.168.1.31 -m copy -a 'dest=/tmp/passwd src=/etc/passwd mode=700 owner=root group=root'
fetch#
用於從被管理機器上面拉取檔案,拉取下來的內容會保留目錄結構,一般情況用在收集被管理機器的日誌檔案等
# 檢視模組引數 [root@ansible ~]# ansible-doc -s fetch src #指定需要從遠端機器拉取的檔案路徑 dest #指定從遠端機器拉取下來的檔案存放路徑 # 從被管理機器上拉取cron日誌檔案,預設會已管理節點地址建立一個目錄,並存放在內 [root@ansible ~]# ansible 192.168.1.31 -m fetch -a 'dest=/tmp src=/var/log/cron' [root@ansible ~]# tree /tmp/192.168.1.31/ /tmp/192.168.1.31/ └── var └── log └── cron 2 directories, 1 file
使用者相關的模組#
user#
用於對系統使用者的管理,使用者的建立、刪除、家目錄、屬組等設定
# 檢視模組引數 [root@ansible ~]# ansible-doc -s user name #指定使用者的名字 home #指定使用者的家目錄 uid #指定使用者的uid group #指定使用者的使用者組 groups #指定使用者的附加組 password #指定使用者的密碼 shell #指定使用者的登入shell create_home #是否建立使用者家目錄,預設是yes remove #刪除使用者時,指定是否刪除家目錄 state: absent #刪除使用者 # 建立使用者名稱指定家目錄,指定uid及組 [root@ansible ~]# ansible 192.168.1.31 -m user -a 'name=mysql home=/opt/mysql uid=1002 group=root' [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'id mysql && ls -l /opt' 192.168.1.31 | CHANGED | rc=0 >> uid=1002(mysql) gid=0(root) 組=0(root) 總用量 0 drwx------ 3 mysql root 78 5月 27 18:13 mysql # 建立使用者,不建立家目錄,並且不能登入 [root@ansible ~]# ansible 192.168.1.31 -m user -a 'name=apache shell=/bin/nologin uid=1003 create_home=no' [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'id apache && tail -1 /etc/passwd' 192.168.1.31 | CHANGED | rc=0 >> uid=1003(apache) gid=1003(apache) 組=1003(apache) apache:x:1003:1003::/home/apache:/bin/nologin # 刪除使用者 [root@ansible ~]# ansible 192.168.1.31 -m user -a 'name=apache state=absent' # 刪除使用者並刪除家目錄 [root@ansible ~]# ansible 192.168.1.31 -m user -a 'name=mysql state=absent remove=yes'
group#
用於建立組,當建立使用者時如果需要指定組,組不存在的話就可以通過group
先建立組
# 檢視模組引數 [root@ansible ~]# ansible-doc -s group name #指定組的名字 gid #指定組的gid state: absent #刪除組 present #建立組(預設的狀態) # 建立組 [root@ansible ~]# ansible 192.168.1.31 -m group -a 'name=www' # 建立組並指定gid [root@ansible ~]# ansible 192.168.1.31 -m group -a 'name=www1 gid=1005' # 刪除組 [root@ansible ~]# ansible 192.168.1.31 -m group -a 'name=www1 state=absent'
軟體包相關的模組#
yum#
用於對軟體包的管理,下載、安裝、解除安裝、升級等操作
# 檢視模組引數 [root@ansible ~]# ansible-doc -s yum name #指定要操作的軟體包名字 download_dir #指定下載軟體包的存放路徑,需要配合download_only一起使用 download_only #只下載軟體包,而不進行安裝,和yum --downloadonly一樣 list: installed #列出所有已安裝的軟體包 updates #列出所有可以更新的軟體包 repos #列出所有的yum倉庫 state: installed, present #安裝軟體包(兩者任選其一都可以) removed, absent #解除安裝軟體包 latest #安裝最新軟體包 # 列出所有已安裝的軟體包 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'list=installed' # 列出所有可更新的軟體包 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'list=updates' #列出所有的yum倉庫 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'list=repos' #只下載軟體包併到指定目錄下 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'name=httpd download_only=yes download_dir=/tmp' #安裝軟體包 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'name=httpd state=installed' #解除安裝軟體包 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'name=httpd state=removed' #安裝包組,類似yum groupinstall 'Development Tools' [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'name="@Development Tools" state=installed'
pip#
用於安裝python中的包
# 檢視模組引數 [root@ansible ~]# ansible-doc -s pip # 使用pip時,需要保證被管理機器上有python-pip軟體包 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'name=python-pip' # 安裝pip包 [root@ansible ~]# ansible 192.168.1.31 -m pip -a 'name=flask'
service#
服務模組,用於對服務進行管理,服務的啟動、關閉、開機自啟等
# 檢視模組引數 [root@ansible ~]# ansible-doc -s service name #指定需要管理的服務名 enabled #指定是否開機自啟動 state: #指定服務狀態 started #啟動服務 stopped #停止服務 restarted #重啟服務 reloaded #過載服務 # 啟動服務,並設定開機自啟動 [root@ansible ~]# ansible 192.168.1.31 -m service -a 'name=crond state=started enabled=yes'
計劃任務相關的模組#
cron#
用於指定計劃任務,和crontab -e
一樣
# 檢視模組引數 [root@ansible ~]# ansible-doc -s cron job #指定需要執行的任務 minute #分鐘 hour #小時 day #天 month #月 weekday #周 name #對計劃任務進行描述 state: absetn #刪除計劃任務 # 建立一個計劃任務,並描述是幹嘛用的 [root@ansible ~]# ansible 192.168.1.31 -m cron -a "name='這是一個測試的計劃任務' minute=* hour=* day=* month=* weekday=* job='/bin/bash /root/test.sh'" [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'crontab -l' 192.168.1.31 | CHANGED | rc=0 >> #Ansible: 這是一個測試的計劃任務 * * * * * /bin/bash /root/test.sh # 建立一個沒有帶描述的計劃任務 [root@ansible ~]# ansible 192.168.1.31 -m cron -a "job='/bin/sh /root/test.sh'" # 刪除計劃任務 [root@ansible ~]# ansible 192.168.1.31 -m cron -a "name='None' job='/bin/sh /root/test.sh' state=absent"
系統資訊相關的模組#
setup#
用於獲取系統資訊的一個模組
# 檢視模組引數 [root@ansible ~]# ansible-doc -s setup # 檢視系統所有資訊 [root@ansible ~]# ansible 192.168.1.31 -m setup # filter 對系統資訊進行過濾 [root@ansible ~]# ansible 192.168.1.31 -m setup -a 'filter=ansible_all_ipv4_addresses' # 常用的過濾選項 ansible_all_ipv4_addresses 所有的ipv4地址 ansible_all_ipv6_addresses 所有的ipv6地址 ansible_architecture 系統的架構 ansible_date_time 系統時間 ansible_default_ipv4 系統的預設ipv4地址 ansible_distribution 系統名稱 ansible_distribution_file_variety 系統的家族 ansible_distribution_major_version 系統的版本 ansible_domain 系統所在的域 ansible_fqdn 系統的主機名 ansible_hostname 系統的主機名,簡寫 ansible_os_family 系統的家族 ansible_processor_cores cpu的核數 ansible_processor_count cpu的顆數 ansible_processor_vcpus cpu的個數