Is there anyway to run multiple Ansible playbooks as multiple users more efficiently?

可紊 提交于 2019-11-30 23:12:00

You can set the become options per:

  • playbook
  • role
  • task

Per playbook:

- hosts: whatever
  become: yes
  become_user: my_username
  roles:
    - checkout_sources
    - install_dependencies
    - make_dirs

Per role:

- hosts: whatever
  roles:
    - checkout_sources
    - role: install_dependencies
      become: yes
      become_user: my_username
    - make_dirs

Per task:

- shell: do something
  become: yes
  become_user: my_username

You can combine this however you like. The playbook can run as user A, a role as user B and finally a task inside the role as user C.

Defining become per playbook or role is rarely needed. If a single task inside a role requires sudo it should only be defined for that specific task and not the role.

If multiple tasks inside a role require become, blocks come in handy to avoid recurrence:

- block:
    - shell: do something
    - shell: do something
    - shell: do something
  become: yes
  become_user: my_username
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!