Ansible - Variable within a lookup

Viewed 19

I'm trying to create an IAM policy specific to the playbook being used, but each playbook uses a common role.

I have the following defined in two different playbooks:

policy_document: "iam_policy_1.json"
policy_document: "iam_policy_2.json"

and the corresponding IAM policy JSON files are in my templates directory for a common role.

In my tasks for the role, I have

- name: create IAM policy
  iam_policy:
    iam_name: "{{ app_name }}"
    policy_name: "{{ app_name }}-policy"
    policy_json: "{{ lookup('template',policy_document) }}"
    iam_type: "role"
    state: "present"

Am I able to do {{ lookup('template',policy_document) }}? I am unsure of having a variable nested within a jinja2 template. Would {{ lookup('template',{{ policy_document }}) }} work?

1 Answers

It turns out, you can do this, but my spacing was incorrect. This is valid:

policy_json: "{{ lookup('template', policy_document) }}"
Related