一千萬個為什麽

搜索

有關Ansible連接到遠程主機的問題



我想問一些關於Ansible工作的問題。

1 - Which shell Ansible uses when logging to remote host ?

https://askubuntu。com/questions/ 937354 /軌道的登錄制-通非交互式殼/ 937716?n要麽edirect = 1#comment1486867_937716

2 - If I have 10 tasks in a playbook, then ansible makes connection 10 times to remote host 要麽 uses a single connection to perf要麽m all these 10 tasks ?

3 - If I am running 10 tasks on 2 hosts,

它將在主機1上,然後在主機2上執行第一項任務 然後在主機1上然後在主機2上執行第二項任務, 。。 。如此,

那麽,這裏的連接是否仍然存在於兩臺主機上?

要麽

它是否首先連接到主機1,運行任務1,然後斷開連接,然後連接到主機2,運行任務1,然後斷開連接?

轉載註明原文: 有關Ansible連接到遠程主機的問題

一共有 3 個回答:

無法回答1(我一直認為它只是使用默認的/ bin/sh,除非另有規定?)。

  1. It will make the connection more than 10 times. A single task will typically have an SCP or SFTP connection to copy the taskfile that'll get remotely executed, then another connection to trigger the script. You can monitor this happening by running your playbook with -vvv, such as:

    ansible-playbook deploy_app.yml -u maplebird -vvv
    

    3rd-level verbosity shows all connections to client.

  2. By default, Ansible will execute tasks concurrently on all hosts up to the max configured number of forks. So, it'll run task 1 on both hosts, then it'll run task 2 on both hosts, etc. Forks are defined in ansible.cfg, and default to 5. Change this variable to a higher number or comment it out:

    forks = 5
    

    Optionally, you can also do a rolling batch when running playbooks by specifying the serial option in the playbook. This will only concurrently execute tasks for however many hosts you've defined.

    Say you're running a playbook against 5 hosts, and have the serial option set. With serial = 1, it will run the full playbook 1 host at a time. With serial = 2, it will run hosts 1 & 2, then hosts 2 & 3, then host 5. Example:

    name: deploy to all webservers
    hosts: webservers
    serial: 2
    roles:
       - deploy_application
    

更多閱讀:

串行(滾動)劇本運行:

福克斯:

希望這可以幫助。

(1)Ansible使用哪個shell?

Ansible默認使用/bin/sh 。在包括RHEL/CentOS的許多* nix系統上,/bin/sh 是 - 然而,在Ubuntu/Debian上它是 dash ,這更基本。

讓Ansible使用bash

應該可以使用 executable =/bin/bash 配置選項 =“nofollow noreferrer”> ansible.cfg ,並可能設置 ansible_shell_type 在清單中。

但實際上(在Ansible 2.2.2及更高版本中)我和其他發現在 ansible.cfg 中設置 executable 不起作用(無論是否在變為模式)。

一種行之有效的方法,但是每個任務意味著更多的代碼,就是寫這個:

- shell:
    echo hello from $0
  args:
    executable: /bin/bash

這應該說來自/ bin/bash 的 hello,表明shell是正確的。

如果您需要使用 rvmrbenvpyenv 或類似工具 require 以前的 source〜/ .bash_profile (與交互式shell一樣),您需要使用包裝腳本在shell任務中調用bash

  1. 默認情況下,Shell模塊使用/ bin/sh,但您可以在全局配置中設置可執行文件,或者在Playbook本地設置可執行文件。

http://docs.ansible.com/ansible/latest/shell_module.html