Interview Prep/Ansible/Vault, Privilege Escalation and Error Handling

How do `block` and `become` inheritance interact?

IntermediateConceptVault, Privilege Escalation and Error Handling
Answer

Keywords on a block (become, become_user, when, vars, tags, environment, ignore_errors) apply to every task inside it, and a task's own keyword overrides the inherited one. This keeps playbooks short:

- hosts: db
  tasks:
    - become: true
      become_user: postgres
      when: pg_role == 'primary'
      block:
        - community.postgresql.postgresql_db: { name: app }
        - community.postgresql.postgresql_user: { name: app_rw, db: app }

A when on a block is evaluated for each task in it, so values set by an earlier task can change the result partway through.

All Ansible questions Roadmap: Ansible

More on Vault, Privilege Escalation and Error Handling