0
votes

I have the following static inventory file:

[nodes:children]
masters
workers

[masters]
node1 ansible_host=1.2.3.4 ansible_port=2220 ansible_user=vagrant ansible_private_key_file=keys/id_rsa

[workers]
node2 ansible_host=1.2.3.4 ansible_port=2221 ansible_user=vagrant ansible_private_key_file=keys/id_rsa

Because my hosts use the same IP, but different ssh ports I need to alias them to so that ansible can see them as two hosts.

I'm trying to use a dynamic inventory script to generate this, but I'm struggling to find any information on the format of the json the I need to return, more specifically - how to alias the hosts.

So my question - does anyone know the json equivalent of the above static inventory file or somewhere where I can find information on the json format?

1

1 Answers

2
votes

Please read this doc about writing dynamic inventory scripts.

There are two steps:

1) script --list should return groups with variables and hosts names in that groups (in your example, you should return groups: nodes, masters, workers and hosts: node1 and node2).

2) script --host <hostname> should return hosts' variables (in your example connection variables like ansible_host and ansible_port).

So, in your example, inventory script should return 3 different json objects when executed with --list, --host node1 and --host node2.