Reading variables with another group

Here is an example inventory file:

[web_servers]
web_server-1 ansible_ssh_host=xxx ansible_ssh_user=yyy

[ops_servers]
ops_server-1 ansible_ssh_host=xxx ansible_ssh_user=zzz

In addition, the group web_servershas certain vars in group_vars/web_servers:

tomcat_jmx_port: 123456

How can I access tomcat_jmx_portvar when working with ops_servers?

Some of them will probably say that I need a transition group (for example all) to host shared vars, but this is just an example, in real life there are many vars with which I want to access from ops_servers, and I want everything to be clear , therefore, tomcat_jmx_portmust remain in the web_serversgroup_vars file .

Actually I need a local search.

Any ideal?

Thank you for your help.

+4
source share
2 answers

, groups, . , - web_servers:

{{ groups['web_servers'][0] }}

, - :

{{ hostvars['somehost']['ansible_default_ipv4']['address'] }}

IP- somehost.

hostvars groups, - :

{{ hostvars[groups['web_servers'][0]]['ansible_default_ipv4']['address'] }}

IP- web_servers.

+4

"" ,

- include_vars: group_vars/web_servers

: , .

{{tomcat_jmx_port}}

+2

Source: https://habr.com/ru/post/1613854/


All Articles