ansible - how to create a list of dicts, based on a list of strings? - Stack Overflow

I am struggling with Ansible data transformation. Would need to create a list of dicts - for haproxy co

I am struggling with Ansible data transformation. Would need to create a list of dicts - for haproxy config - out of a list of hostnames. For example

backends:
  - web1
  - web2
  - web3

This is a plain simle list of hostnames. I would need to convert it into a list of dicts, like this:

backends_dict:
  - name: web1
    address: web1:80
  - name: web2
    address: web2:80
  - name: web3
    address: web3:80

I tried using map, dict, zip and combine filters in all combinations I could think of, also tried to get help from ChatGPT, but its all possible solution went into error. I think there must be a simple way to achieve this.

I am struggling with Ansible data transformation. Would need to create a list of dicts - for haproxy config - out of a list of hostnames. For example

backends:
  - web1
  - web2
  - web3

This is a plain simle list of hostnames. I would need to convert it into a list of dicts, like this:

backends_dict:
  - name: web1
    address: web1:80
  - name: web2
    address: web2:80
  - name: web3
    address: web3:80

I tried using map, dict, zip and combine filters in all combinations I could think of, also tried to get help from ChatGPT, but its all possible solution went into error. I think there must be a simple way to achieve this.

Share Improve this question edited Mar 10 at 9:24 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Mar 10 at 9:15 TrifoTrifo 1335 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Create a list of addresses

  _port: 80
  address: "{{ backends | product([_port]) | map('join', ':') }}"

gives

  address:
    - web1:80
    - web2:80
    - web3:80

Create a dictionary

  backends_dict: "{{ dict(backends | zip(address)) }}"

gives

  backends_dict:
    web1: web1:80
    web2: web2:80
    web3: web3:80

You can convert it to a list

backends_list: "{{ backends_dict | dict2items(key_name='name',
                                              value_name='address') }}"

gives what you want

  backends_list:
    - address: web1:80
      name: web1
    - address: web2:80
      name: web2
    - address: web3:80
      name: web3

Example of a complete playbook for testing

- hosts: localhost

  vars:

    backends: [web1, web2, web3]
    port: 80
    address: "{{ backends | product([port]) | map('join', ':') }}"
    backends_dict: "{{ dict(backends | zip(address)) }}"
    backends_list: "{{ backends_dict | dict2items(key_name='name',
                                                   value_name='address') }}"

  tasks:

    - debug:
        var: address

    - debug:
        var: backends_dict

    - debug:
        var: backends_list

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744854336a4597317.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信