0

following situation. I have 3 plays in total. In the first play I filter a routername and register it in a variable, let's call this variable pe. pe is used in the second play as a host, like this

- hosts: "{{ hostvars['firsthost']['pe']['stdout'] }}"

In my second play I do some actions and afterwards I prepare some output which I store in another variables. Like this:

- name: verify PE ping
      set_fact:
        temp_pe_output: '{{ "WAN interface vrf" "{{ vrf }}" "on PE is NOT reachable" if "{{ ping_output_pe.stdout }}" is search("0/5") else "WAN interface vrf " "{{ vrf }}" " on PE is reachable"}

Now in my third play I want to use these variables from the second play. After several attempts I end with this error after trying to run this task:

  - name: Find the connected PE
    ansible.builtin.shell: grep "description DL" {{ varnet }}/{{ router }}.cfg | awk '{print tolower($5)}'
    register: per

  - name: Prepare Article
    set_fact:
      article: |
        {{ hostvars[{{ per.stdout }}]['temp_cpe_output'] }}
        Test


TASK [Prepare Article] *****************************************************************************************************************************************************************************
fatal: [thirdhost]: FAILED! => {"msg": "template error while templating string: expected token ':', got '}'. String: \"{{ hostvars[{{ per.stdout }}]['temp_cpe_output'] }}\"\n\"Test\"\n"}

per.stdout contains the name of the 2nd host. Is there a hole in my logic and what does that error mean?

  • 2
    [Moustaches don't stack](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names) => `{{ hostvars[per.stdout]['temp_cpe_output'] }}`. You must fix the probelm in your previous `set_fact` too. I suspect you want: `temp_pe_output: "WAN interface vrf {{ vrf }} on PE is{{ ping_output_pe.stdout is search('0/5') | ternary(' NOT', '') }} reachable"` – Zeitounator Dec 03 '21 at 14:44
  • maybe use the ansible controller as a "cache" medium to store some details inside a JSON file and read that from the third playbook. – TRW Dec 08 '21 at 14:57

0 Answers0