Ansible - read inventory hosts and variables to group_vars/all file

前端 未结 7 1798
醉酒成梦
醉酒成梦 2020-12-23 11:47

I have a dummy doubt that keeps me stuck for a long time. I have a very banal inventory file with hosts and variables:

[lb]
10.112.84.122

[tomcat]
10.112.84         


        
相关标签:
7条回答
  • 2020-12-23 12:50

    If you want to programmatically access the inventory entries to include them in a task for example. You can refer to it like this:

    {{ hostvars.tomcat }}
    

    This returns you a structure with all variables related with that host. If you want just an IP address (or hostname), you can refer to it like this:

    {{ hostvars.jboss5.ansible_ssh_host }}
    

    Here is a list of variables which you can refer to: click. Moreover, you can declare a variable and set it with for example result of some step in a playbook.

    - name: Change owner and group of some file
      file: path=/tmp/my-file owner=new-owner group=new-group
      register: chown_result
    

    Then if you play this step on tomcat, you can access it from jboss5 like this:

    - name: Print out the result of chown
      debug: msg="{{ hostvars.tomcat.chown_result }}"
    
    0 讨论(0)
提交回复
热议问题