简体   繁体   中英

How to get a custom list from yaml file using ansible?

Using this yaml I'm trying to get a list of the 'Machine' attribute.

domainInfo:
    AdminUserName: '--FIX ME--'
    AdminPassword: '--FIX ME--'
topology:
    Name: 'wld-pil-10'
    ConfigBackupEnabled: true
    AdminServerName: 'wls-pil-10-sa-adm-n0'
    DomainVersion: 12.2.1.4.0
    ProductionModeEnabled: true
    ArchiveConfigurationCount: 20
    Cluster:
        'test-bruno-jee-r01a-c01':
            ClientCertProxyEnabled: true
            WeblogicPluginEnabled: true
    Server:
        'wls-pil-10-sa-adm-n0':
            ListenPort: 11030
            WeblogicPluginEnabled: true
            ClientCertProxyEnabled: true
            Machine: 'wlm-pil-10-n0'
        'test-bruno-jee-r01a-it-c01-m1-n1':
            ListenPort: 10022
            WeblogicPluginEnabled: true
            ClientCertProxyEnabled: true
            NMSocketCreateTimeoutInMillis: 30000
            Machine: 'wlm-pil-10-n1'
        'test-bruno-jee-r02a-it-c01-m1-n1':
            ListenPort: 10025
            WeblogicPluginEnabled: true
            ClientCertProxyEnabled: true
            NMSocketCreateTimeoutInMillis: 30000
            Machine: 'wlm-pil-10-n2'

I can get a list of the servers by putting the yaml in a variable named "yaml_domain_file" and this code:

  • set_fact: servers: "{{ yaml_domain_file.topology.Server | list }}"

I get:

ok: [wls-pil-10-sa-adm-n0] => { "msg": [ "wls-pil-10-sa-adm-n0", "test-bruno-jee-r01a-it-c01-m1-n1", "test-bruno-jee-r01a-it-c01-m1-n2" ] }

I'm trying to get the list of machines with this code:

  • debug: msg: "{{ yaml_domain_file.topology.Server.*.Machine | list }}"

but is not possible. How to get that information?

Thanks to everyone !

Try json_query

- debug:
    msg: "{{ yaml_domain_file.topology.Server|json_query('*.Machine') }}"

Q: " what If I want to put each of the servers in an array? "

A: The simplest option is dict2items filter. For example

 - set_fact:
     servers: "{{ yaml_domain_file.topology.Server|dict2items }}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM