linux 将包含相同键的多个yaml文件中的值追加到列表变量

uubf1zoe  于 5个月前  发布在  Linux
关注(0)|答案(1)|浏览(97)

多个yaml文件包含一个键为id的值,我需要将其附加到列表变量中。

---
name: somename
group: somegroup
id: someid

字符串
我尝试过使用ansible.builtin.command来cat和注册文件的内容,但是没有成功地找到一种方法来过滤返回的stdout_lines以获得我想要的值。我的尝试看起来像下面这样,但是在这一点上有太多的迭代,所以它有点混乱。

- name: Store job definition content
  local_action:
    module: ansible.builtin.command
    cmd: "cat {{ item.path }}"
  loop:
    "{{ definition_files.files }}"
  register:
    job_definitions

- name: Get a list of existing jobs ids
  ansible.builtin.set_fact:
    existing_jobs: "{{ existing_jobs + (item.stdout | to_yaml | map(attribute='id')}}"
  loop:
    "{{ job_definitions.results }}"


另一个例子也是不正确的,不工作,但演示了我一直在尝试得到我的结果:

- name: Define empty list
  ansible.builtin.set_fact:
    uuid_list: []

- name: Create a list of job ids from template files
  ansible.builtin.set_fact:
    uuid_list: "{{uuid_list + lookup('file', item.path | to_yaml).id }}"
  loop:
    "{{ definition_files.files }}"

watbbzwu

watbbzwu1#

给定以下测试目录布局

.
├── definition_files
│   ├── def1.yaml
│   └── def2.yaml
└── test.yaml

字符串
测试定义文件的内容
第一个月

name: name1
group: group1
id: abc


definition_files/def2.yaml

name: name2
group: group2
id: def


以下test.yaml行动手册

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: append ids from each files
      set_fact:
        my_id_list: "{{ my_id_list | d([]) + [(lookup('file', item) | from_yaml).id] }}"
      loop: "{{ q('fileglob', 'definition_files/*.yaml' ) }}"

    - debug:
        var: my_id_list


提供:

$ ansible-playbook test.yaml 

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [append ids from each files] *********************************************************************************************************************************************************************************
ok: [localhost] => (item=/tmp/test/definition_files/def2.yaml)
ok: [localhost] => (item=/tmp/test/definition_files/def1.yaml)

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "my_id_list": [
        "def",
        "abc"
    ]
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


最新的测试是使用以下ansible版本进行的:

$ ansible-playbook --version
ansible-playbook [core 2.16.1]
  config file = None
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/user/.local/bin/ansible-playbook
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True

相关问题