【问题标题】:How to get a custom list from yaml file using ansible?如何使用 ansible 从 yaml 文件中获取自定义列表?
【发布时间】:2020-12-21 12:38:55
【问题描述】:

使用这个 yaml 我正在尝试获取“机器”属性的列表。

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'

我可以通过将 yaml 放入名为“yaml_domain_file”的变量和以下代码中来获取服务器列表:

  • 设置事实: 服务器:“{{ yaml_domain_file.topology.Server | list }}”

我明白了:

好的:[wls-pil-10-sa-adm-n0] => { “味精”:[ “wls-pil-10-sa-adm-n0”, “test-bruno-jee-r01a-it-c01-m1-n1”, “test-bruno-jee-r01a-it-c01-m1-n2” ] }

我正在尝试使用此代码获取机器列表:

  • 调试: 味精:“{{ yaml_domain_file.topology.Server.*.Machine | list }}”

但不可能。如何获取这些信息?

谢谢大家!

【问题讨论】:

    标签: ansible yaml jinja2


    【解决方案1】:

    试试 json_query

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

    问:如果我想将每个服务器放在一个数组中怎么办?

    A:最简单的选项是dict2items 过滤器。例如

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

    【讨论】:

    • 是否可以将“topology”的yaml分支放入变量中?
    • - set_fact: my_var="{{ yaml_domain_file.topology }}" 大概?
    • 如果我想把每台服务器放在一个数组中怎么办?
    • 有很多选择。例如,请参阅dict2items。下一个选项是修改json_query
    • 这样的? - 名称:收集服务器变量的详细信息:servers_detail: [] set_fact: servers_detail: "{{ yaml_domain_file.topology.Server | json_query([item]) }}" with_items: "{{ servers }}"
    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2018-06-21
    相关资源
    最近更新 更多