1

I've been working on this problem for a while and can't find a solution.

My playbook is:

- hosts: localhost
  connection: local
  gather_facts: False
  vars:
    vpc_stack_name: "VPC-CF"

  tasks:
  - name: Get summary information about a stack
    amazon.aws.cloudformation_info:
      stack_name: "{{ vpc_stack_name }}"
    register: vpc_stack_facts

  - debug:
      var: "vpc_stack_facts.cloudformation['{{ vpc_stack_name }}'].stack_outputs['VPCID']"

  - set_fact:
      temp: "{{ vpc_stack_facts.cloudformation['{{ vpc_stack_name }}'].stack_outputs['VPCID'] }}"

I know the embedded {{ vpc_stack_name }} is wrong and not allowed but can't figure out how to get around it.

I haven't got the grasp of Ansible lookups as yet.

Thank You in advance!!

seshadri_c
  • 6,906
  • 2
  • 10
  • 24
Txynidakis
  • 21
  • 5
  • It looks ok for `debug` as your are reconstructing a var name for the `var` attribute. For yous `set_fact` you cannot nest jinja2 markers, just use the var name which holds the corresponding value => `"{{ vpc_stack_facts.cloudformation[vpc_stack_name].stack_outputs['VPCID'] }}"`. – Zeitounator Jan 08 '21 at 10:39

1 Answers1

1

As you have mentioned, the use of Jinja expressions {{ vpc_stack_name }} while inside a Jinja context is not correct. Ansible will try to evaluate any text without quotes as a variable. So you can keep the vpc_stack_name variable as it is: [vpc_stack_name].

Example:

  vars:
    vpc_stack_name: VPC-CF

  tasks:
  - cloudformation_info:
      stack_name: "{{ vpc_stack_name }}"
    register: vpc_stack_facts
  - set_fact:
      temp_var: "{{ vpc_stack_facts['cloudformation'][vpc_stack_name|quote]['stack_outputs']['VPC'] }}"
  - debug:
      var: temp_var

Note: In the above example I am using the key as VPC to get the VPC ID as output.

  • Used [''] notation instead of dots . to access dict items
  • Used |quote filter around the variable vpc_stack_name

Yields:

ok: [localhost] => {
    "temp_var": "vpc-1234ab5678c90d1e"
}
seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • 1
    OP's current expression for `debug: var` is totally correct (even though I would rather recommand using `msg` with below in this case) and should not fire an error. Removing the dot notation is only required to use variables or literal key name containing "special" chars (space, dash, etc...) and not required for `stack_outputs` (although you perfectly have the choice). `"{{ vpc_stack_facts.cloudformation[vpc_stack_name].stack_outputs.VPCID }}"` will work for both a `set_fact` or a `debug: msg` – Zeitounator Jan 08 '21 at 11:06
  • Yes the issue was having Jinja expression within a Jinja context for `debug: msg` or `set_fact`.. I realized it works with dot notation as well, but having `[]` seems more consistent. – seshadri_c Jan 08 '21 at 11:09
  • 1
    This is really a matter of opinion ;) I always favor the dot notation over the array one (less verbose) and don't care about mixing. My point was: you don't have to change it in this case and that is actually not a fix for OP's problem. – Zeitounator Jan 08 '21 at 11:14
  • 1
    My intention *was not* to say that using `[]` or `|quote` is going to fix OP's issue. in case it sounded like that I changed the order for explaining my example. – seshadri_c Jan 08 '21 at 11:24
  • 1
    Thank you @seshadri_c & Zeitounator This is really appreciated! I didn't realize I was using a combination of dot notation and []!!! I was blindly following another example to get to specific stack output! – Txynidakis Jan 10 '21 at 03:38