[英]ansible playbook hosts, is there a way to filter by variable
I'm new to ansible so bear with me, if my question is a bit basic. 我是不熟悉ansible的新手,如果我的问题有点基本,请多多包涵。
I have 10 WordPress sites with different themes, all of them are listed in the hosts file under 'production' group: 我有10个主题不同的WordPress网站,所有这些网站均在“生产”组下的主机文件中列出:
[production]
black.com
red.com
blue.com
Each site require different variables: 每个站点需要不同的变量:
theme_name: black
Is there anyway that I can run a playbook on 'production' host group , using variable? 无论如何,我可以使用变量在“生产”主机组上运行剧本吗?
You can not easily* filter by variables. 您不容易*通过变量过滤。 But you could add your hosts to additional groups like so:
但是您可以将主机添加到其他组中,如下所示:
[production]
black.com
red.com
blue.com
[black]
black.com
[red]
red.com
[blue]
blue.com
Now for the theme name you an create group-vars files. 现在为主题名称创建一个group-vars文件。 For example, for the red group you create the file
group_vars/red
with the content: 例如,对于红色组,您可以使用以下内容创建文件
group_vars/red
:
theme_name: red
Now you can run your playbook with the black
, red
and blue
groups. 现在,您可以使用
black
, red
和blue
组来运行您的剧本。
* I guess there is a way, by running filters on the hostvars
dict and reduce it to a list of hostnames matching your criteria. *我想有一种方法,可以在
hostvars
dict上运行过滤器并将其简化为与您的条件匹配的主机名列表。 But this seems to be overhead and against best practice. 但这似乎是开销,并且违反了最佳实践。 If you want to target a specific set of hosts you should have a group for them in the inventory.
如果要定位一组特定的主机,则清单中应有一组主机。
Not sure your question is absolutely clear, but another possible answer to your questions might be: 不确定您的问题是否绝对明确,但是另一个可能的答案是:
You can assign variables directly to hosts or groups in the inventory, there is a good introduction as to how you can do this here . 您可以在清单中直接分配变量主机或群体,有一个很好的介绍,以你如何能做到这一点在这里 。
Example lifted from the page, variables defined after the host entry: 从页面上举的示例,在主机条目之后定义的变量:
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
This should work fine for small amounts of hosts, although there are better approaches when working at scale. 对于少量主机,这应该可以正常工作,尽管在大规模工作时有更好的方法。
hth. 心连心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.