Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use sudo in Ansible with connection "local"?

Tags:

ansible

I give up, just can't understand how to use Ansible with "connection: local" + "sudo: yes". I have something like:

ansible-playbook ansible/desktop.yml

- hosts: localhost
  connection: local
  ...
  tasks:
    - apt_repository: repo='ppa:alexey-smirnov/deadbeef'
      sudo: yes

I've also tried sudo_user: ... param, sudo before the command, ansible-playbook --sudo and --ask-sudo-pass

Currently:

failed: [localhost] => {"failed": true}
msg: [Errno 13] Permission denied

How should it be executed?

ansible --version
ansible 1.7.2
like image 214
Sergey Avatar asked Oct 20 '14 07:10

Sergey


People also ask

How do I use the sudo command in Ansible?

Ansible Sudo or become is a method to run a particular task in a playbook with Special Privileges like root user or some other user. become and become_user both have to be used in a playbook in certain cases where you want your remote user to be non-root.it is more like doing sudo -u someuser before running a task.

Can Ansible manage localhost?

Using localhost in ansible playbook hosts argumentIn the hosts argument mention localhost so this entire playbook will run on locally or ansible control machine. This playbook will run on your local machine. Note: you can also mention 127.0. 0.1 in the place of localhost the playbook, Both localhost and 127.0.

What does Ansible connection local mean?

It is to execute the tasks locally on the same host (i.e., the controller) where the playbook is run. From the documentation, It may be useful to use a playbook locally, rather than by connecting over SSH. Follow this answer to receive notifications. answered Mar 18, 2020 at 19:30.


1 Answers

Try

ansible-playbook -i <inventory> ansible/desktop.yml -u <local user who can sudo with password> --ask-sudo-pass

This will make ansible use the remote user you mentioned in -u. And when it uses that user to sudo, it will ask you for sudo password.

like image 120
Zasz Avatar answered Sep 19 '22 00:09

Zasz