Answers: <p>For example,</p>
  <pre class="lang-yaml prettyprint-override"><code>result: &quot;{{ service.control.0.ipa }},{{ management.control.0.ipa }}&quot;
  </code></pre>
  <p>gives</p>
  <pre class="lang-yaml prettyprint-override"><code>result: 192.168.101.81,10.100.78.81
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-09-18T17:59:50.873Z
Answers: And when pointed out, it's obvious. thanks!
Answers: Freejack
Answers: CC BY-SA 4.0
Answers: 2022-09-18T04:33:23.210Z
Answers: 2022-09-18T04:33:23.210Z
Answers: Vladimir Botka
Body: <p>I'm trying to access a specific value in a list of data and it fails, can't find the ipa variable.</p>
  <p>Here is a short bit of the boulder.yaml vars file, yes, dual homed. Network configurations require it:</p>
  <pre><code>service:
    control:
    - name: &quot;bldr0cuomdev01&quot;
      ipa: 192.168.101.81
    - name: &quot;bldr0cuomdev02&quot;
      ipa: 192.168.101.82
  management:
    control:
    - name: &quot;bldr0cuomdev11&quot;
      ipa: 10.100.78.81
    - name: &quot;bldr0cuomdev12&quot;
      ipa: 10.100.78.82
  </code></pre>
  <p>And I'm trying to use the following (much shorter) jinja2 template (nodes.j2) to create a file:</p>
  <pre><code>{{ service.control.ipa[0] }},{{ management.control.ipa[0] }}
  </code></pre>
  <p>I'm basically getting the error that ipa is not found.</p>
  <p>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.</p>
  <pre><code>{% for s in service.control %}
  {{ s.ipa }}
  {% endfor %}
  </code></pre>
  <p>I saw another suggestion of using .0 instead of a bracketed array</p>
  <pre><code>{{ service.control.ipa.0 }},{{ management.control.ipa.0 }}
  </code></pre>
  <p>And also tried using a quoted zero</p>
  <pre><code>{{ service.control.ipa['0'] }},{{ management.control.ipa['0'] }}
  </code></pre>
  <p>But neither work. Searching on the 'net is common enough that my search is not finding anything relevant.</p>
  
Tags: <ansible><jinja>
Title: How to access elements of a variable in ansible
UserDisplayName: Freejack
ThreadText: # How to access elements of a variable in ansible
  
  (Asked by: username_0 on 2022-09-18)
  
  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.
  
  **Tags:** ansible, jinja
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-09-18. Score: 2
  
  For example,
  
  ```yaml
  result: "{{ service.control.0.ipa }},{{ management.control.0.ipa }}"
  
  ```
  
  gives
  
  ```yaml
  result: 192.168.101.81,10.100.78.81
  
  ```
  
  > **Comments:**
  > **username_0**: And when pointed out, it's obvious. thanks!  
  
  ---
  
  