首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >懒惰地评价变量

懒惰地评价变量
EN

Stack Overflow用户
提问于 2022-01-24 10:32:13
回答 1查看 344关注 0票数 1

我有一个变量引用,当Playbook启动和引用被评估时,这个变量是未知的。是否有一种在实际访问变量时延迟加载该变量的方法?

在清单中:

代码语言:javascript
复制
all:
  hosts:
    l01lin08:
      bamboo_agents_it:
        - bamboo_agent_app_name: bamboo-agent-it
          bamboo_agent_install_dir: /data/bamboo-agent-it
          bamboo_agent_capabilities: "{{ bamboo_agent_capabilities_it }}"
        - # ...
        - # ...
      bamboo_agent_capabilities_it:
        # Error: bamboo_agent_install_dir is not known when evaluating the variable
        capabilitiy: '{{ bamboo_agent_install_dir }}/bla/blubb'
        another.one: 123
        is.something: true

bamboo_agent_capabilities_it.capabilitybamboo_agents_it[].bamboo_agent_install_dir的引用在此上下文中不为所知。

功能是一组键值对,它可以是所有的东西。这些值并不总是路径,键也不总是相同的。因此,我无法判断何时添加父路径,何时不在目标任务中。

在剧本中,我在bamboo_agents_it循环中调用一个角色

代码语言:javascript
复制
  tasks:
    Install all bamboo agents
    - include_role:
        name: bamboo-agent-linux
      vars:
        bamboo_agent_app_name: '{{ item.bamboo_agent_app_name }}'
        bamboo_agent_install_dir: '{{ item.bamboo_agent_install_dir }}'
        bamboo_agent_capabilities: '{{ item.bamboo_agent_capabilities }}'
      loop: '{{ bamboo_agents_it }}'

在这个循环中,item.bamboo_agent_install_dir是可用的,但是我希望有一个通用的配置定义。我不想把这些单钥匙分开处理。

  • 是否有一种通用的方法来解决这个问题?
  • 也许可以在访问它时在任务中加载变量吗?

我使用的一个解决方法是替换一个占位符。但我希望有更多的习惯方式:

代码语言:javascript
复制
# Inventory
      bamboo_agent_capabilities_it:
        # Error: bamboo_agent_install_dir is not known when evaluating the variable
        capabilitiy: '[bamboo_agent_install_dir]/bla/blubb'

# Task:
  lineinfile:
    path: '{{ bamboo_agent_install_dir }}/bin/bamboo-capabilities.properties'
    line: '{{ item.key }}={{ item.value | replace("[bamboo_agent_install_dir]", bamboo_agent_install_dir) }}'
  loop: '{{ bamboo_agent_default_capabilities | dict2items + bamboo_agent_capabilities | dict2items }}'
EN

回答 1

Stack Overflow用户

发布于 2022-01-24 12:17:44

您可以做的是声明您希望该变量以以下名称命名的名称:

代码语言:javascript
复制
bamboo_agent_capabilities_it:
  capability: /foo/bar
  dir: bamboo_agent_install_dir

然后使用vars lookup获取该变量的值,例如:

代码语言:javascript
复制
bamboo_agent_capabilities: "{{ lookup('vars', item.bamboo_agent_capabilities.dir) }}{{ item.bamboo_agent_capabilities.capability }}" 

考虑到剧本:

代码语言:javascript
复制
- hosts: localhost
  gather_facts: no
  vars:
    bamboo_agents_it:
      - bamboo_agent_capabilities: "{{ bamboo_agent_capabilities_it }}"
    bamboo_agent_capabilities_it:
      capability: /foo/bar
      dir: bamboo_agent_install_dir

  tasks:
    - set_fact:
        bamboo_agent_install_dir: /path/to/dir

    - debug:
        msg: 
          bamboo_agent_capabilities: "{{ lookup('vars', item.bamboo_agent_capabilities.dir) }}{{ item.bamboo_agent_capabilities.capability }}" 
      loop: "{{ bamboo_agents_it }}"

这产生了:

代码语言:javascript
复制
PLAY [localhost] ***************************************************************************************************

TASK [set_fact] ****************************************************************************************************
ok: [localhost]

TASK [debug] *******************************************************************************************************
ok: [localhost] => (item={'bamboo_agent_capabilities': {'capability': '/foo/bar', 'dir': 'bamboo_agent_install_dir'}}) => 
  msg:
    bamboo_agent_capabilities: /path/to/dir/foo/bar

PLAY RECAP *********************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70832381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档