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 thevagrantuser 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 thesuprompt - on
ubuntu1- we will run the command as therootuser since we instructed Ansible to usesudo, 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) flagansible all -m command -a "whoami" -K - modify the
hosts.inifile by addingansible_become_pass=vagrantto the line describingcentos1