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

后端 未结 1 552
清歌不尽
清歌不尽 2021-01-06 03:26

Currently my playbook structure is like this:

~/test_ansible_roles ❯❯❯ tree .
.
├── checkout_sources
│   └── tasks
│       └── main.yml
├── install_dependenc         


        
1条回答
  •  生来不讨喜
    2021-01-06 04:11

    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
    

    0 讨论(0)
提交回复
热议问题