AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 743056
Accepted
Rafal Niznik
Rafal Niznik
Asked: 2023-04-16 21:40:58 +0800 CST2023-04-16 21:40:58 +0800 CST 2023-04-16 21:40:58 +0800 CST

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

  • 772

我有以下词典:

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与answer.key匹配,以便输出看起来像这样?最好调整 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
ansible
  • 1 1 个回答
  • 49 Views

1 个回答

  • Voted
  1. Best Answer
    Vladimir Botka
    2023-04-17T00:56:50+08:002023-04-17T00:56:50+08:00

    创建答案

      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

        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
    

    在生产中,将声明放入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}) }}"
    

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

    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
    
    • 2

相关问题

  • 在ansible中跳过一些带有提示的任务[关闭]

  • ansible中事实变量的分隔符值

  • 剧本给出语法错误

  • sshpass 在 alpine linux 中不起作用

  • Ansible shell 模块空响应

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve