Ansible:如何更新与另一个目录键匹配的数组键值

Ansible:如何更新与另一个目录键匹配的数组键值

我有以下词典:

question:
  first_run:
    app:
      - answer: null
        name: first_name
        question: What is your First name?
      - answer: null
        name: last_name
        question: What is your Last name?
    core:
      - question: question1
      - question: question2
answer:
  first_name: John
  last_name: Smith

我使用以下任务来手动更新回答下的值Question.first_run.app效果很好

- name: Update question variable
  set_fact:
    question:
      first_run:
        app:
          - name: first_name
            question: "What is your First name?"
            answer: "{{ answer.first_name }}"
          - name: last_name
            question: "What is your Last name?"
            answer: "{{ answer.last_name }}"

我还测试了以下任务(对于这种情况,这将是更可取的方法)

- name: "TEST-1"
  set_fact:
    question: "{{ question | combine({'first_run': {'app': question.first_run.app | map('combine', {'answer': update }) | list }}, recursive=True) }}"
  loop: "{{ question.first_run.app }}"
  vars:
    update: "{{ answer[item.name] if item.name in answer.keys() else item.answer }}"

- name: "TEST-2"
  set_fact:
    question: "{{ question | combine({'first_run': {'app': question.first_run.app | map('combine', {'answer': (answer[item.name])})}}, recursive=True) }}"
  loop: "{{ question.first_run.app }}"
  vars:c
    update: "{{ answer | dict2items | selectattr('key', 'in', [item.name]) | map(attribute='value') | first }}"
#    update: "{{ answer[item.name] }}"    # THis also works

然而 TEST-1 和 TEST-2 的输出总是这样的:

question:
  first_run:
    app:
      - answer: Smith
        name: first_name
        question: What is your First name?
      - answer: Smith
        name: last_name
        question: What is your Last name?
    core:
      - question: question1
      - question: question2

我测试的最后一个任务是这样的:
它可以工作,但所需的结果保存在新的 var app_list 下

- name: "TEST-3"
  set_fact:
    question: "{{ question | combine({'first_run': {'app': app_list}}, recursive=True) }}"
  vars:
    app_list: []
  loop: "{{ question.first_run.app }}"
  set_fact:
    app_list: "{{ app_list + [item | combine({'answer': answer[item.name]})] }}"

问题:

我需要更新任务“更新问题变量”,这样我就不必手动放置数组中的所有这些键Question.first_run.app

如何使用循环来匹配Question.first_run.app.name答案键所以输出会是这样的?最好调整TEST-1或TEST-2或TEST-3

question:
  first_run:
    app:
      - answer: John
        name: first_name
        question: What is your First name?
      - answer: Smith
        name: last_name
        question: What is your Last name?
    core:
      - question: question1
      - question: question2

答案1

创建答案

  answers: "{{ question.first_run.app|
               map(attribute='name')|
               map('extract', answer)|
               map('community.general.dict_kv', 'answer') }}"

给出

  answers:
  - answer: John
  - answer: Smith

更新字典应用程序

  app_update: "{{ question.first_run.app|
                  zip(answers)|
                  map('combine') }}"

给出

  app_update:
  - answer: John
    name: first_name
    question: What is your First name?
  - answer: Smith
    name: last_name
    question: What is your Last name?

更新第一次运行

    first_run_update: "{{ question.first_run|
                          combine({'app': app_update}) }}"

给出

  first_run_update:
    app:
    - answer: John
      name: first_name
      question: What is your First name?
    - answer: Smith
      name: last_name
      question: What is your Last name?
    core:
    - question: question1
    - question: question2

最后更新一下问题

    - set_fact:
        question: "{{ question|
                      combine({'first_run': first_run_update}) }}"

给你想要的

  question:
    first_run:
      app:
      - answer: John
        name: first_name
        question: What is your First name?
      - answer: Smith
        name: last_name
        question: What is your Last name?
      core:
      - question: question1
      - question: question2

用于测试的完整剧本示例

- name: Substitute attribute *answer* by *name*
  hosts: localhost

  vars:

    question:
      first_run:
        app:
        - answer: null
          name: first_name
          question: What is your First name?
        - answer: null
          name: last_name
          question: What is your Last name?
        core:
        - question: question1
        - question: question2

    answer:
      first_name: John
      last_name: Smith

    answers: "{{ question.first_run.app|
                 map(attribute='name')|
                 map('extract', answer)|
                 map('community.general.dict_kv', 'answer') }}"
    app_update: "{{ question.first_run.app|
                    zip(answers)|
                    map('combine') }}"
    first_run_update: "{{ question.first_run|
                          combine({'app': app_update}) }}"
    

  tasks:

    - debug:
        var: answers
    - debug:
        var: app_update
    - debug:
        var: first_run_update

    - set_fact:
        question: "{{ question|
                      combine({'first_run': first_run_update}) }}"
    - debug:
        var: question

在生产中,将声明放入组变量

shell> cat group_vars/all/answers.yml 
answers: "{{ question.first_run.app|
             map(attribute='name')|
             map('extract', answer)|
             map('community.general.dict_kv', 'answer') }}"
app_update: "{{ question.first_run.app|
                zip(answers)|
                map('combine') }}"
question_update:
  first_run: "{{ question.first_run|
                 combine({'app': app_update}) }}"

将数据放入变量。例如,创建一个文件

shell> cat qa.yml 
question:
  first_run:
    app:
    - answer: null
      name: first_name
      question: What is your First name?
    - answer: null
      name: last_name
      question: What is your Last name?
    core:
    - question: question1
    - question: question2

answer:
  first_name: John
  last_name: Smith

,并简化剧本

shell> cat pb.yml 
- hosts: all
  vars_files:
    - qa.yml
  tasks:
    - set_fact:
        question: "{{ question|combine(question_update) }}"
    - debug:
        var: question

运行剧本

shell> ansible-playbook -l localhost pb.yml

相关内容