How to extract one attribute from a list of dictionaries

74 views
Skip to first unread message

jean-christophe manciot

unread,
Apr 21, 2020, 11:39:46 AM4/21/20
to Ansible Project
Let's suppose the following list of dictionaries:
- domain_definition:
        - name: server11
          cluster: cluster1
          port: '8080'
        - name: server12
          cluster: cluster2
          port: '8090'
        - name: server21
          cluster: cluster3
          port: '9080'
        - name: server22
          cluster: cluster4
          port: '9090'

The aim is to extract all ```name: 'value'``` from ```domain_definition``` into a list:
        - name: server11
        - name: server12
        - name: server21
        - name: server22

I tried for instance the following:
- set_fact:
        domain_definition_names_list: "{{ ['name'] | 
                                 intersect(domain_definition.keys()|list) }}"
but it fails:
fatal: [localhost]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'keys'...}

Any suggestion?



jean-christophe manciot

unread,
Apr 21, 2020, 11:41:31 AM4/21/20
to Ansible Project
 I'm using:
- ansible 2.9.7
- python 3.8.2 

Dick Visser

unread,
Apr 21, 2020, 11:55:02 AM4/21/20
to ansible...@googlegroups.com
just to make sure - this is a list of list of dictionaries.
If this is your source of truth, and you intend to have a list of dicts, then it could look like this:


domain_definition:
        - name: server11
          cluster: cluster1
          port: '8080'
        - name: server12
          cluster: cluster2
          port: '8090'
        - name: server21
          cluster: cluster3
          port: '9080'
        - name: server22
          cluster: cluster4
          port: '9090'


This will make a difference when manipulating the data.



--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0d4429c3-3a17-4078-ade4-38cd1f48ed83%40googlegroups.com.


--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Dick Visser

unread,
Apr 21, 2020, 12:04:52 PM4/21/20
to ansible...@googlegroups.com
More accurately, the data structure you posted isn't a variable, it's an item from a "upper" list variable - which we don't know the name of.

An example:


---
- hosts: localhost
  connection: local
  gather_facts: no

  vars:

    domain_definition:
      - name: server11
        cluster: cluster1
        port: '8080'
      - name: server12
        cluster: cluster2
        port: '8090'
      - name: server21
        cluster: cluster3
        port: '9080'
      - name: server22
        cluster: cluster4
        port: '9090'

  tasks:
    - name: Keep names
      debug:
        msg: "{{ domain_definition | json_query('[*].{name: name}') }}"


Gives:

TASK [Keep names] **************************************************************
ok: [localhost] =>
  msg:

jean-christophe manciot

unread,
Apr 21, 2020, 12:12:46 PM4/21/20
to Ansible Project
That's what I meant (just a typo in the structure).
 
 

jean-christophe manciot

unread,
Apr 21, 2020, 12:35:22 PM4/21/20
to Ansible Project
Thanks. :-)

SandeepK

unread,
Apr 23, 2020, 2:15:29 PM4/23/20
to Ansible Project
Can we give the same information in hosts file and still work?
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Apr 24, 2020, 12:34:43 AM4/24/20
to ansible...@googlegroups.com
what do you mean. Examples with code?

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c928a179-8c43-4375-8c63-0514056d43cf%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

jean-christophe manciot

unread,
Apr 24, 2020, 4:01:30 AM4/24/20
to Ansible Project
Yes, of course: you can define the variable anywhere, including in the hosts file.
There are 2 differences though:
  • the precedence.
  • the way the variables are defined: the inventory file is not a yaml file (ini format). Refer to this post for how to define your dictionary(ies) in the hosts file.

Stefan Hornburg (Racke)

unread,
Apr 24, 2020, 4:06:08 AM4/24/20
to ansible...@googlegroups.com
On 4/24/20 10:01 AM, jean-christophe manciot wrote:
> Yes, of course: you can define the variable anywhere, including in the hosts file.
> There are 2 differences though:
>
> * the precedence
> <https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable>.
> * the way the variables are defined: the inventory file is not a yaml file (ini format). Refer to this post
> <https://stackoverflow.com/questions/29856738/how-to-define-hash-dict-in-ansible-inventory-file> for how to define
> your dictionary(ies) in the hosts file.
>

The second statement is correct only if you refer to the host file (defaults to /etc/ansible/host). The inventory for
your playbook can be written completely in YAML.

Regards
Racke

> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/8a4beb04-ffdd-44fb-b3d8-f0149eff5a61%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/8a4beb04-ffdd-44fb-b3d8-f0149eff5a61%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

SandeepK

unread,
Apr 24, 2020, 10:54:19 AM4/24/20
to Ansible Project
Thank you guys that helps!


On Friday, April 24, 2020 at 4:06:08 AM UTC-4, Stefan Hornburg (Racke) wrote:
On 4/24/20 10:01 AM, jean-christophe manciot wrote:
> Yes, of course: you can define the variable anywhere, including in the hosts file.
> There are 2 differences though:
>
>   * the precedence
>     <https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable>.
>   * the way the variables are defined: the inventory file is not a yaml file (ini format). Refer to this post
>     <https://stackoverflow.com/questions/29856738/how-to-define-hash-dict-in-ansible-inventory-file> for how to define
>     your dictionary(ies) in the hosts file.
>

The second statement is correct only if you refer to the host file (defaults to /etc/ansible/host). The inventory for
your playbook can be written completely in YAML.

Regards
        Racke

> --
> 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
Reply all
Reply to author
Forward
0 new messages