Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify a different domain name in Ansible inventory

Tags:

ansible

I am using a Linux VM managed many Linux boxes (in a different domain), I find it annoying to use FQDN for each individual servers, because our internal domain name is very long.

For example

[web]
serve1.part.one.of.very.long.internal.domain.name.com
anotherserver.part.one.of.very.long.internal.domain.name.com

Is there a way to specify a default domain for groups of servers in inventory? I tried adding andible_domain variable in inventory file as a variable but did not work.

like image 246
Ask and Learn Avatar asked Sep 19 '16 04:09

Ask and Learn


People also ask

How do I specify Ansible inventory file?

Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against. The default location for inventory is a file called /etc/ansible/hosts . You can specify a different inventory file at the command line using the -i <path> option.

How can you specify a different inventory file when you run an Ansible command on the command line?

You can specify a different inventory file using the -i <path> option on the command line. Not only is this inventory configurable, but you can also use multiple inventory files at the same time (explained below) and also pull inventory from dynamic or cloud sources, as described in Dynamic Inventory.

Can we have multiple inventory files in Ansible?

If the location given to -i in Ansible is a directory (or as so configured in ansible. cfg ), Ansible can use multiple inventory sources at the same time. When doing so, it is possible to mix both dynamic and statically managed inventory sources in the same ansible run.


1 Answers

By default Ansible will assume that your inventory_hostname (the first string on the line in the inventory file) is what you would use to connect to that.

You can, however, always override this by using ansible_host (or ansible_ssh_host in older versions) which is useful if for some reason that's not the FQDN of the host or the domain for the host isn't in your DNS search domain list.

So you could do something like this:

[all:vars]
host_domain=part.one.of.very.long.internal.domain.name.com
ansible_host="{{inventory_hostname}}.{{host_domain}}"

[web]
server1
anotherserver
like image 78
ydaetskcoR Avatar answered Sep 18 '22 13:09

ydaetskcoR