大网数据广告活动
服务器运维问答

Dynamically creating a list in Ansible with jinja

· 52CE 我爱测

I need to dynamically set a new list type variable list var. Here's a basic playbook example: vars: app_instances: - host_name: host1-domain inst_count: 3 - host_name: host2-domain inst_count: 1 - host_name: host3-domain inst_cou…

问题原文

原帖链接 · SergioLeone · CC BY-SA 4.0

I need to dynamically set a new list type variable list var.

Here's a basic playbook example:

  vars:
    app_instances:
      - host_name: host1-domain
        inst_count: 3
      - host_name: host2-domain
        inst_count: 1
      - host_name: host3-domain
        inst_count: 1

  tasks:
    - set_fact:
        instance_config: >-
          {% set inst_config = [] %}
          {% for inst in app_instances %}
            {% for inst_num in range(inst.inst_count) %}
              {% set node_number = inst.host_name.split('-') | first | replace('host', '') %}
              {% set host_name = "host_name" %}
              {% set host_num = "host_num" %}
              {% set inst_name = "inst_name" %}
              {% set node_conf = { host_name: inst.host_name, host_num: node_number, inst_name: inst_num+1 } %}
              {{ inst_config.append(node_conf) }}
            {% endfor %}
          {% endfor %}
          {{ inst_config|join(",") }}

    - debug:
        msg: "{{ instance_config }}"

This obviously sets the instance_config to a string with the following contents:

"instance_config": "                           \n                          \n                          \n                            \n                            \n   {'host_name': 'host1-domain', 'host_num': '1', 'inst_name': 1},{'host_name': 'host1-domain', 'host_num': '1', 'inst_name': 2},{'host_name': 'host1-domain', 'host_num': '1', 'inst_name': 3},{'host_name': 'host2-domain', 'host_num': '2', 'inst_name': 1},{'host_name': 'host3-domain', 'host_num': '3', 'inst_name': 1}"

So while the structure of the list that I'm getting is correct, it's a String and I can't seem to make this into a variable that would be a list instead. What Am I missing here? What I need eventually is a variable:

instance_config = [
  {'host_name': 'host1-domain', 'host_num': '1', 'inst_name': 1},
  {'host_name': 'host1-domain', 'host_num': '1', 'inst_name': 2},
  {'host_name': 'host1-domain', 'host_num': '1', 'inst_name': 3},
  {'host_name': 'host2-domain', 'host_num': '2', 'inst_name': 1},
  {'host_name': 'host3-domain', 'host_num': '3', 'inst_name': 1}
]

已采纳解答

原帖链接 · SergioLeone · CC BY-SA 4.0

Answering my own question as I have found a solution:

  1. enable do jinja extension in ansible.cfg: jinja2_extensions = jinja2.ext.do
  2. replace {{ inst_config.append(node_conf) }} with {% do inst_config.append(node_conf) %}
  3. trim whitespaces using {%- and -%}.

The final result of the set_fact task looks like this:

    - set_fact:
        instance_config: >-
          {%- set instance_config = [] -%}
          {%- for inst in app_instances -%}
            {%- for inst_num in range(inst.inst_count) -%}
              {%- set node_number = inst.host_name.split('-') | first | replace('host', '') | int -%}
              {%- set host_name = "host_name" -%}
              {%- set host_num = "host_num" -%}
              {%- set inst_name = "inst_name" -%}
              {%- set node_conf = { host_name: inst.host_name, host_num: node_number, inst_name: inst_num+1 } -%}
              {%- do instance_config.append(node_conf) -%}
            {%- endfor -%}
          {%- endfor -%}
          {{ instance_config }}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-11-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。