7

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 ?

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

  • 2
    IIRC, ansible uses ssh multiplexing, so should not it be a single connection to a remote host for all tasks? – heemayl Jul 25 '17 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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