How to switch a user per task or set of tasks?

前端 未结 5 1885
無奈伤痛
無奈伤痛 2020-11-29 17:08

A recurring theme that\'s in my ansible playbooks is that I often must execute a command with sudo privileges (sudo: yes) because I\'d like to do it for a certa

5条回答
  •  天涯浪人
    2020-11-29 17:37

    You can specify become_method to override the default method set in ansible.cfg (if any), and which can be set to one of sudo, su, pbrun, pfexec, doas, dzdo, ksu.

    - name: I am confused
      command: 'whoami'
      become: true
      become_method: su
      become_user: some_user
      register: myidentity
    
    - name: my secret identity
      debug:
        msg: '{{ myidentity.stdout }}'
    

    Should display

    TASK [my-task : my secret identity] ************************************************************
    ok: [my_ansible_server] => {
        "msg": "some_user"
    }
    

提交回复
热议问题