Ansible:如何从另一个变量构造变量然后获取其值 [英] Ansible: how to construct a variable from another variable and then fetch it's value

查看:101
本文介绍了Ansible:如何从另一个变量构造变量然后获取其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,我需要使用一个变量'target_host',然后将'_host'附加到它的值上以获得另一个我需要其值的变量名. 如果你看我的剧本.任务nbr 1,2,3获取变量的值,但是nbr 4无法执行我期望的操作.还有其他方法可以达到相同效果吗?

Here is my problem I need to use one variable 'target_host' and then append '_host' to it's value to get another variable name whose value I need. If you look at my playbook. Task nbr 1,2,3 fetch the value of variable however nbr 4 is not able to do what I expect. Is there any other way to achieve the same in ansible?

   ---
    - name: "Play to for dynamic groups"
      hosts: local 
      vars:
        - target_host: smtp
        - smtp_host: smtp.max.com
      tasks:
        - name: testing
          debug: msg={{ target_host }}
        - name: testing
          debug: msg={{ smtp_host }}
        - name: testing
          debug: msg={{ target_host }}_host
        - name: testing
          debug: msg={{ {{ target_host }}_host }}


Output:

TASK: [testing] *************************************************************** 
ok: [127.0.0.1] => {
    "msg": "smtp"
}

TASK: [testing] *************************************************************** 
ok: [127.0.0.1] => {
    "msg": "smtp.max.com"
}

TASK: [testing] *************************************************************** 
ok: [127.0.0.1] => {
    "msg": "smtp_host"
}

TASK: [testing] *************************************************************** 
ok: [127.0.0.1] => {
    "msg": "{{{{target_host}}_host}}"
}

推荐答案

您需要在其周围加上引号:

You need to put quotes around it:

- hosts: local
  vars: [ target_host: smtp ]
  tasks:
    debug: msg="{{ target_host }}_host"

-编辑-

Kashyap我需要比这更上一层楼.想象有 另一个变量"smtp_host",我想在以下位置构造该变量 运行时使用另一个变量(target_host)并附加一个字符串 '_host'到它. = {{{{target_host}} _ host}} –最大值

Kashyap I need to go one more level than this. Imagine there is another variable 'smtp_host' and I want to construct that variable at runtime using another variable(target_host) and attaching a string '_host' to it. = {{ {{ target_host }}_host }} – Max

我的坏.阅读不够仔细.

My bad. Didn't read carefully enough.

无法使用此(AFAIK).阻止我们执行此操作(无论您如何旋转)的主要限制是单变量过程,而您想要的则需要多遍.

This (AFAIK) isn't possible. The primary limitation that stops us doing this (no matter how you spin it), is 'variable expansion' in ansible is a single pass process and what you want requires multiple-passes.

我能想到的只有[严重hacky]方式:

Only [seriously hacky] ways I can think of are:

  • 使用template从您的剧本动态创建剧本并执行.
  • 听说 Jinja2引擎进行了多次通过评估.如果将这些字符串放在模板中,然后使用lookup('template', ...)过滤器,则可能是这样.不幸的是,我没有使用Jinja2模板的经验,因此不确定是否可以选择.
  • Create the playbook dynamically from your playbook using template and execute it.
  • I heard that Jinja2 engine does multi-pass evaluation. May be if you put these strings in a template and then use the lookup('template', ...) filter. Unfortunately I have no experience with Jinja2 templates so not quite sure if this is even an option.

这篇关于Ansible:如何从另一个变量构造变量然后获取其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆