ansible 命令使用說明

2021-09-27 05:05:58 字數 4346 閱讀 8485

1. ansible命令的使用說明

ansible 主機或組-m 模組名-a '模組引數' ansible引數

表示呼叫什麼模組,使用模組的那些引數

• 主機和組,是在/etc/ansible/hosts 裡進行指定的部分,當然動態inventory 使用的是指令碼從外部應用裡獲取的主機;

• 模組名,可以通過ansible-doc -l 檢視目前安裝的模組,預設不指定時,使用的是command模組,具體可以檢視/etc/ansible/ansible.cfg 的「#module_name = command 」 部分,預設模組可以在該配置檔案中進行修改;

• 模組引數,可以通過 「ansible-doc -s 模組名」 檢視具體的用法及後面的引數;

• ansible引數,可以通過ansible命令的幫助資訊裡檢視到,這裡有很多引數可以供選擇,如是否需要輸入密碼、是否sudo等。

2. 命令執行模組

命令執行模組包含如下 四個模組:

• command模組:該模組通過-a跟上要執行的命令可以直接執行,不過命令裡如果有帶有如下字元部分則執行不成功 「 "<", ">", "|", "&" ;

• shell 模組:用法基本和command一樣,不過其是通過/bin/sh進行執行,所以shell 模組可以執行任何命令,就像在本機執行一樣;

• raw模組:用法和shell 模組一樣 ,其也可以執行任意命令,就像在本機執行一樣;

• script模組:其是將管理端的shell 在被管理主機上執行,其原理是先將shell 複製到遠端主機,再在遠端主機上執行,原理類似於raw模組。

注:raw模組和comand、shell 模組不同的是其沒有chdir、creates、removes引數,chdir引數的作用就是先切到chdir指定的目錄後,再執行後面的命令,這在後面很多模組裡都會有該引數 。

command模組包含如下選項:

• creates:乙個檔名,當該檔案存在,則該命令不執行 

• free_form:要執行的linux指令 

• chdir:在執行指令之前,先切換到該指定的目錄 

• removes:乙個檔名,當該檔案不存在,則該選項不執行

• executable:切換shell來執行指令,該執行路徑必須是乙個絕對路徑

3. ansible -h詳解

[root@localhost ~]# ansible -h

usage: ansible [options]

options:

-a module_args, --args=module_args    #模組的引數,如果執行預設command的模組,即是命令引數,如:「date」,"pwd"等等

module arguments    # 模組引數

-k, --ask-pass        ask for ssh password     #登入密碼,提示輸入ssh密碼而不是假設基於金鑰的驗證

--ask-su-pass         ask for su password      #su切換密碼

-k, --ask-sudo-pass   ask for sudo password    # 提示密碼使用sudo,sudo表示提權操作

--ask-vault-pass      ask for vault password

-b seconds, --background=seconds        # 後台執行超時時間

run asynchronously, failing after x seconds

(default=n/a)

-c, --check           don't make any changes; instead, try to predict some    #只是測試一下會改變什麼內容,不會真正去執行;相反,試圖**一些可能發生的變化

of the changes that may occur

-c connection, --connection=connection       #連線型別使用。可能的選項是paramiko(ssh),ssh和地方。當地主要是用於crontab或啟動。

connection type to use (default=smart)

-f forks, --forks=forks        #並行任務數。num被指定為乙個整數,預設是5

specify number of parallel processes to use

(default=5)

-h, --help            show this help message and exit      # 開啟幫助文件api

-i inventory, --inventory-file=inventory         #指定庫存主機檔案的路徑,預設為/etc/ansible/hosts

specify inventory host file

(default=/etc/ansible/hosts)

-l subset, --limit=subset          #進一步限制所選主機/組模式  --limit=192.168.91.135 只對這個ip執行

further limit selected hosts to an additional pattern

--list-hosts          outputs a list of matching hosts; does not execute

anything else

-m module_name, --module-name=module_name         # 執行模組的名字,預設使用 command 模組,所以如果是只執行單一命令可以不用 -m引數

module name to execute (default=command)

-m module_path, --module-path=module_path        #要執行的模組的路徑,預設為/usr/share/ansible/

specify path(s) to module library

(default=/usr/share/ansible/)

-o, --one-line        condense output          # 壓縮輸出,摘要輸出.嘗試一切都在一行上輸出。

-p poll_interval, --poll=poll_interval        #調查背景工作每隔數秒。需要- b

set the poll interval if using -b (default=15)

--private-key=private_key_file           # 私鑰路徑,使用這個檔案來驗證連線

use this file to authenticate the connection

-s, --su              run operations with su       #用 su 命令

-r su_user, --su-user=su_user         # 指定su的使用者,預設是root使用者

run operations with su as this user (default=root)

-s, --sudo            run operations with sudo (nopasswd)    

-u sudo_user, --sudo-user=sudo_user    #sudo到哪個使用者,預設為 root  

desired sudo user (default=root)

-t timeout, --timeout=timeout         #指定ssh預設超時時間,  預設是10s

override the ssh timeout in seconds (default=10)

-t tree, --tree=tree  log output to this directory        #將日誌內容儲存在該輸出目錄,結果儲存在乙個檔案中在每台主機上。

-u remote_user, --user=remote_user        #遠端使用者, 預設是root使用者

connect as this user (default=root)

--vault-password-file=vault_password_file  

vault password file

-v, --verbose         verbose mode (-vvv for more, -vvvv to enable       #詳細資訊

connection debugging)

--version             show program's version number and exit        # 輸出ansible的版本

linux diff 命令使用說明

1 diff命令的功能 linux中diff命令的功能為逐行比較兩個文字檔案,列出其不同之處。它對給出的檔案進行系統的檢查,並顯示出兩個檔案中所有不同的行,不要求事先對檔案進行排序。2 語法 diff options file1 file2 該命令告訴使用者,為了使兩個檔案file1和file2一致...

scp命令使用說明

scp是linux常用的命令,它可以方便的進行檔案的傳輸。利用scp進行檔案傳輸時,通過指定的加密演算法還可以提公升傳輸速度。scp secure copy 允許不同的主機之間進行檔案傳輸。scp使用ssh進行資料的傳輸,提供了和ssh相同的身份認證和同一級別的安全策略。從本地複製單個檔案 scp ...

Trap命令使用說明

trap命令用於指定在接收到訊號後將要採取的行動。trap命令的一種常見用途是在指令碼程式被中斷時完成清理工作。可以使用trap l來檢視具體訊號列表。trap捕捉到訊號之後,可以有三種響應方式 trap commands signal listtrap signal listtrap signal...