Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - How to execute local commands with default user, not sudo?

Tags:

ansible

I have an ansible playbook that uses become to gain root access on the remote hosts using

become: yes   

I also have some pre_tasks that have to run local commands at the ansible host in before.

How can I force this local tasks to be executed with the default user (the user that runs ansible-playbook)?

Here is my playbook (it is meant to perform a local copy operation as default user as a pre task):

---
- hosts: all
  vars:
    proprietary_files: "/some/files/not/managed/by/vcs"
    filesToWorkOnLater: "config_files"
  pre_tasks:
  - name: "Copy from {{proprietary_files}} to {{filesToWorkOnLater}}"
      local_action: 
         module: copy
         src: "{{proprietary_files}}/" 
         dest: "{{filesToWorkOnLater}}/"

  become: yes   
  roles:
     ...   
...     

At the moment I'am getting complains from my local machine like

sudo: a password is required
like image 415
jschnasse Avatar asked Nov 21 '25 11:11

jschnasse


1 Answers

Using command with delegate_to and become: no instead of local_action works:

---
- hosts: all
  vars:
    proprietary_files: "/some/files/not/managed/by/vcs"
    filesToWorkOnLater: "config_files"
  pre_tasks:
    - name: "Copy from {{proprietary_files}} to {{filesToWorkOnLater}}"
      command: cp -r {{proprietary_files}}/ {{filesToWorkOnLater}}/
      delegate_to: localhost
      become: no

  become: yes   
  roles:
     ...   
...     
like image 187
jschnasse Avatar answered Nov 23 '25 00:11

jschnasse