Interview Prep/Ansible/Security and Secrets

What are the injection risks in Ansible, and how do you mitigate them?

AdvancedProductionSecurity and Secrets
Answer

Two classes. Template injection happens when untrusted text (an EC2 tag, a hostname, command output, an API response) is rendered as Jinja2 and contains {{ ... }}. Ansible marks facts, registered results and lookup outputs as unsafe by default so they are not re-templated. Do not defeat that with | from_json on untrusted input and then template it, or by allow_unsafe.

Shell injection happens when variables go into shell: strings. Mitigate by using modules or command with argv, and quoting with quote where a shell is unavoidable.

- ansible.builtin.command:
    argv: ["/usr/bin/grep", "-r", "{{ term }}", "/var/log/app"]
- ansible.builtin.shell: "grep -r {{ term | quote }} /var/log/app"
All Ansible questions Roadmap: Ansible

More on Security and Secrets