Ansible reports “permission denied” when issuing command on remote host

Ansible 2.1
Ansible host: Ubuntu 16.04
Remote host: CentOS 6.5

I have a simple ansible project:

├── hosts
├── roles
│   └── setup
│       ├── defaults
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml
│       └── templates
│           └── automation-agent.config.j2
└── site.yml

Command I used to run playbook:

ansible-playbook -i hosts site.yml --user admin --ask-pass

On the remote host, I have set up user admin with root priviledge:

root    ALL=(ALL)       ALL
admin   ALL=(ALL)       ALL

However, one of the playbook tasks ran into issue:

- name: Back up Automation Agent config file if exists
  command: mv /etc/mongodb-mms/automation-agent.config /etc/mongodb-mms/automation-agent.config.bak

Ansible reports:

try this:
ansible-playbook -i hosts site.yml --user admin --ask-pass --become

--become-user admin is basically telling ansible to login as admin and then use sudo to become admin, which is basically useless. With the command above it will become ‘root’, the default.

You also need to add --ask-become-pass

Yes, --ask-become-pass helped. Removing ‘–become-user admin’ as it is redundant. The whole command now is:

ansible-playbook -i hosts site.yml --user admin --ask-pass --become --ask-become-pass

This is telling Ansible to ssh using ‘admin’, and uses ‘admin’ sudo priviledge to play tasks. It works great.

Thanks.

You can just add : yes in your playbook so you don’t have to define it when running the command.