problem to fetch value from var files

7 views
Skip to first unread message

Eric Chang

unread,
Jun 27, 2019, 5:17:07 AM6/27/19
to Ansible Project
I have a yml file named: client_hosts.yml
it looks like below

client_hosts:
  abc.com:
  - host: dc021
    ipv4: 192.168.11.21
  - host: dc022
    ipv4: 192.168.11.22
  - host: dc023
    ipv4: 192.168.11.23
    iuser: True
    user: john
  - host: dc024
    ipv4: 192.168.11.24
  - host: dc025
    ipv4: 192.168.11.25
    iuser: True
    user: Mary
  - host: dc026
    ipv4: 192.168.11.26
  - host: dc027
    ipv4: 192.168.11.27
  - host: dc028
    ipv4: 192.168.11.28
  - host: dc029
    ipv4: 192.168.11.29

I want to select user with iuser=True
so I create a playbook , include the file

with the code below , I can get a list.

- name: load client_host
  include_vars:
    file: client_hosts.yml
    name: ch

- name: get user from yml
  debug:
    msg: "{{ ch['client_hosts']['abc.com']}}"


ok: [dc053.abc.com] => {
    "msg": [
        {
            "host": "dc026",
            "ipv4": "192.168.11.26",
            "iuser": "True",
            "user": [
                "joy"
            ]
        },
        {
            "host": "dc027",
            "ipv4": "192.168.11.27"
        },
        {
            "host": "dc028",
            "ipv4": "192.168.11.28"
        },
        {
            "host": "dc032",
            "hwaddress": "30:85:a9:a3:f4:1d",
            "ipv4": "192.168.11.32"
        },
        {
            "host": "dc033",
            "hwaddress": "d8:cb:8a:c3:6a:f8",
            "ipv4": "192.168.11.33"
        },
        {
            "host": "dc034",
            "hwaddress": "70:4d:7b:a3:66:f1",
            "ipv4": "192.168.11.34",
            "iuser": "False",
            "user": [
                "changch"
            ]
        }
    ]
}


but then how do I fecth the user value only when iuser is True ?



Kai Stian Olstad

unread,
Jun 27, 2019, 7:45:15 AM6/27/19
to ansible...@googlegroups.com
{{ ch['client_hosts']['abc.com'] | selectattr('iuser', 'defined') |
selectattr('iuser') | list }}


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages