Skip to content

Example 3

hosts.ini

[centos]
provisioner     ansible_connection=local
centos1         ansible_host=192.168.77.22      ansible_become=true     ansible_become_method=su

[ubuntu]
ubuntu1         ansible_host=192.168.77.23      ansible_become=true     ansible_become_method=sudo

Tip

You can change the behavior of the become directive by editing the default become_method (which is sudo).

Try to execute the whoami command on all hosts.

ansible all -m command -a "whoami"
What is the expected output of the command ?

The command will generate a different output on every host

  • on provisioner - we will be identified as the vagrant user since we did not explicitly instruct Ansible to act otherwise
  • on centos1 - we will receive a timeout since we have not provided any credentials for the su prompt
  • on ubuntu1 - we will run the command as the root user since we instructed Ansible to use sudo, which as we have already established is passwordless
Bonus round

Fix the problem with centos1. Use one of the following methods

  • instruct Ansible to ask for password in runtime using the --ask-become-pass (-K) flag
    ansible all -m command -a "whoami" -K
    
  • modify the hosts.ini file by adding ansible_become_pass=vagrant to the line describing centos1