Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 235
Suzanno Hogwarts Avatar asked Sep 08 '17 07:09

Suzanno Hogwarts


2 Answers

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

like image 178
Konstantin Suvorov Avatar answered Sep 19 '22 13:09

Konstantin Suvorov


You can add the ansible_become_pass variable to specify the become password in your playbook.

More details can be found here: http://docs.ansible.com/ansible/latest/become.html

like image 36
sys0dm1n Avatar answered Sep 18 '22 13:09

sys0dm1n