简体   繁体   English

Ansible playbook 中的主机名检查

[英]Hostname check in Ansible playbook

I have some ansible playbooks I use for linux clients.我有一些用于 linux 客户端的 ansible 剧本。 The connection in to the environment is over VPN, and each connected client gets an IP from a pool.与环境的连接是通过 VPN 进行的,每个连接的客户端都从池中获取 IP。 The IP assigned is the first available.分配的 IP 是第一个可用的。 The clients update DNS using SSSD.客户端使用 SSSD 更新 DNS。 This leads to the same IP being assigned in to more then one host in DNS, and a playbook may run on the wrong system.这导致相同的 IP 被分配给 DNS 中的多个主机,并且剧本可能在错误的系统上运行。

Is there a way to check and fail if the hostname on the client does not match the hostname in the inventory?如果客户端上的主机名与清单中的主机名不匹配,有没有办法检查并失败? Enabling strict host key checking would solve this partly, but I'd like a proper check to ensure the playbooks runs on the correct client.启用严格的主机密钥检查将部分解决此问题,但我希望进行适当的检查以确保剧本在正确的客户端上运行。

After KamilCuk´s comment I looked in to ansible_facts and added a comparison between fqdn and inventory_hostname .在 KamilCuk 发表评论后,我查看了ansible_facts并添加了 fqdn 和inventory_hostname之间的比较。 My end result in the playbook is:我在剧本中的最终结果是:

- name: Check inventory_hostname to fqdn
  assert:
    that:
      - inventory_hostname == ansible_facts["fqdn"]
    fail_msg: Hostname does not match inventory
    quiet: yes

Even though some clients no show up as "green" in the gathering facts section, if they are not the correct client it will fail in the assert section.即使某些客户端在收集事实部分中没有显示为“绿色”,但如果它们不是正确的客户端,它将在断言部分中失败。

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

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