Ansible - 命令講解


Ansible命令

命令集

# 以下所有命令均可使用`-h`參數獲取幫助信息
ansible               # Define and run a single task 'playbook' against a set of hosts  # 常用命令
ansible-config        # View ansible configuration.
ansible-console       # REPL console for executing Ansible tasks.  # 交互式工具,相當linux中的shell,不常用
ansible-doc           # plugin documentation tool  # 查詢模塊的文檔說明,類似linux中的man,常用命令
ansible-galaxy        # Perform various Role and Collection related operations.
ansible-inventory     # Show Ansible inventory information, by default it uses the inventory script JSON format
ansible-playbook      # Runs Ansible playbooks, executing the defined tasks on the targeted hosts.  # 執行系列任務,類似linux中的sh,常用命令
ansible-pull          # pulls playbooks from a VCS repo and executes them for the local host
ansible-vault         # encryption/decryption utility for Ansible data files

常用命令集

# 常用命令集

ansible
# Ansible指令的核心部分,主要用於執行ad-hoc命令,也就是單條命令
# 默認使用command模塊,默認的使用模塊可以在ansible.cfg中進行定義

ansible-doc
# 用於查看模塊信息,
# 參數 -l 列出可用的插件
# 參數 -s 用於查看指定模塊的用法,例如“ansible-doc -s shell”

ansible-playbook
# 讀取playbook文件內容並執行定義的任務
# 對於固化的需求通常採用playbook方式定義和實現

ansible-vault
# 主要應用於包含敏感信息的場景,可以加密和解密敏感信息
# 經過加密後的playbook,編輯時需要輸入事先設定的密碼才能打開
# 使用 --ask-vault-pass 參數執行加密後的playbook文件

ansible-config
# 查看ansible的配置
# 參數 list 打印所有配置參數,參數 view 查看配置文件
# 結合 grep 使用可以獲取具體配置信息,例如“ansible-config view |grep -v "#" |grep host_key_checking”

ad-hoc命令

# ansible <host-pattern> [-f forks] [-m module_name] [-a args]
# <host-pattern>      命令生效的主機: <inventory_group_name>/<inventory_host_ip>/all 

# 常用選項  通過 ansible -h 命令獲取詳細信息
-v                    # 輸出詳細執行過程信息  verbose mode (-vvv for more, -vvvv to enable connection debugging)
-i inventory_file     # 指定inventory文件,默認爲/etc/ansible/hosts
-f forks              # 指定併發線程數(一次處理多少個主機),默認5個線程
-m module_name        # 指定執行使用的模塊
-M directory          # 指定模塊存放路徑,默認爲/usr/share/ansible
-a args               # 指定模塊參數
-l hosts_ip           # 限定主機(只在限定主機上執行任務)

--become-method BECOME_METHOD    # 提權方法,默認爲sudo,一般爲su
--become-user BECOME_USER        # 切換用戶,默認爲root
-K, --ask-become-pass            # 請求提權密碼

--list-hosts        # 列出此次的主機列表,但不做任何改變
--syntax-check      # 語法檢查
-C, --check         # 檢驗可能的錯誤,但不做任何改變


# 主機名文件保留
- "-t"或"--tree"選項可以將ansible的執行結果按主機名保存在指定目錄下的文件中
- 使用"-t"選項可以將第一次執行的結果按主機名保存在文件中, 此後即使不使用"-t"選項也能提升執行速度


# 命令示例
ansible 192.168.56.3 -a 'hostname'  # 默認使用command模塊(不支持變量、管道等)
ansible 192.168.56.3 -m command -a 'hostname'
ansible Test --list  # 列出Test組所有的主機列表

ansible Test:Test2 -m ping  # 通過ping模塊檢查Test組和Test2組的主機是否存活
ansible Test:!Test2 -m ping  # 在Test組但不在Test2組的主機
ansible Test:&Test2 -m ping  # 同時存在Test組和Test2組的主機
ansible Tes* -m ping  # 組名開頭爲Tes的主機

ansible all -m shell -a 'hostname && date'  # 通過shell模塊在所有主機試行shell命令
ansible all -f 10 -m shell -a 'hostname && date;cat /etc/system-release'  # shell模塊支持變量、管道、if判斷等複雜命令

部分參數如果不指定,將採用ansible.cfg中的設置值,或者採用原始默認值。

ad-hoc常用命令

# ping        測試指定主機連接性
ansible ta -m ping
ansible -i /etc/ansible/hosts ta -m ping  #  指定inventory文件

# command     在指定主機上執行命令(默認模塊),不支持變量、管道、重定向等shell特性
ansible ta -m command -a "chdir=/home/ ls ./"  # 在遠程主機上切換到home目錄執行ls命令 

# shell       在指定主機上執行命令或運行腳本,打開遠程主機的shell進程的一個子shell運行命令,支持shell的變量、管道、重定向等特性
ansible ta -m shell -a 'hostname && date;cat /etc/system-release'  # 支持變量、管道、if判斷等複雜命令
ansible ta -m shell -a "echo 'this is a test' > test.txt" -C  # 驗證可能的錯誤,但不做任何改變
ansible ta -m shell -a 'echo "123456"|passwd --stdin root' -K  # 更改root密碼

# script      調用本地腳本在遠程主機執行,但只能執行腳本,不能調用其他指令,且不支持管道命令
ansible ta -m script -a "/home/vipxf/test.sh"
ansible ta -m script -a "removes=/home/vipxf/test.sh /home/vipxf/test2.sh" -o  # 如果遠程主機上test.sh文件存在就執行本地腳本test2.sh,否則就不執行
ansible ta -m script -a "creates=/home/vipxf/test.sh /home/vipxf/test2.sh" -o  # 如果遠程主機上test.sh文件存在就不執行本地腳本test2.sh,否則就執行

# raw         類似shell,支持管道命令,可用於沒有python環境的遠程主機
ansible ta -m raw -a "cd /home;pwd"

# stat        獲取文件信息
ansible ta -m stat -a "path=/etc/ansible/hosts" 

# setup       系統信息,收集facts
ansible ta -m setup
ansible ta -m setup | grep "xxx"  # 獲取主機所有facts然後結合grep命令過濾
ansible ta -m setup -a "filter=ansible_os_family"  # 通過filter參數來查看指定信息
ansible ta -m setup -a 'filter="*mem*"' --tree ./facts  # 通過filter參數查看指定信息並按主機名保存到facts目錄

# copy        複製文件到遠程主機的指定位置
ansible ta -m copy -a "src=/etc/hosts dest=~/test.hosts mode=664 owner=vipxf group=vipxf"  # 複製本地文件到遠程主機並設置權限
ansible ta -m copy -a "content="this is a test" dest=~/test.hosts mode=664 owner=vipxf group=vipxf"  # 在遠程主機創建指定內容文件並設置權限

# fetch       複製指定主機的文件到本地
ansible ta -m fetch -a "src=~/testfile.txt dest=~/"  # 將在指定目錄下創建對應IP文件夾來存放文件,保留了原目錄結構

# get_url     在遠程主機下載網絡文件,支持文件校驗
ansible ta -m get_url -a "url=http://172.20.5.3/pub/soft/docker/docker-19.03.9.tgz dest=~/ mode=655 owner=vipxf"  # 下載文件到遠程主機指定目錄並設置權限

# file        設置文件屬性
ansible ta -m file -a "path=~/testfile.txt mode=755 owner=vipxf group=vipxf"  # 更新文件屬性
ansible ta -m file -a "path=~/testfile.txt state=touch mode=644"  # 創建文件
ansible ta -m file -a "path=~/testfile.txt state=absent"  # 刪除文件
ansible ta -m file -a "path=~/testdir mode=755 state=directory"  # 創建目錄

# cron        設置計劃任務
ansible ta -m cron -a "minute=0 hour=22 job='/home/vipxf/testcron.sh' name='test cron'"  # 創建計劃任務
ansible ta -m cron -a "name='test cron' state=absent"  # 刪除計劃任務

# group       管理用戶組
ansible ta -b --become-user root --become-method su -m group -a "name=test system=yes" --ask-become-pass  # 創建用戶組
ansible ta -b --become-user root --become-method su -m group -a "name=test state=absent" --ask-become-pass  # 刪除用戶組

# user        管理用戶
ansible ta -b --become-user root --become-method su -m user -a "name=test system=yes group=test password=pw@123" -K  # 創建用戶
ansible ta -b --become-user root --become-method su -m user -a "name=test state=absent force=yes" -K  # 刪除用戶

# service     管理服務
ansible ta -b --become-user root --become-method su -m service -a "name=sshd enabled=yes state=started" -K  # 啓動httpd服務並且不設置開機啓動
ansible ta -b --become-user root --become-method su -m service -a "name=sshd state=stopped" -K  # 停止httpd服務並且不設置開機啓動

# yum         管理程序包
ansible ta -b --become-user root --become-method su -m yum -a "name=httpd state=present" -K  # 安裝包,也可以將state設爲installed
ansible ta -b --become-user root --become-method su -m yum -a "name=httpd state=latest disable_gpg_check=no" -K  # 關閉完整性校驗,安裝最新版包
ansible ta -b --become-user root --become-method su -m yum -a "name=httpd state=absent" -K  # 卸載包,也可以將state設爲removed
ansible ta -b --become-user root --become-method su -m yum -a "name=nginx state=latest enablerepo=local" -K  # 啓用本地yum源安裝最新版軟件包

# hostname    管理主機名稱
ansible ta -b --become-user root --become-method su -m hostname -a "name=test" --ask-become-pass  # 更改遠程主機名

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章