I have the following inventory file called hosts-local
:
host-1 ansible_host=minio-1.localdomain
host-2 ansible_host=minio-2.localdomain
host-3 ansible_host=minio-3.localdomain
host-4 ansible_host=minio-4.localdomain
[minio_cluster_members]
host-1
host-2
host-3
host-4
[nginx_reverse_proxies]
host-1
host-3
[all:vars]
ansible_connection=ssh
and a playbook that references the groups:
- name: "Install and configure Minio"
hosts: minio_cluster_members
become: yes
remote_user: ansible
roles:
- minio
- name: "Install and configure nginx reverse proxy"
hosts: nginx_reverse_proxies
become: yes
remote_user: ansible
roles:
- nginx
And I invoke ansible like this $ ansible-playbook -i hosts-local playbook.yml
My original thought was that I'd have hosts-local
, hosts-test
and hosts-prod
files so that host-1
, host-2
etc could be defined on a deployment environment basis (albeit with the duplication of the rest of the groups).
But then someone suggested I look at ansible patterns and specifically the --limit
argument, referencing the ansible docs. This looks particularly relevant:
Finally, you can use --limit to read the list of hosts from a file by prefixing the file name with @:
ansible-playbook site.yml --limit @retry_hosts.txt
The way I've read this is that I'd have a single inventory file like this:
[minio_cluster_members]
host-1
host-2
host-3
host-4
[nginx_reverse_proxies]
host-1
host-3
[all:vars]
ansible_connection=ssh
and then a series of files that define host-1
, host-2
etc for each environment:
# local dev hosts
host-1 ansible_host=minio-1.localdomain
host-2 ansible_host=minio-2.localdomain
host-3 ansible_host=minio-3.localdomain
host-4 ansible_host=minio-4.localdomain
# test hosts
host-1 ansible_host=test-minio-1.test.domain.com
host-2 ansible_host=test-minio-2.test.domain.com
host-3 ansible_host=test-minio-3.test.domain.com
host-4 ansible_host=test-minio-4.test.domain.com
etc.
And then I'd invoke ansible like this: $ ansible-playbook -i inventory playbook.yml --limit 'all:@hosts-local'
But for the life of me I cannot get this working. It complains about every line in the limit file (including the comment):
[WARNING]: Could not match supplied host pattern, ignoring: # local dev hosts
[WARNING]: Could not match supplied host pattern, ignoring: host-1 ansible_host=minio-1.localdomain
[WARNING]: Could not match supplied host pattern, ignoring: host-2 ansible_host=minio-2.localdomain
[WARNING]: Could not match supplied host pattern, ignoring: host-3 ansible_host=minio-3.localdomain
[WARNING]: Could not match supplied host pattern, ignoring: host-4 ansible_host=minio-4.localdomain
What I am doing wrong? Have I misunderstood how patterns and specifically the --limit
argument with a file work? What format/syntax should the file be? I cannot find any examples of this on the internet anywhere.
I think the --limit
argument was a red herring, and not what I wanted in order to solve the problem.
What I've discovered is that you can specify multiple inventory files. So I have the main inventory hosts
:
[minio_cluster_members]
host-1
host-2
host-3
host-4
[nginx_reverse_proxies]
host-1
host-3
[all:vars]
ansible_connection=ssh
I have an inventory for local hosts called hosts-local
:
[local]
host-1 ansible_host=minio-1.localdomain
host-2 ansible_host=minio-2.localdomain
host-3 ansible_host=minio-3.localdomain
host-4 ansible_host=minio-4.localdomain
and one for test called hosts-test
:
[test]
host-1 ansible_host=test-minio-1.test.domain.com
host-2 ansible_host=test-minio-2.test.domain.com
host-3 ansible_host=test-minio-3.test.domain.com
host-4 ansible_host=test-minio-4.test.domain.com
And I can combine them with: $ ansible-playbook playbook.yml -i hosts -i hosts-local
That's it! That's all there is to it.
Plus I get the added benefit of having groups test
, local
etc, so I can set variables based on the group name.