Howdy!
I'm trying figure out how to best classify nodes in order to be able
to run the same playbook(s) efficiently against multiple different
target environments.
To illustrate, let's say I have a playbook that sets up an appserver
and one for a database server. Part of the appserver is a
configuration file that must contain the IP and port number of the
database. Getting the values dynamically into the file is, of course,
is solved with templates.
But I wan't this to work when I run both playbooks against different
target environments. For example one with a single, local, Vagrant
instance and one with three separate servers (2 app & 1 db).
So regardless of the host/inventory configuration I'd like to be able
to do something like following in my template:
{{ hostvars['db']['host'] }}
and
{{ hostvar['db']['port'] }}
But I am unable to see how to achieve this with the current layout /
function of the hosts (inventory) file. I imagine what I'd like
(need?) is the ability to define nodes as a value pair consisting of
an arbitrary name and it's hostname/IP.
Or is this simply a matter of me not having wrapped my head around
Ansible in the correct way?
The fictive example below would cause nodes to appear in the hostvar
structure by their name (if given) and by their IP/hostname if no name
was given.
== 1 host
Mårten Gustafson wrote:
Howdy!
I'm trying figure out how to best classify nodes in order to be able
to run the same playbook(s) efficiently against multiple different
target environments.
To illustrate, let's say I have a playbook that sets up an appserver
and one for a database server. Part of the appserver is a
configuration file that must contain the IP and port number of the
database. Getting the values dynamically into the file is, of course,
is solved with templates.
But I wan't this to work when I run both playbooks against different
target environments. For example one with a single, local, Vagrant
instance and one with three separate servers (2 app & 1 db).
So regardless of the host/inventory configuration I'd like to be able
to do something like following in my template:
{{ hostvars['db']['host'] }}
and
{{ hostvar['db']['port'] }}
But I am unable to see how to achieve this with the current layout /
function of the hosts (inventory) file. I imagine what I'd like
(need?) is the ability to define nodes as a value pair consisting of
an arbitrary name and it's hostname/IP.
You could have a host-variable containing the name of the database
server to use. Your template would then use something like
{{ hostvars[db_server]['port'] }}
to access the database's settings.
Hm, right. Like this:
- group: appservers
hosts:
- 1.2.3.4
vars:
- db_server: 1.2.3.4
- node: 1.2.3.4
vars:
- port: 9999
That ought to work, thanks!
Has there been any thoughts along the lines of allowing arbitrary
naming of hosts in the inventory file?
/mårten.
That ought to work, thanks!
Has there been any thoughts along the lines of allowing arbitrary
naming of hosts in the inventory file?
Yeah, we know what we want to do, but haven’t done it yet.
Basically we need to have a variable like ansible_ssh_host and if set, connection.py will use that as the host address versus the name of the host record, just like ansible_ssh_port can be used to override the port.
Just needs a small patch and testing, syntactic sugar in the inventory parser could come later.
–Michael