Refactor ansible inventory .

There are hosts with similar names:

host1.datacenter-a.com host2.datacenter-a.com host1.datacenter-k.com host2.datacenter-k.com host1.datacenter-x.com host2.datacenter-x.com 

The number I can roll as [1: 2]:

 host[1:2].datacenter-a.com host[1:2].datacenter-k.com host[1:2].datacenter-x.com 

Is it possible to somehow enumerate letters that are not a sequence (even in increments)?

 # все эти варианты не работают host[1:2].datacenter-[a,k,x].com host[1:2].datacenter-[akx].com host[1:2].datacenter-[a|k|x].com host[1:2].datacenter-[a:k:x].com host[1:2].datacenter-[akx].com 
  • Unfortunately grouping is possible only by range. All that comes to mind is clearly to prescribe chidren groups - Borys Borysenko
  • @BorysBorysenko aha, already convinced that there are no options yet. Perhaps someday implement. - Nick Volynkin

1 answer 1

The comments have already correctly said that the possibilities of host patterns are limited to a range (letters or numbers). In principle, this would have been possible to complete the question, but.

I want to indicate another possible direction for solving the problem You can use dynamic inventory .

Our task is to write some inventory/dyn.py on the python, which will output the usual json:

 { "_meta": { "hostvars": { "10.1.0.10": { "ansible_user": "vagrant", "ansible_ssh_private_key_file": "/home/mrtuovinen/.ssh/id_rsa", "ansible_port": 22 }, "10.1.0.11": { "ansible_user": "ubuntu", "ansible_ssh_private_key_file": "/home/mrtuovinen/.ssh/id_rsa", "ansible_port": 22 }, "10.1.0.12": { "ansible_user": "steve", "ansible_ssh_private_key_file": "/home/mrtuovinen/.ssh/key.pem", "ansible_port": 2222 } } }, "vagrantbox": [ "10.1.0.10" ], "ubuntubox": [ "10.1.0.11" ], "osxbox": [ "10.1.0.12" ] } 

and run it:

 ansible-playbook -i inventory/dyn.py -l targethost my_playbook.yml 

In the scripts you can already use all the power of regulars, cycles, patterns, etc.

Of course, not suitable for all cases - sometimes it is easier to manually specify the hosts.

  • Hmm, but I did not know that as a source of dynamic inventory, you can give code for execution. - Nick Volynkin
  • one
    By the way, I came to the conclusion that folding the groups is not always the best solution. First, it breaks the logic of plugins for syntax highlighting .ini files. Secondly, it imposes a specific order of iterating the hosts, which breaks the serial: n - Nick Volynkin