How to mix privileged and unprivileged users in a playbook?

0

The user I use for my the hosts in playbooks cannot sudo due to safety reasons, but I still need to perform admin tasks like make sure git is installed. Is it possible to have Ansible connect to the same host with different users in a single run?

Yuri Geinish

Posted 2019-04-10T11:31:42.890

Reputation: 27

Answers

0

Specifying a remote_user at task level is supported since 2013. Try this (Note: commands are here just to illustrate usage of the other tasks parameters):

- name: Playbook with mixed users
  hosts: all
  remote_user: default_user_without_sudo
  become: false

  tasks:
    - name: normal task
      command: /bin/true

    - name: sudo task
      apt:
        name: [git, apache]
        state: present
      remote_user: user_with_sudo
      become: true

A better approach (and more readable at final) would be to use the same user_with_sudo throughout your playbook and to only become: true when needed.

Zeitounator

Posted 2019-04-10T11:31:42.890

Reputation: 214