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

14,760

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"
Share:
14,760

Related videos on Youtube

AKS
Author by

AKS

A Quote on "Quote(s)": For every quote, there exists at least one contradictory quote. - AKS

Updated on June 04, 2022

Comments

  • AKS
    AKS almost 2 years

    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.

  • geckos
    geckos almost 6 years
    No need for join just use .split('.', 1)[1]