Ansible-如何使用默认用户而不是sudo执行本地命令?

时间:2019-09-25 09:52:20

标签: ansible

我有一个有趣的剧本,它使用become来使用来获得远程主机上的root访问权限

become: yes   

我还有一些 pre_tasks ,它们必须在以前的可运行主机上运行本地命令

如何强制使用默认用户(运行ansible-playbook的用户)执行本地任务?

这是我的剧本(它是要作为默认用户执行local copy操作作为前置任务)

---
- 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:
     ...   
...     

此刻,我从本地计算机收到投诉

sudo: a password is required

1 个答案:

答案 0 :(得分:1)

使用commanddelegate_tobecome: no代替local_action可以做到:

---
- 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:
     ...   
...