1

I have the following dictionaries:

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

I use the following tasks to manually update answer values under question.first_run.app which works fine

- 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 }}"

I have tested also following tasks (this would be more preferable approach for this scenario)

- 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

However the output from TEST-1 and TEST-2 is always like this:

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

Last taks I have tested was this:
It works but required result is saved under new 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:

I need to update the task "Update question variable" so I do not have to manually place all these keys from the array question.first_run.app

How do I use a loop to match question.first_run.app.name with answer.key so the output would look like this? Best to adjust TEST-1 or TEST-2 or 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
  • Marching or matching?
    – RonJohn
    Apr 16, 2023 at 18:19

1 Answer 1

2

Create the answers

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

gives

  answers:
  - answer: John
  - answer: Smith

Update the dictionary app

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

gives

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

Update first_run

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

gives

  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

Finally, update question

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

gives what you want

  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

Example of a complete playbook for testing

- 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

In production, put the declarations into the group_vars

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}) }}"

Put the data into the vars. For example, create a file

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

, and simplify the playbook

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

Run the playbook

shell> ansible-playbook -l localhost pb.yml
2
  • This work perfect thanks for example! However I need to keep answers the way they are instead of converting them to array. Examples from TEST-1 and TEST-2 is almost there. But the same value is inserted under answer key. How we can adjust TEST-1 or TEST-2 or TEST-3 Apr 21, 2023 at 10:04
  • Make sure what you want to keep as is. I'm not touching your dictionary answer. I use it to create a new temporary list answers (plural). Change the name of this list to whatever you like better. Apr 21, 2023 at 10:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .