相关疑难解决方法(0)

Ansible:如何从另一个变量构造变量然后获取它的值

这是我的问题我需要使用一个变量'target_host',然后将'_host'附加到它的值,以获得另一个我需要的变量名称.如果你看看我的剧本.任务nbr 1,2,3获取变量的值,但nbr 4无法完成我的预期.在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": …
Run Code Online (Sandbox Code Playgroud)

ansible

35
推荐指数
6
解决办法
6万
查看次数

标签 统计

ansible ×1