【Ansible學(xué)習(xí)】- Ad-Hoc命令參數(shù)使用說明

Ansible 常用參數(shù)說明

使用ansible -h命令就可以列出所有的命令參數(shù),下面列舉了常用的一些參數(shù)。

option 說明
-v 輸出詳細(xì)信息
-i 指定inventory文件,默認(rèn)使用/etc/ansible/hosts
-f fork的進(jìn)程個(gè)數(shù),默認(rèn)為5
--list-hosts 列出主機(jī)列表,并不會(huì)執(zhí)行其他操作
-private-key=xxx 指定ssh連接使用的文件
-m 指定module,默認(rèn)為command
-a 指定module的參數(shù)
-o 精簡輸出內(nèi)容
-k 提示輸入密碼
-K 提示輸入sudo密碼,與-sudo一起使用
-T 設(shè)置連接超時(shí)時(shí)間
-B 設(shè)置后臺(tái)運(yùn)行并設(shè)置超時(shí)時(shí)長

使用示例

-i示例

[root@centos7 ~]# cat hosts
[apache]
172.20.21.121
172.20.21.123

[nginx]
172.20.21.120
172.20.21.121

[servers:children]
apache
nginx

[servers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123456'

[root@centos7 ~]# ansible -i hosts apache --list-hosts
  hosts (2):
    172.20.21.121
    172.20.21.123

[root@centos7 ~]# ansible -i hosts apache -m ping -o
172.20.21.123 | SUCCESS => {"changed": false, "ping": "pong"}
172.20.21.121 | SUCCESS => {"changed": false, "ping": "pong"}

-k示例

不指定module,默認(rèn)將使用command。

[root@centos7 ~]# ansible -i hosts apache -k -o -a "echo hello"
SSH password: 
172.20.21.123 | SUCCESS | rc=0 | (stdout) hello
172.20.21.121 | SUCCESS | rc=0 | (stdout) hello

ansible-doc -s取得更詳細(xì)信息

希望知道更加詳細(xì)的module的信息,最好的方法是使用ansible自帶的ansible-doc-s選項(xiàng)。

[root@centos7 ~]# ansible-doc -s raw
- name: Executes a low-down and dirty SSH command
  raw:
      executable:            # change the shell used to execute the command. Should be an absolute path
                               to the executable. when using privilege
                               escalation (`become'), a default shell
                               will be assigned if one is not provided as
                               privilege escalation requires a shell.
      free_form:             # (required) the raw module takes a free form command to run. There is no
                               parameter actually named 'free form'; see
                               the examples!

[root@centos7 ~]# ansible-doc -s expect
- name: Executes a command and responds to prompts.
  expect:
      chdir:                 # Change into this directory before running the command.
      command:               # (required) The command module takes command to run.
      creates:               # A filename, when it already exists, this step will *not* be run.
      echo:                  # Whether or not to echo out your response strings.
      removes:               # A filename, when it does not exist, this step will *not* be run.
      responses:             # (required) Mapping of expected string/regex and string to respond with.
                               If the response is a list, successive
                               matches return successive responses. List
                               functionality is new in 2.1.
      timeout:               # Amount of time in seconds to wait for the expected strings.

所有命令參數(shù)

[root@localhost ~]# ansible -h
Usage: ansible <host-pattern> [options]

Options:
  -a MODULE_ARGS, --args=MODULE_ARGS    模塊的參數(shù),如果執(zhí)行默認(rèn)COMMAND的模塊,即是命令參數(shù),如:“date”,"pwd"等等
                        module arguments    模塊參數(shù)
  -k, --ask-pass        ask for SSH password  登錄密碼,提示輸入SSH密碼而不是假設(shè)基于密鑰的驗(yàn)證
  --ask-su-pass         ask for su password    su切換密碼
  -K, --ask-sudo-pass   ask for sudo password  提示密碼使用sudo,sudo表示提權(quán)操作
  --ask-vault-pass      ask for vault password
  -B SECONDS, --background=SECONDS     后臺(tái)運(yùn)行超時(shí)時(shí)間
                        run asynchronously, failing after X seconds
                        (default=N/A)
  -C, --check           don't make any changes; instead, try to predict some    只是測試一下會(huì)改變什么內(nèi)容,不會(huì)真正去執(zhí)行;相反,試圖預(yù)測一些可能發(fā)生的變化
                        of the changes that may occur
  -c CONNECTION, --connection=CONNECTION   連接類型使用??赡艿倪x項(xiàng)是paramiko(SSH),SSH和地方。當(dāng)?shù)刂饕怯糜赾rontab或啟動(dòng)。
                        connection type to use (default=smart)
  -f FORKS, --forks=FORKS   并行任務(wù)數(shù)。NUM被指定為一個(gè)整數(shù),默認(rèn)是5
                        specify number of parallel processes to use
                        (default=5)
  -h, --help            show this help message and exit   打開幫助文檔API
  -i INVENTORY, --inventory-file=INVENTORY    指定庫存主機(jī)文件的路徑,默認(rèn)為/etc/ansible/hosts
                        specify inventory host file
                        (default=/etc/ansible/hosts)
  -l SUBSET, --limit=SUBSET    進(jìn)一步限制所選主機(jī)/組模式  --limit=192.168.91.135 只對這個(gè)ip執(zhí)行
                        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   執(zhí)行模塊的名字,默認(rèn)使用 command 模塊,所以如果是只執(zhí)行單一命令可以不用 -m參數(shù)
                        module name to execute (default=command)
  -M MODULE_PATH, --module-path=MODULE_PATH    要執(zhí)行的模塊的路徑,默認(rèn)為/usr/share/ansible/
                        specify path(s) to module library
                        (default=/usr/share/ansible/)
  -o, --one-line        condense output      壓縮輸出,摘要輸出.嘗試一切都在一行上輸出。
  -P POLL_INTERVAL, --poll=POLL_INTERVAL    調(diào)查背景工作每隔數(shù)秒。需要- b
                        set the poll interval if using -B (default=15)
  --private-key=PRIVATE_KEY_FILE    私鑰路徑,使用這個(gè)文件來驗(yàn)證連接
                        use this file to authenticate the connection
  -S, --su              run operations with su    用 su 命令
  -R SU_USER, --su-user=SU_USER      指定SU的用戶,默認(rèn)是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到哪個(gè)用戶,默認(rèn)為 root  
                        desired sudo user (default=root)
  -T TIMEOUT, --timeout=TIMEOUT    指定SSH默認(rèn)超時(shí)時(shí)間,  默認(rèn)是10S
                        override the SSH timeout in seconds (default=10)
  -t TREE, --tree=TREE  log output to this directory     將日志內(nèi)容保存在該輸出目錄,結(jié)果保存在一個(gè)文件中在每臺(tái)主機(jī)上。
  -u REMOTE_USER, --user=REMOTE_USER    遠(yuǎn)程用戶, 默認(rèn)是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    詳細(xì)信息
                        connection debugging)
  --version             show program's version number and exit   輸出ansible的版本

如果覺得有用,歡迎關(guān)注我的微信,有問題可以直接交流:

你的關(guān)注是對我最大的鼓勵(lì)!
你的關(guān)注是對我最大的鼓勵(lì)!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,564評(píng)論 19 139
  • ansible 系統(tǒng)架構(gòu) ansible簡介ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,ansible是一個(gè)配置管理和...
    運(yùn)維阿文閱讀 9,841評(píng)論 1 53
  • 1)安裝2)常用模塊3)inventory4)playbook(role\tag\template)5) yaml...
    秦記閱讀 4,596評(píng)論 2 5
  • 第3章 Ansible Ad-Hoc命令集 在前面章節(jié)中,為大家介紹了掌握Ansible所必須了解的基礎(chǔ),如變量命...
    stanleylst閱讀 684評(píng)論 0 0
  • 曾經(jīng)的我們,哭著哭著就笑了,而如今,笑著笑著卻哭了…… 十年前,我們還不懂距離,不懂除卻親情與友情的愛情,總覺得,...
    情戀風(fēng)塵閱讀 499評(píng)論 0 0

友情鏈接更多精彩內(nèi)容