查询 DNS 但如果未找到 DNS 条目则回退到 IP 地址的可靠逻辑?

问题描述 投票:0回答:1

查询 DNS 但如果没有找到 DNS 条目则回退到 IP 地址的 Ansible 逻辑?

我有一个小型的 ansible 剧本,可以捕获事实并将其转储到文件中,如下所示:

---
- hosts: Configuration
  gather_facts: yes
  become: true
  tasks:
    - name: Gather Facts
      setup:

    - name: Capture Facts
      delegate_to: localhost
      run_once: true
      copy:
        content: "{{ hostvars[inventory_hostname] | to_nice_json }}"
        dest: /tmp/Capture-{{ ansible_default_ipv4.address }}.json

主机“配置”正是我们在任何给定时间配置的内容。

但我更愿意使用 DNS 名称作为捕获文件名的一部分(如果存在)。鉴于我们的配置,有时会在我们的环境中更新 DNS 之前进行配置,例如实验室工作等。

在ansible中,也许可以进行查找(dig,ansible_default_ipv4.address)?但是作为 ansible 的新手,不确定如何在查找为空时创建条件检查?然后默认为ip地址?

ansible dns
1个回答
0
投票

不完全是我所追求的,但这有效......

- hosts: Configuration
  gather_facts: yes
  become: true
  tasks:
    - name: Gather Facts
      setup:

    - debug:
        msg: "{{ ansible_fqdn }} : {{ ansible_hostname  }} : {{ ansible_default_ipv4.address }}"

    - name: Capture Facts Via IP Address
      delegate_to: localhost
      #run_once: true
      copy:
        content: "{{ hostvars[inventory_hostname] | to_nice_json }}"
        dest: /tmp/Capture-{{ ansible_hostname }}-{{ ansible_default_ipv4.address }}.json
      when: ansible_hostname == ansible_fqdn

    - name: Capture Facts Via FQDN
      delegate_to: localhost
      #run_once: true
      copy:
        content: "{{ hostvars[inventory_hostname] | to_nice_json }}"
        dest: /tmp/Capture-{{ ansible_hostname }}-{{ ansible_fqdn }}.json
      when: ansible_hostname != ansible_fqdn

实验室环境配置会进行原始镜像以提高速度,因此每台机器、docker 镜像、VM 等都是镜像,直到以后进行定制。因此,如果图像主机名是“模板”,则在配置后的实验室中我们将有很多“模板”系统。因此,当 DNS 等待相关实验室配置时,上述方法有效。当然,如果 DNS 提前配置,ansible_fqdn 应该是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.