cm_1nges
cm_1nges

Reputation: 48

Can't run sudo command in Ansible playbook

I am writing an Ansible playbook to automate a series of sudo commands on various hosts. When I execute these commands individually in puTTY, I have no permission problems, as I have been granted proper access. However, when I attempt to create a playbook to do the same thing, I am told

user is not allowed to execute ... on host_name

For example, if I do $ sudo ls /root/, I have no problem, and, once I enter my password, can see the contents of /root/

In the case of my Ansible playbook ...

---
- host: servers
  tasks:
  - name: ls /root/
    shell: ls /root/
    become: true
    become_method: sudo

...I then get the error mentioned above.

Any ideas why this would be the case? It seems to be telling me I don't have permission to run a command that I otherwise could run in an individual puTTY terminal.

Upvotes: 0

Views: 4105

Answers (1)

techraf
techraf

Reputation: 68439

[ ] automate a series of sudo commands on various hosts. When I execute these commands individually [ ]

Any ideas why this would be the case?

Sounds like you configured specific commands in the sudoers file (unfortunately you did not provide enough details, fortunately you asked for "ideas" not the real cause).

Ansible shell module does not run the command you specify prepended with sudo - it runs the whole shell session with sudo, so the command doesn't match what you configured in sudoers.

Either allow all commands to be run with elevated privileges for the Ansible user, or use raw module instead of shell.

Upvotes: 1

Related Questions