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
    • 最新
    • 标签
主页 / server / 问题 / 1043047
Accepted
SergioLeone
SergioLeone
Asked: 2020-11-19 04:28:25 +0800 CST2020-11-19 04:28:25 +0800 CST 2020-11-19 04:28:25 +0800 CST

使用 jinja 在 Ansible 中动态创建列表

  • 772

我需要动态设置一个新的列表类型变量list var。

这是一个基本的剧本示例:

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

这显然将 设置instance_config为具有以下内容的字符串:

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

因此,虽然我得到的列表的结构是正确的,但它是一个字符串,我似乎无法将它变成一个可以作为列表的变量。我在这里想念什么?我最终需要的是一个变量:

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}
]
python ansible ansible-playbook jinja
  • 2 2 个回答
  • 5021 Views

2 个回答

  • Voted
  1. Best Answer
    SergioLeone
    2020-11-19T05:07:02+08:002020-11-19T05:07:02+08:00

    在我找到解决方案时回答我自己的问题:

    1. do在 ansible.cfg 中启用jinja 扩展:jinja2_extensions = jinja2.ext.do
    2. 替换{{ inst_config.append(node_conf) }}为{% do inst_config.append(node_conf) %}
    3. {%-使用和修剪空格-%}。

    任务的最终结果set_fact如下所示:

        - 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 }}
    
    • 1
  2. kucaahbe
    2022-03-30T06:17:22+08:002022-03-30T06:17:22+08:00

    union作为使用内置过滤器解决类似问题的替代方法:

    - hosts: localhost
      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: >-
              {{ instance_config | default([]) | union(app_instances) }}
        # just to demo that with approach could be used anywhere, without
        # worrying about overriding previously set values
        - set_fact:
            instance_config: >-
              {{ instance_config | default([]) | union(['more', 'items']) }}
    
        - debug:
            var: instance_config
    

    值得注意的是,任何阵列修改都不是为了清晰起见,而是应该易于采用。

    我的案例是关于收集 systemd 单元名称,这些名称是由几个彼此独立的角色动态生成的。

    • 0

相关问题

  • 在 cygwin/XP 下安装完整 Python 的最佳方式?

  • Mac OS X:从 python 脚本中更改 $PATH

  • 可以使用 easy_install 和 bdist_rpm 安装吗?

  • 我可以“注册”python 脚本以在 Windows 上执行吗?

  • 如何使用脚本远程重启 Windows 服务?

Sidebar

Stats

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

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve