如果库存仅为本地主机,则与多个组共享 ansible 变量 [英] ansible variables shared with multiple groups if inventory is only localhost

查看:30
本文介绍了如果库存仅为本地主机,则与多个组共享 ansible 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ansible 通过本地机器上的 kubectl 命令将作业/配置发送到我的 k8s 集群.我有我的清单文件设置,所以每个集群都是它自己的组,每个集群基本上都是到 localhost 的连接.

I use ansible to send jobs / configurations to my k8s cluster via the kubectl command on my local machine. I have my inventory file setup so that each cluster is it's own group and each cluster is basically a connection to localhost .

# Inventory File
#
[east.k.example.com]
localhost              ansible_connection=local

[east2.k.example.com]
localhost              ansible_connection=local

然后在我的 group_vars 目录中,我有一个不同的文件,其中包含我的清单文件中我的组的名称,该文件包含每个集群的所有不同变量.

Then in my group_vars directory I have a different file with the name of my group from my inventory file that holds all the different variables for each cluster.

我使用 limit 选项将运行限制为仅针对一个集群:ansible-playbook -vv create.yaml -least2.k.example.com --tags ingress-generate-only

I limit my runs to target only one cluster with the limit option: ansible-playbook -vv create.yaml -l east2.k.example.com --tags ingress-generate-only

问题是,当我尝试在模板中使用变量时,我从其他组中获取了变量.我在想是因为每个组都包含本地主机.

The problem is that when I attempt to use variables in my templates I get variables from the other groups. I'm thinking because each group includes localhost.

有没有更好的方法来解决这个问题?我可以设置一个标志,以便组只包含 group_var 文件中的变量吗?

Is there a better way to solve this issue? Can I set a flag so that groups only include the variables from the group_var files?

谢谢,

推荐答案

重构您的库存以使用不同的名称:

Refactor your inventory to use distinct names:

# Inventory File
#
[east.k.example.com]
east ansible_connection=local

[east2.k.example.com]
east2 ansible_connection=local

这样 Ansible 会将它们视为不同的主机,从而不会合并来自不同组的变量.

This way Ansible will treat them as different hosts, thus not merging variables from different groups.

这篇关于如果库存仅为本地主机,则与多个组共享 ansible 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆