简体   繁体   English

Ansible,检查库存组和主机

[英]Ansible, checking inventory groups and hosts

Are there any possible ways to check if registered variables (hostnames) with a loop are in inventory and print their groups?是否有任何可能的方法来检查带有循环的注册变量(主机名)是否在清单中并打印它们的组?

I am creating a role to install heartbeat with ansible role.我正在创建一个角色来安装带有 ansible 角色的心跳。 But it will be installed only on one jumphost which will ping the other.但它只会安装在一个会 ping 另一个的 jumphost 上。

Inside this role i have created separated files and put config for each host in them.在这个角色中,我创建了单独的文件并将每个主机的配置放入其中。 But i need to add some fields to to find out which group they belong to (we need it in Kibana).但是我需要添加一些字段来找出它们属于哪个组(我们在 Kibana 中需要它)。

I used Jinja templates but it paste the group that owns the host I play the role.我使用了 Jinja 模板,但它粘贴了拥有我扮演角色的主机的组。

{% for group in group_names %}
    group: {{ group }}
{% endfor %}

so I started to search if there is any way in ansible itself to do it.所以我开始搜索 ansible 本身是否有任何方法可以做到这一点。 I

- name: list of hosts
  debug:
    msg: "{{ item }}"
  with_items:
    - "{{ groups['all'] }}"

it returned hosts and i would like compare this list with list in our inventory and get their group names to paste them to the icmp.yml configs of heartbeat它返回了主机,我想将此列表与我们库存中的列表进行比较,并获取他们的组名以将它们粘贴到心跳的 icmp.yml 配置中

Q;问; Are there any possible ways to check if registered variables (hostnames) with a loop are in inventory and print their groups ?是否有任何可能的方法来检查带有循环的注册变量(主机名)是否在库存中打印它们的组

A: Yes.答:是的。 It's possible to use Special Variables可以使用特殊变量

ansible_play_hosts_all : List of all the hosts that were targeted by the play ansible_play_hosts_all : 被播放的所有主机列表

group_names : List of groups the current host is part of group_names :当前主机所属的组列表

- debug:
    msg: "{{ hostvars[item].group_names }}"
  loop: "{{ hostnames }}"
  when: item in ansible_play_hosts_all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM