Answers: <p><em>(1) Which shell does Ansible use?</em></p>
  
  <p>Ansible uses <code>/bin/sh</code> by default. On many *nix systems including RHEL / CentOS, <code>/bin/sh</code> is <code>bash</code> - however, <a href="https://wiki.ubuntu.com/DashAsBinSh" rel="nofollow noreferrer">on Ubuntu/Debian it's <code>dash</code></a> which is much more basic.</p>
  
  <h3>Making Ansible use bash</h3>
  
  <p>It should be possible to change this with the <code>executable = /bin/bash</code> config option <a href="http://docs.ansible.com/ansible/latest/intro_configuration.html#executable" rel="nofollow noreferrer">in ansible.cfg</a> and possibly also setting <a href="http://docs.ansible.com/ansible/latest/intro_inventory.html" rel="nofollow noreferrer">ansible_shell_type</a> in the inventory. </p>
  
  <p>But in practice (on Ansible 2.2.2 and higher) I and <a href="https://github.com/ansible/ansible/issues/1238#issuecomment-200362771" rel="nofollow noreferrer">others</a> have found that setting <code>executable</code> in <code>ansible.cfg</code> doesn't work (whether in <code>become</code> mode or not).</p>
  
  <p>An approach that works well, but does mean a bit more code per task, is to write this:</p>
  
  <pre><code>- shell:
      echo hello from $0
    args:
      executable: /bin/bash
  </code></pre>
  
  <p>This should say <code>hello from /bin/bash</code>, indicating the shell is correct.</p>
  
  <p>If you need to use <code>rvm</code>, <code>rbenv</code>, <code>pyenv</code> or similar tools that <a href="https://github.com/rbenv/rbenv#installation" rel="nofollow noreferrer">require</a> a previous <code>source ~/.bash_profile</code> (as with an interactive shell), you will need to <a href="https://github.com/ansible/ansible/issues/1238#issuecomment-57797275" rel="nofollow noreferrer">use a wrapper script</a> or <a href="https://github.com/ansible/ansible/issues/4854#issuecomment-182582413" rel="nofollow noreferrer">invoke bash within the shell task</a>.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-07-27T15:39:15.040Z
Answers: 2017-07-28T07:31:06.933Z
Answers: 2017-07-28T07:31:06.933Z
Answers: RichVel
Answers: <ol>
  <li>Shell module use /bin/sh by default, but you can set executable in global config or locally in playbook.</li>
  </ol>
  
  <p><a href="http://docs.ansible.com/ansible/latest/shell_module.html" rel="nofollow noreferrer">http://docs.ansible.com/ansible/latest/shell_module.html</a></p>
  
Answers: CC BY-SA 3.0
Answers: 2017-07-25T09:41:57.107Z
Answers: 2017-07-25T09:41:57.107Z
Answers: Quarind
Answers: <pre>
  /*sudo apt-get update
  sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg -y
  
  curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg REDIRECT /dev/null
  ** Replace REDIRECT with chevron for greater than (YT doesn't like chevrons!) ** 
  
  AZ_REPO=$(lsb_release -cs)
  echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list
  sudo apt-get update
  sudo apt-get install azure-cli
  sudo apt install ansible
  sudo apt install python3-pip
  pip3 install ansible[azure] --user
  az login
  az ad sp create-for-rbac --name ansible
  az role assignment create --assignee appID --role Contributor
  az account show --query '{tenantId:tenantId,subscriptionid:id}';
  az ad sp list --display-name ansible --query '{clientId:[0].appId}'
  vi ~/.azure/credentials
  ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth"
  ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth state=absent"
  mkdir azure
  cd azure
  vi azure_vm.yml
  ansible localhost -m azure_rm_resourcegroup -a "name=Testing location=uksouth"
  ansible-playbook azure_vm.yml
  cat azure_vm.yml
  ssh youtubedemo@IP_ADDRESS*/
  </pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-06-26T05:24:40.163Z
Answers: 2022-06-26T06:26:21.083Z
Answers: 2022-06-26T06:26:21.083Z
Answers: Guest
Answers: <pre><code>Sudo apt-get update
  sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg -y
  
  curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg REDIRECT /dev/null
  </code></pre>
  <p>Replace REDIRECT with chevron for greater than (YT doesn't like chevrons!)</p>
  <pre>
  AZ_REPO=$(lsb_release -cs)
  
  echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list
  
  sudo apt-get update
  sudo apt-get install azure-cli
  sudo apt install ansible
  sudo apt install python3-pip
  pip3 install ansible[azure] --user
  az login
  az ad sp create-for-rbac --name ansible
  
  az role assignment create --assignee appID --role Contributor
  
  az account show --query '{tenantId:tenantId,subscriptionid:id}';
  
  az ad sp list --display-name ansible --query '{clientId:[0].appId}'
  
  vi ~/.azure/credentials
  
  ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth"
  
  ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth state=absent"
  
  mkdir azure
  cd azure
  vi azure_vm.yml
  ansible localhost -m azure_rm_resourcegroup -a "name=Testing location=uksouth"
  
  ansible-playbook azure_vm.yml
  
  cat azure_vm.yml
  
  ssh youtubedemo@IP_ADDRESS
  </pre>
  <p>Links:</p>
  <p><a href="https://azure.microsoft.com/en-gb/free/?ref=microsoft.com" rel="nofollow noreferrer">https://azure.microsoft.com/en-gb/free/?ref=microsoft.com</a></p>
  <p><a href="https://docs.microsoft.com/en-us/azure/developer/ansible/install-on-linux-vm" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/developer/ansible/install-on-linux-vm</a></p>
  <p><a href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt</a></p>
  <p><a href="https://docs.microsoft.com/en-us/azure/developer/ansible/create-ansible-service-principal" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/developer/ansible/create-ansible-service-principal</a></p>
  <p><a href="https://docs.ansible.com/ansible/latest/scenario_guides/guide_azure.html" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/scenario_guides/guide_azure.html</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2022-06-26T05:54:54.543Z
Answers: 2022-06-26T06:25:23.773Z
Answers: 2022-06-26T06:25:23.773Z
Answers: Guest
Answers: <p>Can't answer 1 (I always thought it just uses the default /bin/sh unless specified otherwise?).</p>
  
  <ol start="2">
  <li><p>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:</p>
  
  <pre><code>ansible-playbook deploy_app.yml -u maplebird -vvv
  </code></pre>
  
  <p>3rd-level verbosity shows all connections to client.</p></li>
  <li><p>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:</p>
  
  <pre><code>forks = 5
  </code></pre>
  
  <p>Optionally, you can also do a rolling batch when running playbooks by specifying the <code>serial</code> option in the playbook.  This will only concurrently execute tasks for however many hosts you've defined.</p>
  
  <p>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 &amp; 2, then hosts 2 &amp; 3, then host 5.  Example:</p>
  
  <pre><code>name: deploy to all webservers
  hosts: webservers
  serial: 2
  roles:
     - deploy_application
  </code></pre></li>
  </ol>
  
  <p>More reading:</p>
  
  <p>Serial (rolling) playbook runs:</p>
  
  <ul>
  <li><a href="http://docs.ansible.com/ansible/latest/playbooks_delegation.html#rolling-update-batch-size" rel="noreferrer">http://docs.ansible.com/ansible/latest/playbooks_delegation.html#rolling-update-batch-size</a></li>
  </ul>
  
  <p>Forks:</p>
  
  <ul>
  <li><a href="http://docs.ansible.com/ansible/latest/intro_configuration.html#forks" rel="noreferrer">http://docs.ansible.com/ansible/latest/intro_configuration.html#forks</a></li>
  </ul>
  
  <p>Hope this helps.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-07-25T08:49:03.427Z
Answers: IIRC, `ansible` uses `ssh` multiplexing, so should not it be a single connection to a remote host for all tasks?
Answers: heemayl
Answers: CC BY-SA 3.0
Answers: 2017-07-21T19:08:44.110Z
Answers: 2017-07-21T19:08:44.110Z
Answers: maplebird
Body: <p>I want to ask some questions about working of Ansible.</p>
  
  <p><strong>1</strong> - Which shell Ansible uses when logging to remote host ? </p>
  
  <p><a href="https://askubuntu.com/questions/937354/track-logins-made-via-non-interactive-shells/937716?noredirect=1#comment1486867_937716">https://askubuntu.com/questions/937354/track-logins-made-via-non-interactive-shells/937716?noredirect=1#comment1486867_937716</a></p>
  
  <p><strong>2</strong> -  If I have 10 tasks in a playbook, then ansible makes connection 10 times to remote host OR uses a single connection to perform all these 10 tasks ?</p>
  
  <p><strong>3</strong> - If I am running 10 tasks on 2 hosts,</p>
  
  <p>It will do first task on host 1 and then on host 2
  then second task on host 1 and then on host 2,
  ..
  . so on, </p>
  
  <p>.</p>
  
  <p>So, here also the connection persists to both the hosts?</p>
  
  <p>OR</p>
  
  <p>does it firstly connect to host 1, runs task 1 and then disconnects AND then connects to host 2, runs task 1 and then disconnects ?</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-07-26T19:30:37.500Z
Comments: In general, the Stack Exchange platform works best if you only ask one question per... question.
Comments: Xiong Chiamiov
Comments: CC BY-SA 3.0
Comments: 2017-07-26T20:10:31.613Z
Comments: Yes indeed ! But since these were little questions and related to a single topic, I combined them.
Comments: luv.preet
Comments: CC BY-SA 3.0
Comments: 2017-08-07T11:37:59.920Z
Comments: Please don't combine questions in future, it makes it harder to answer and read the whole Q&A set.  Question 1 is not really related to the other two.
Comments: RichVel
Tags: <ansible>
Title: Questions about Ansible connection to remote hosts
UserDisplayName: luv.preet
ThreadText: # Questions about Ansible connection to remote hosts
  
  (Asked by: username_0 on 2017-07-21)
  
  I want to ask some questions about working of Ansible.
  
  **1** - Which shell Ansible uses when logging to remote host ?
  
  <https://askubuntu.com/questions/937354/track-logins-made-via-non-interactive-shells/937716?noredirect=1#comment1486867_937716>
  
  **2** - If I have 10 tasks in a playbook, then ansible makes connection 10 times to remote host OR uses a single connection to perform all these 10 tasks ?
  
  **3** - If I am running 10 tasks on 2 hosts,
  
  It will do first task on host 1 and then on host 2
  then second task on host 1 and then on host 2,
  ..
  . so on,
  
  .
  
  So, here also the connection persists to both the hosts?
  
  OR
  
  does it firstly connect to host 1, runs task 1 and then disconnects AND then connects to host 2, runs task 1 and then disconnects ?
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: In general, the Stack Exchange platform works best if you only ask one question per... question.  
  > **username_0**: Yes indeed ! But since these were little questions and related to a single topic, I combined them.  
  > **username_2**: Please don't combine questions in future, it makes it harder to answer and read the whole Q&A set.  Question 1 is not really related to the other two.  
  
  ---
  
  ## 5 answers
  
  ### Answer by username_5 on 2017-07-21. Score: 7 | Accepted answer
  
  Can't answer 1 (I always thought it just uses the default /bin/sh unless specified otherwise?).
  
  2. 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 username_5 -vvv
  
     ```
  
     3rd-level verbosity shows all connections to client.
  3. 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
  
     ```
  
  More reading:
  
  Serial (rolling) playbook runs:
  
  * <http://docs.ansible.com/ansible/latest/playbooks_delegation.html#rolling-update-batch-size>
  
  Forks:
  
  * <http://docs.ansible.com/ansible/latest/intro_configuration.html#forks>
  
  Hope this helps.
  
  > **Comments:**
  > **username_6**: IIRC, `ansible` uses `ssh` multiplexing, so should not it be a single connection to a remote host for all tasks?  
  
  ---
  
  ### Answer by username_2 on 2017-07-27. Score: 3
  
  *(1) Which shell does Ansible use?*
  
  Ansible uses `/bin/sh` by default. On many \*nix systems including RHEL / CentOS, `/bin/sh` is `bash` - however, [on Ubuntu/Debian it's `dash`](https://wiki.ubuntu.com/DashAsBinSh) which is much more basic.
  
  ### Making Ansible use bash
  
  It should be possible to change this with the `executable = /bin/bash` config option [in ansible.cfg](http://docs.ansible.com/ansible/latest/intro_configuration.html#executable) and possibly also setting [ansible\_shell\_type](http://docs.ansible.com/ansible/latest/intro_inventory.html) in the inventory.
  
  But in practice (on Ansible 2.2.2 and higher) I and [others](https://github.com/ansible/ansible/issues/1238#issuecomment-200362771) have found that setting `executable` in `ansible.cfg` doesn't work (whether in `become` mode or not).
  
  An approach that works well, but does mean a bit more code per task, is to write this:
  
  ```
  - shell:
      echo hello from $0
    args:
      executable: /bin/bash
  
  ```
  
  This should say `hello from /bin/bash`, indicating the shell is correct.
  
  If you need to use `rvm`, `rbenv`, `pyenv` or similar tools that [require](https://github.com/rbenv/rbenv#installation) a previous `source ~/.bash_profile` (as with an interactive shell), you will need to [use a wrapper script](https://github.com/ansible/ansible/issues/1238#issuecomment-57797275) or [invoke bash within the shell task](https://github.com/ansible/ansible/issues/4854#issuecomment-182582413).
  
  
  ---
  
  ### Answer by username_3 on 2017-07-25. Score: 2
  
  1. Shell module use /bin/sh by default, but you can set executable in global config or locally in playbook.
  
  <http://docs.ansible.com/ansible/latest/shell_module.html>
  
  
  ---
  
  ### Answer by username_4 on 2022-06-26. Score: 0
  
  ```
  
  /*sudo apt-get update
  sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg -y
  
  curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg REDIRECT /dev/null
  ** Replace REDIRECT with chevron for greater than (YT doesn't like chevrons!) ** 
  
  AZ_REPO=$(lsb_release -cs)
  echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list
  sudo apt-get update
  sudo apt-get install azure-cli
  sudo apt install ansible
  sudo apt install python3-pip
  pip3 install ansible[azure] --user
  az login
  az ad sp create-for-rbac --name ansible
  az role assignment create --assignee appID --role Contributor
  az account show --query '{tenantId:tenantId,subscriptionid:id}';
  az ad sp list --display-name ansible --query '{clientId:[0].appId}'
  vi ~/.azure/credentials
  ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth"
  ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth state=absent"
  mkdir azure
  cd azure
  vi azure_vm.yml
  ansible localhost -m azure_rm_resourcegroup -a "name=Testing location=uksouth"
  ansible-playbook azure_vm.yml
  cat azure_vm.yml
  ssh youtubedemo@IP_ADDRESS*/
  
  ```
  
  
  ---
  
  ### Answer by username_4 on 2022-06-26. Score: 0
  
  ```
  Sudo apt-get update
  sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg -y
  
  curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg REDIRECT /dev/null
  
  ```
  
  Replace REDIRECT with chevron for greater than (YT doesn't like chevrons!)
  
  ```
  
  AZ_REPO=$(lsb_release -cs)
  
  echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list
  
  sudo apt-get update
  sudo apt-get install azure-cli
  sudo apt install ansible
  sudo apt install python3-pip
  pip3 install ansible[azure] --user
  az login
  az ad sp create-for-rbac --name ansible
  
  az role assignment create --assignee appID --role Contributor
  
  az account show --query '{tenantId:tenantId,subscriptionid:id}';
  
  az ad sp list --display-name ansible --query '{clientId:[0].appId}'
  
  vi ~/.azure/credentials
  
  ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth"
  
  ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth state=absent"
  
  mkdir azure
  cd azure
  vi azure_vm.yml
  ansible localhost -m azure_rm_resourcegroup -a "name=Testing location=uksouth"
  
  ansible-playbook azure_vm.yml
  
  cat azure_vm.yml
  
  ssh youtubedemo@IP_ADDRESS
  
  ```
  
  Links:
  
  <https://azure.microsoft.com/en-gb/free/?ref=microsoft.com>
  
  <https://docs.microsoft.com/en-us/azure/developer/ansible/install-on-linux-vm>
  
  <https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt>
  
  <https://docs.microsoft.com/en-us/azure/developer/ansible/create-ansible-service-principal>
  
  <https://docs.ansible.com/ansible/latest/scenario_guides/guide_azure.html>
  
  
  ---
  
  