Search code examples
ansibleansible-2.xdomain-nameansible-facts

Ansible: ansible_domain or facter_domain - How to get domain value of a hostname mentioned in inventory file


Ansible 1.9.2 / 1.9.4
CentOS 6.7

In my invetory file, I have:

[zabbix_server]
zabbix.dev-white.projectname.jenkins

[some_other_zabbix_server]
someotherzabbix.dev-blue.project.jenkins ansible_ssh_host=10.120.133.11

When I run:

ansible -m setup -i hosts zabbix.dev-white.projectname.jenkins 

It gives me the following error:

zabbix.dev-white.projectname.jenkins | FAILED => FAILED: No authentication methods available

In my playbook, I'm trying to get the domain value of the zabbix server which is dev-white.projectname.jenkins. I tried the following but variable which are for showing the domain values are coming as blank/null, why?

Note: Value for inventory_hostname is showing correctly!! but domain values for ansible_domain / facter_domain are coming as blank/null value.

- debug: msg="My server is= {{ hostvars[groups['zabbix_server'][0]].inventory_hostname }} and domain is= {{ hostvars[groups['zabbix_server'][0]].ansible_domain }} --- {{ hostvars[groups['zabbix_server'][0]]['inventory_hostname'] }} -- {{ hostvars[groups['zabbix_server'][0]]['ansible_domain'] }} -- -- {{ hostvars[groups['zabbix_server'][0]]['facter_domain'] }}"

Tried the above code for some_other_zabbix_server group variable (where ansible_ssh_host variable is set), still no values are coming for showing its domain. PS: ansible_ssh_host property will be deprecated in newer Ansible versions.


Solution

  • One way is to split and join all parts except the first part.

      - debug: var="{{ groups['zabbix_server'][0].split('.')[1:] | join('.') }}"
    

    Result:

      "dev-white.projectname.jenkins"