Nice!

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Aug 9, 2022, at 9:10 AM, Richard Megginson 
<[email protected]<mailto:[email protected]>> wrote:

Here is a way that
* uses only ansible built-ins (note that json_query is a) in an unsupported 
collection b) requires python json libraries which may not be available)
* works with every version of jinja2 (EL7 and later controller)

insights.jdbc.password={{ postgres_create_users | selectattr('role', 'match', 
'^user4$') | map(attribute='password') | first }}

On Tue, Aug 9, 2022 at 6:09 AM 'Walter Rowe' via Ansible Project 
<[email protected]<mailto:[email protected]>> 
wrote:
You also can use json_query.

---
- name: test
  hosts: localhost
  become: no
  gather_facts: no
  vars:
    postgres_create_users:
      - {role: user1, password: password1}
      - {role: user2, password: password2}
      - {role: user3, password: password3}
      - {role: user4, password: password4}
    my_user: user4
  tasks:
    - debug:
        msg: "{{ (postgres_create_users | 
json_query('[?role==`'+my_user+'`]'))[0].password }}"


Note that in my jinja2 variable template in the debug statement I insert a 
variable "my_user" to show that json queries can be dynamic based on a value 
only known at run-time. Change the value of my_user on the command line and see 
how it changes the output.

% ansible-playbook foo.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that 
the implicit localhost does not match 'all'
PLAY [test] 
************************************************************************************************************
TASK [debug] 
***********************************************************************************************************
ok: [localhost] => {
    "msg": "password4"
}
PLAY RECAP 
*************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0

% ansible-playbook foo.yml -e my_user=user2
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that 
the implicit localhost does not match 'all'
PLAY [test] 
************************************************************************************************************
TASK [debug] 
***********************************************************************************************************
ok: [localhost] => {
    "msg": "password2"
}
PLAY RECAP 
*************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce
On Tuesday, August 9, 2022 at 6:49:20 AM UTC-4 
[email protected]<mailto:[email protected]> wrote:
On Mon, 8 Aug 2022 23:07:32 -0700 (PDT)
"[email protected]" <[email protected]> wrote:

> insights.jdbc.password={{ postgres_create_users ??? }}

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"

--
Vladimir Botka

--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/cbdebba3-d3b0-455d-ac16-498e773c0c4cn%40googlegroups.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2Fcbdebba3-d3b0-455d-ac16-498e773c0c4cn%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C66a7465451a14734c32608da7a0899f0%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637956474606109993%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=5eTznzicxlRj5EpxUNqXjmcI7QJCQajjul9Tuk%2B%2FU2o%3D&reserved=0>.

--
You received this message because you are subscribed to a topic in the Google 
Groups "Ansible Project" group.
To unsubscribe from this topic, visit 
https://groups.google.com/d/topic/ansible-project/Vx1VC0R3nb0/unsubscribe<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Ftopic%2Fansible-project%2FVx1VC0R3nb0%2Funsubscribe&data=05%7C01%7Cwalter.rowe%40nist.gov%7C66a7465451a14734c32608da7a0899f0%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637956474606266653%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=LAG2Ec%2FfArocXieoQc94nJjLwsrD5YWyeaRJTGQJn8k%3D&reserved=0>.
To unsubscribe from this group and all its topics, send an email to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CALF5A-K13OZu6i%3Dbh8TriR8CDcPVvaOztv88QqcM%2BmBOw0E-Hw%40mail.gmail.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCALF5A-K13OZu6i%253Dbh8TriR8CDcPVvaOztv88QqcM%252BmBOw0E-Hw%2540mail.gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C66a7465451a14734c32608da7a0899f0%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637956474606266653%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=CftCcO0XJSDH%2BvuvzlzWWkisBhswq14W%2Bpva01jUeng%3D&reserved=0>.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/66B4D729-A0F7-4224-AD76-9CD2F7BDEE2E%40nist.gov.

Reply via email to