How to access elements of a variable in ansible



  • I'm trying to access a specific value in a list of data and it fails, can't find the ipa variable.

    Here is a short bit of the boulder.yaml vars file, yes, dual homed. Network configurations require it:

    service:
      control:
      - name: "bldr0cuomdev01"
        ipa: 192.168.101.81
      - name: "bldr0cuomdev02"
        ipa: 192.168.101.82
    management:
      control:
      - name: "bldr0cuomdev11"
        ipa: 10.100.78.81
      - name: "bldr0cuomdev12"
        ipa: 10.100.78.82
    

    And I'm trying to use the following (much shorter) jinja2 template (nodes.j2) to create a file:

    {{ service.control.ipa[0] }},{{ management.control.ipa[0] }}
    

    I'm basically getting the error that ipa is not found.

    Since there are two sections, having it be a loop isn't going to work but if I test just the service section as a loop, it does work. So it's able to find ipa, but my method isn't valid for some reason.

    {% for s in service.control %}
    {{ s.ipa }}
    {% endfor %}
    

    I saw another suggestion of using .0 instead of a bracketed array

    {{ service.control.ipa.0 }},{{ management.control.ipa.0 }}
    

    And also tried using a quoted zero

    {{ service.control.ipa['0'] }},{{ management.control.ipa['0'] }}
    

    But neither work. Searching on the 'net is common enough that my search is not finding anything relevant.



  • For example,

    result: "{{ service.control.0.ipa }},{{ management.control.0.ipa }}"
    

    gives

    result: 192.168.101.81,10.100.78.81
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2