Run ansible playbook with sudo

ansibleansible-playbook

I have a playbook and I want to run it with sudo. It is my ansible playbook:

site.yml

---
- name: Server
  hosts: node1
  sudo: yes
  roles:
    - dbserver

When I run it I get this:

ansible-playbook -i hosts site.yml

PLAY [Server] ***************************************************************** 

GATHERING FACTS *************************************************************** 
fatal: [node1] => Missing sudo password

TASK: [dbserver | installing server] ****************************************** 
FATAL: no hosts matched or all hosts have already failed -- aborting


PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/robe/site.retry

node1                      : ok=0    changed=0    unreachable=1    failed=0

Then I add the ansible sudo pass on site.yml:

---
- name: Server
  hosts: node1
  sudo_pass: ubuntu
  roles:
    - dbserver

I get this error:

ERROR: sudo_pass is not a legal parameter at this level in an Ansible Playbook

Then my questions are:

  • Do I have add to each tasks the ansible sudo_pass attribute?
  • Is there any way to say sudo and sudo_pass in the playbook?

Best Answer

sudo_pass is not something Ansible knows. If you need to enter a sudo password on node1, then you keep sudo: yes in the playbook, and you'll have to give Ansible the sudo password on the commandline when running your playbook:

ansible-playbook -i hosts site.yml -K

Note the -K parameter. It will then ask you for the sudo password before the playbook starts.

(the long version is --ask-sudo-pass by the way. I had to look that up)

Related Topic