dzbeda
dzbeda

Reputation: 303

How to extract a specific value from input list as part of Ansible template file

I have the following input list (as part of inputfile)

postgres_create_users:
        - {role: user1, password: password1}
        - {role: user2, password: password2}
        - {role: user3, password: password3}
        - {role: user4, password: password4}

As part of Jinja template file, I need to extract the password of user4. How can this be done?

Jinja file

insights.jdbc.password={% for item in {{ postgres_create_users }} ??? %}

Upvotes: 1

Views: 160

Answers (3)

Vladimir Botka
Vladimir Botka

Reputation: 68104

Create a dictionary. The best choice might be the same place the list postgres_create_users comes from. For example,

shell> cat group_vars/all/postgres_create_users.yml   
postgres_create_users:
  - {role: user1, password: password1}
  - {role: user2, password: password2}
  - {role: user3, password: password3}
  - {role: user4, password: password4}
pcu_dict: "{{ postgres_create_users|
              items2dict(key_name='role',
                         value_name='password') }}"

gives

  pcu_dict:
    user1: password1
    user2: password2
    user3: password3
    user4: password4

The usage is trivial. For example, the template

shell> cat templates/server.xml.j2  
insights.jdbc.password="{{ pcu_dict.user4 }}"

and the playbook

shell> cat pb.yml  
- hosts: localhost
  tasks:
    - debug:
        msg: "{{ lookup('template', 'server.xml.j2') }}"

gives (abridged)

shell> ansible-playbook pb.yml  

TASK [debug]
**************************************
ok: [localhost] => msg: |-
    insights.jdbc.password="password4"

Upvotes: 1

dzbeda
dzbeda

Reputation: 303

I have figure it out :

insights.jdbc.password={% for item in postgres_create_users if item.role == 'user4' %}{{ item.password }}{% endfor %}

Upvotes: 0

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39244

You could translate the list to a dictionary by the mean of the items2dict, to make it easier to access the password of user4.

insights.jdbc.password={{ 
   (
     postgres_create_users
     | items2dict(key_name='role', value_name='password')
   ).user4
}}

Given the task:

- debug:
    msg: >-
      insights.jdbc.password={{ (
          postgres_create_users
          | items2dict(key_name='role', value_name='password')
        ).user4
      }}

  vars:
    postgres_create_users:
      - role: user1
        password: password1
      - role: user2
        password: password2
      - role: user3
        password: password3
      - role: user4
        password: password4

Would give:

ok: [localhost] => 
  msg: insights.jdbc.password=password4

And if you need the user to be from a variable, e.g. role:

insights.jdbc.password={{ 
   (
     postgres_create_users
     | items2dict(key_name='role', value_name='password')
   )[role]
}}

So, the task becomes:

- debug:
    msg: >-
      insights.jdbc.password={{ (
          postgres_create_users
          | items2dict(key_name='role', value_name='password')
        )[role]
      }}
  vars:
    role: user4
    postgres_create_users:
      - role: user1
        password: password1
      - role: user2
        password: password2
      - role: user3
        password: password3
      - role: user4
        password: password4

Upvotes: 2

Related Questions