I have to administer an important number of hosts where there is only one user able to access from SSH (let's call him foo) and then have to login as root to execute privilegied commands.

Foo user doesn't have any kind of sudo privileges so I'm forced to login as root to execute privilegied commands. I'm actually using an expect script to do this job (ssh login as foo, login as root via su) and I want to switch to Ansible. I know I could create a new user and give him sudo privileges or basically give sudo privileges to foo but is it possible to do the same thing I'm doing with expect with Ansible?

I would like to do the ssh connection as foo and then execute playbooks or any kind of adhoc command as root. I didn't found any information in the Ansible documentation about that so I'm asking here if someone already had this kind of interrogation.

1 Answer 1


With ansible, you can use sudo or su:

- hosts: example.com
  gather_facts: False
  su: yes
  su_user: root
    - shell: whoami

You could also do that from the command line:

ansible-playbook --su --su-user=root --ask-su-pass playbook.yml

This functionality has been available since this pull request, and is present in current 1.6.6.

  • 1
    Thanks. How could you have different su password for each host? ansible_su_pass doesnt exist
    – Arka
    Jul 14, 2014 at 21:22
  • Have you find how to choose ansible su pass ?
    – jeremieca
    Jan 10, 2015 at 9:28
  • As of ansible 1.9.4, you can't have different passwords for each host. You can have different ssh keys though.
    – Schlueter
    Oct 28, 2015 at 21:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .