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

前端 未结 10 489
我寻月下人不归
我寻月下人不归 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 22:51

    You have two ways to choose:
    1. General using.

    vars:
        - target_host: smtp
        - smtp: smtp.max.com
    tasks: 
        - name: testing
            debug: msg={{ target_host }}
        - name: testing
            debug: msg={{ smtp }}
        - name: testing
            debug: msg={{ vars[target_host] }}
    

    2. Using fact

    tasks: 
        - set_fact: target_host=smtp
        - set_fact: smtp=smtp.max.com
        - name: testing
            debug: msg={{ target_host }}
        - name: testing
            debug: msg={{ smtp }}
        - name: testing
            debug: msg={{hostvars[inventory_hostname][target_host]}}
    

提交回复
热议问题