Ansible: how to construct a variable from another variable and then fetch it's value

前端 未结 10 468
我寻月下人不归
我寻月下人不归 2020-12-02 22:50

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 pl

10条回答
  •  死守一世寂寞
    2020-12-02 23:09

    This is possible as of Ansible 2.5 with the vars lookup plugin, which I think is less likely to break without warning than some of the other methods posted here. For example:

    ---
     - name: "Example of dynamic groups"
       hosts: localhost
       vars:
         - target_host: smtp
         - smtp_host: smtp.max.com
       tasks:
         - name: testing
           debug: msg={{ lookup('vars', target_host + '_host') }}
    

    Output:

    PLAY [Example of dynamic groups] **************************************************
    
    TASK [Gathering Facts] **************************************************
    ok: [localhost]
    
    TASK [testing] **************************************************
    ok: [localhost] => {
        "msg": "smtp.max.com"
    }
    
    

提交回复
热议问题