Ansible 的 ansible_fqdn 值不一致

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

变量

{{ansible_fqdn}}
可以采用两个不同的值:服务器的短名称 (server300) 或其长名称 (server300.prod.x.y.z)。 当我们在 playbook 中使用此变量来检索文件
{{ansible_fqdn}}.crt
时,根据所选的值,无法找到该文件,并且 playbook 将失败。 为了在一系列服务器上获得一致的主机名值,是否应该使用
{{ansible_hostname}}
(来自 linux 命令
uname -n
的短主机名)和
{{inventory_hostname}}
(来自“hosts”文件的长主机名)? 或者有没有办法从
{{ansible_fqdn}}
获得一致的值?

ansible hostname
2个回答
5
投票

问:“变量 {{ ansible_fqdn }} 可以采用两个不同的值:服务器的短名称 (server300) 或其长名称 (server300.prod.x.y.z)。”

A:这取决于您如何配置主机名。看看

uname -n
。要么长

[admin@test_23 ~]$ uname -n
test_23.example.com

shell> ansible test_23 -m setup | grep ansible_fqdn
        "ansible_fqdn": "test_23.example.com",

,或者很短

[admin@test_11 ~]$ uname -n
test_11

shell> ansible test_11 -m setup | grep ansible_fqdn
        "ansible_fqdn": "test_11",

问:“应该使用 {{ ansible_hostname }} (来自 linux 命令 uname -n 的短主机名)和 {{ inventory_hostname }} (来自“hosts”文件的长主机名)吗?”

A:这取决于您如何配置库存。变量 ansible_hostname 不是必需的。如果您不运行 setup (或从 cache 获取变量),并且如果您没有显式声明它,则不会定义它,例如

shell> cat hosts
test_23

shell> ansible test_23 -m debug -a var=inventory_hostname
test_23 | SUCCESS => {
    "inventory_hostname": "test_23"
}
shell> ansible test_23 -m debug -a var=ansible_hostname
test_23 | SUCCESS => {
    "ansible_hostname": "VARIABLE IS NOT DEFINED!"
}

您可以声明 aliasansible_hostname,例如

shell> cat hosts
alias_of_test_23 ansible_hostname=test_23

shell> ansible alias_of_test_23 -m debug -a var=inventory_hostname
alias_of_test_23 | SUCCESS => {
    "inventory_hostname": "alias_of_test_23"
}
shell> ansible alias_of_test_23 -m debug -a var=ansible_hostname
alias_of_test_23 | SUCCESS => {
    "ansible_hostname": "test_23"
}

如果运行 setup,则 ansible_hostname 的值是命令

uname -n
中的短主机名,例如

shell> cat hosts
alias_of_test_23 ansible_host=test_23.example.com

shell> ansible alias_of_test_23 -m setup | grep ansible_hostname
        "ansible_hostname": "test_23",

问:“或者有没有办法从 {{ ansible_fqdn }} 获取一致的值?”

A:还有更多选择:

  • 在远程主机上,配置主机名以为您提供 FQDN

  • 在清单中,声明别名的 FQDN 形式并使用 inventory_hostname

  • 如果上述选项不可行,您必须声明一个自定义变量。

一致性取决于你。


0
投票

以下是有关主机的官方文档中的一些有趣信息:https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html#information-about-ansible-magic-variables

我建议使用您自己定义的 inventory_hostname 。 由于您还在清单中定义了地址,因此可以在主机文件中进行设置。给定一个库存片段:

---
mariadb-servers:
  hosts:
    mariadb1.example.com:
      ansible_host: 1.2.3.4 # IP for the hostname, external or internal
      ansible_user: ubuntu

  vars:
    mariadb_target_group: mariadb-servers

然后作为前置任务,你可以有类似的东西:

- name: Build hosts file
  ansible.builtin.lineinfile:
    path: /etc/hosts
    regexp: '.*{{ item }}$'
    line: "{{ hostvars[item].ansible_host }} {{ item }}"
    state: present
    mode: "0644"
  when: hostvars[item].ansible_host is defined
  loop: "{{ groups[mariadb_target_group] }}"

这样服务器的名称是一致的,并且它也将解析为您设置的内容,而不需要 DNS(您可能需要也可能不需要)。 如果需要,可以在此基础上完成将主机名正确设置为 FQDN 的其他建议。

各种工具使用不同的方式获取主机名,因此主机的设置最好保持一致。 在 Linux 上正确设置 fqdn 后,您还将获得 ansible_fqdn 的一致输出。

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