How can I run commands in sudo mode with ansible playbook?

Suzanno Hogwarts picture Suzanno Hogwarts · Sep 8, 2017 · Viewed 19.3k times · Source

I am trying to run a "folder creation" command with my ansible playbook. (Code is below)

The creation requires sudo login to execute.

I run the playbook as follows:

ansible-playbook myfile.yml --ask-pass

This prompts for user account password of remote machine. The ssh connection gets established, but commands fail with permission denied since its not taking super user password.

How can I fix my issue?

 hosts: GSP
 tasks:
   - name: "make build directory"
     command: mkdir -p /home/build/
     become: true
     become_user: root
   - name: "change permissions on the directory"
     command: chmod 777 -R /home/
     become: true
     become_user: root

Answer

Konstantin Suvorov picture Konstantin Suvorov · Sep 8, 2017

There's also --ask-become-pass switch for ansible-playbook cli to query user for sudo password.