【发布时间】:2020-10-09 21:20:57
【问题描述】:
我有以下名为 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。这看起来特别相关:
最后,您可以使用 --limit 通过在文件名前加上 @ 前缀来从文件中读取主机列表:
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 参数如何工作?文件应该是什么格式/语法?我在互联网上的任何地方都找不到任何这样的例子。
【问题讨论】: