3

The variable {{ansible_fqdn}} can take two different values: the server's short name (server300) or its long name (server300.prod.x.y.z). When we use this variable in the playbook to retrieve a file {{ansible_fqdn}}.crt, depending on the value chosen, the file can be not found, and the playbook will fail. To have a consistent value for hostnames over a list of servers, should one rather use {{ansible_hostname}} (short hostname from linux command uname -n) and {{inventory_hostname}} (long hostname from "hosts" file) instead? Or is there a way to obtain a consistent value from {{ansible_fqdn}}?

RotS
  • 2,142
  • 2
  • 24
  • 30
  • 1
    Those are all different values for different situation and the one to use depends on your exact requirement. `ansible_fqdn` and `ansible_hostname` are collected during facts gathering after a successful connection to the target. Their content come from calling different functions. See https://github.com/ansible/ansible/issues/38777 for a start of a discussion. Anyway, the content will be the one you discover. `inventory_hostname` is the name you set yourself in your inventory. If that name contains dots, you can use `inventory_hostname_short` which will return the chars before the first dot. – Zeitounator Feb 24 '22 at 17:13

1 Answers1

3

Q: "The variable {{ ansible_fqdn }} can take two different values: the server's short name (server300) or its long name (server300.prod.x.y.z)."

A: It depends on how you configure hostname. Take a look at uname -n. Either it is long

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

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

, or it is short

[admin@test_11 ~]$ uname -n
test_11

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

Q: "Should one rather use {{ ansible_hostname }} (short hostname from linux command uname -n) and {{ inventory_hostname }} (long hostname from "hosts" file) instead?"

A: It depends on how you configure the inventory. The variable ansible_hostname is not required. If you don't run setup (or get the variables from a cache) and if you don't declare it explicitly it won't be defined, e.g.

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!"
}

You can declare alias and ansible_hostname, e.g.

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"
}

If you run setup the value of ansible_hostname is the short hostname from the command uname -n, e.g.

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",

Q: "Or is there a way to obtain a consistent value from {{ ansible_fqdn }}?"

A: There are more options:

  • At remote hosts, configure hostnames to provide you with the FQDN

  • In the inventory, declare the FQDN form of aliases and use inventory_hostname

  • If the options above are not feasible you'll have to declare a custom variable.

The consistency is up to you.

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63