首页 > 解决方案 > Ansible object list from merging a list and a value

问题描述

Given a string list

list:
  - a
  - b

and a string

str: c

How can be obtained an object list with the same number of elements as the first list with the following structure?:

new_list:
  - list_key: a
    str_key: c
  - list_key: b
    str_key: c

Also, I'm using Ansible 2.8

标签: ansibleyamlansible-2.x

解决方案


For example

    - set_fact:
        new_list: "{{ list|
                      map('regex_replace', '^(.*)$', '{list_key: \\1}')|
                      map('from_yaml')|
                      map('combine', {'str_key': str})|
                      list }}"

推荐阅读