Ansible 库存模式和限制参数
Ansible inventory patterns and limit argument
我有以下名为 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
以及引用这些组的剧本:
- 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
然后我像这样调用 ansible $ ansible-playbook -i hosts-local playbook.yml
我最初的想法是我有 hosts-local
、hosts-test
和 hosts-prod
文件,以便可以定义 host-1
、host-2
等部署环境基础(尽管其余组重复)。
但后来有人建议我查看 ansible 模式,特别是 --limit
参数,引用 the ansible docs。这看起来特别相关:
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
我读这篇文章的方式是,我有一个像这样的库存文件:
[minio_cluster_members]
host-1
host-2
host-3
host-4
[nginx_reverse_proxies]
host-1
host-3
[all:vars]
ansible_connection=ssh
然后是为每个环境定义 host-1
、host-2
等的一系列文件:
# 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
等等
然后我会像这样调用 ansible:$ ansible-playbook -i inventory playbook.yml --limit 'all:@hosts-local'
但我这辈子都做不到。它抱怨限制文件中的每一行(包括注释):
[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
我做错了什么?我是否误解了模式,特别是文件的 --limit
参数是如何工作的? format/syntax 文件应该是什么?我在互联网上的任何地方都找不到这方面的任何例子。
我认为 --limit
的论点是转移注意力,而不是我想要的解决问题的方法。
我发现您可以指定多个清单文件。所以我有主要库存 hosts
:
[minio_cluster_members]
host-1
host-2
host-3
host-4
[nginx_reverse_proxies]
host-1
host-3
[all:vars]
ansible_connection=ssh
我有一个名为 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
还有一个用于测试的 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
我可以将它们组合成:$ ansible-playbook playbook.yml -i hosts -i hosts-local
就是这样!仅此而已。
另外,我还获得了使用群组 test
、local
等的额外好处,因此我可以根据群组名称设置变量。
我有以下名为 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
以及引用这些组的剧本:
- 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
然后我像这样调用 ansible $ ansible-playbook -i hosts-local playbook.yml
我最初的想法是我有 hosts-local
、hosts-test
和 hosts-prod
文件,以便可以定义 host-1
、host-2
等部署环境基础(尽管其余组重复)。
但后来有人建议我查看 ansible 模式,特别是 --limit
参数,引用 the ansible docs。这看起来特别相关:
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
我读这篇文章的方式是,我有一个像这样的库存文件:
[minio_cluster_members]
host-1
host-2
host-3
host-4
[nginx_reverse_proxies]
host-1
host-3
[all:vars]
ansible_connection=ssh
然后是为每个环境定义 host-1
、host-2
等的一系列文件:
# 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
等等
然后我会像这样调用 ansible:$ ansible-playbook -i inventory playbook.yml --limit 'all:@hosts-local'
但我这辈子都做不到。它抱怨限制文件中的每一行(包括注释):
[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
我做错了什么?我是否误解了模式,特别是文件的 --limit
参数是如何工作的? format/syntax 文件应该是什么?我在互联网上的任何地方都找不到这方面的任何例子。
我认为 --limit
的论点是转移注意力,而不是我想要的解决问题的方法。
我发现您可以指定多个清单文件。所以我有主要库存 hosts
:
[minio_cluster_members]
host-1
host-2
host-3
host-4
[nginx_reverse_proxies]
host-1
host-3
[all:vars]
ansible_connection=ssh
我有一个名为 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
还有一个用于测试的 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
我可以将它们组合成:$ ansible-playbook playbook.yml -i hosts -i hosts-local
就是这样!仅此而已。
另外,我还获得了使用群组 test
、local
等的额外好处,因此我可以根据群组名称设置变量。