Why do I have to split up this set_fact into multiple? or am I doing something incorrect?

Hi can someone please explain why the following two seperate 'set_fact’s work.

  • name : combine network and wildcard

set_fact:

net_mask: “{{ ‘{{network}}/{{wildcard}}’ }}”

when: ansible_network_os == ‘asa’

  • name : determine netmask from wildcard

set_fact:

mynetmask: “{{ net_mask |ipaddr(‘netmask’) }}”

when: ansible_network_os == ‘asa’

But this ‘combined’ effort doesn’t work

  • name : determine netmask from network and wildcard

set_fact:

mynetmask : “{{ ‘{{network}}/{{wildcard}}’ |ipaddr(‘netmask’) }}”

when: ansible_network_os == ‘asa’

I’m trying to understand the limitations of set_facts.

Cheers.

Hi can someone please explain why the following two seperate 'set_fact's
work.

  - name : combine network and wildcard
    set_fact:
       net_mask: "{{ '{{network}}/{{wildcard}}' }}"
    when: ansible_network_os == 'asa'

You can't use {{ }} inside {{ }}
So correct syntax is

   - name : combine network and wildcard
     set_fact:
        net_mask: '{{network}}/{{wildcard}}'
     when: ansible_network_os == 'asa'

  - name : determine netmask from wildcard
    set_fact:
        mynetmask: "{{ net_mask |ipaddr('netmask') }}"
    when: ansible_network_os == 'asa'

But this 'combined' effort doesn't work

  - name : determine netmask from network and wildcard
    set_fact:
        mynetmask : "{{ '{{network}}/{{wildcard}}' |ipaddr('netmask') }}"
    when: ansible_network_os == 'asa'

   - name : determine netmask from network and wildcard
     set_fact:
         mynetmask : "{{ (network ~ '/' ~ wildcard) |ipaddr('netmask') }}"
     when: ansible_network_os == 'asa'

I'm trying to understand the limitations of set_facts.

There is no limitation, you just need to use the correct jinja2 syntax.

Thank you very much. It looks like I have some reading to do :slight_smile:

Cheers