【问题标题】:loop over a yaml to get a specific key value in ansible-playbook遍历 yaml 以获取 ansible-playbook 中的特定键值
【发布时间】:2017-08-24 05:00:39
【问题描述】:

我有一个结构如下的 yaml 文件 (abc.yaml):

- my-container-sanity-testing:
    tier: 0
    phase: 1
    provision_windows: True
    resource_group_type: openstack
    resource_groups:
      - name: 'container_0'
        role: 'atomic'
        count: 1
        image: 'fedora-atomic-cloud-released-latest'
      - name: 'windows_0'
        role: 'ad'
        count: 1
        image: 'win-2012-r2'

我有一个如下任务的剧本。

-name: Get test metadata
 set_fact: 
  test_metadata="{{ lookup('file', '{{ playbook_dir }}/abc.yaml') | from_yaml }}"

假设我已经知道什么特定的列表值(my-container-sanity-testing)并检查列表值(它是一个字典)是否具有键 provision_windows: True,我如何循环遍历 yaml。

以上任何提示都会有所帮助。

【问题讨论】:

    标签: ansible yaml


    【解决方案1】:

    Ansible 使用 Jinja2 模板,因此您可以使用 filters,在本例中为 selectattr filter:

    - debug:
        msg: "{{ test_metadata | selectattr('provision_windows') | list }}"
    

    ==> 这将显示test_metadata 中属性provision_windows 设置为True 的所有元素

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      相关资源
      最近更新 更多