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

前端 未结 10 448
我寻月下人不归
我寻月下人不归 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"
    }
    
    
    0 讨论(0)
  • 2020-12-02 23:11

    Seems to me you can just use the var option instead of msg:

      debug: var="{{ target_host }}_host"
    

    Gives:

    TASK [testing] ********************************************************************************************************************************
    ok: [localhost] => {
        "smtp_host": "smtp.max.com"
    }
    
    0 讨论(0)
  • 2020-12-02 23:12

    You can nest your lookups like so:

    ---
    - hosts: local
      connection: local
      gather_facts: no
      vars:
        target_host: smtp
        lookup_host: "{{ target_host }}_host"
        smtp_host: smtp.max.com
      tasks:
        - debug: var="{{ lookup_host }}"
    
    0 讨论(0)
  • 2020-12-02 23:14

    I'm currently using the array-like syntax of Jinja 2. I don't think this is a great solution, but I've yet to find something better.

    Let me give an example with one of my abstracted tasks. See my variable configuration and example task below:

    # Variables file, available in the task context
    containers:
      app:
        image: mynamespace/myappcontainer:snapshot
      web:
        image: nginx:latest
      db:
        image: mariadb:latest
    
    
    
    # Example task
    - name: Start containers
      docker_container:
        name: "{{ item }}"
        image: "{{ containers[item].image }}"
      with_items:
        - app
        - web
        - db
    

    In the above example I'm using the with_items Ansible loop, which runs the task for each item and makes the {{ item }} variable available accordingly.
    This results in creating 3 Docker containers each with the proper container name based on the items list, and the proper image retrieved from the external variables I've configured.

    Even though this example uses with_items, it is of course adaptable to your problem with use of your own variables.

    Although this works perfectly fine in this situation, I'm afraid this requires the variables you'd like to access to be part of some parent variable (containers in this example). Therefore I'd recommend to split variables with a . to construct a hierarchy, and not with a _.

    A variable like a.b.c where b is dynamic, would be accessible using a[b].c.
    A variable like a.b where b is dynamic, would be accessible using a[b].


    A solution you would use might look like (untested):

    - name: "Play to for dynamic groups"
      hosts: local 
      vars:
        - target: smtp
        - hosts:
            smtp: smtp.max.com
            imap: imap.max.com
      tasks:
        - name: testing
          debug: msg={{ hosts[target] }}
    

    Note that the variables are configured slightly differently, because it's structure is hierarchical.

    0 讨论(0)
提交回复
热议问题