简体   繁体   中英

Hostname check in Ansible playbook

I have some ansible playbooks I use for linux clients. The connection in to the environment is over VPN, and each connected client gets an IP from a pool. The IP assigned is the first available. The clients update DNS using SSSD. 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.

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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