9

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 ?

3
  • 1
    In general, the Stack Exchange platform works best if you only ask one question per... question. Commented Jul 26, 2017 at 19:30
  • Yes indeed ! But since these were little questions and related to a single topic, I combined them.
    – luv.preet
    Commented Jul 26, 2017 at 20:10
  • 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.
    – RichVel
    Commented Aug 7, 2017 at 11:37

5 Answers 5

7

Can't answer 1 (I always thought it just uses the default /bin/sh unless specified otherwise?).

  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
    

More reading:

Serial (rolling) playbook runs:

Forks:

Hope this helps.

1
  • 2
    IIRC, ansible uses ssh multiplexing, so should not it be a single connection to a remote host for all tasks?
    – heemayl
    Commented Jul 25, 2017 at 8:49
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 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 and possibly also setting ansible_shell_type in the inventory.

But in practice (on Ansible 2.2.2 and higher) I and others 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 a previous source ~/.bash_profile (as with an interactive shell), you will need to use a wrapper script or invoke bash within the shell task.

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

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

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*/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.