覆盖Ansible中默认变量的属性

覆盖Ansible中默认变量的属性,ansible,Ansible,我试图找出是否有任何方法可以覆盖group_vars在默认值中声明的变量的属性。 确切的问题是,我自动化了MySQL服务器的部署,并在playbook的默认设置中创建了默认配置选项,作为键值对列表,但有时需要更改某些值,但不一定全部更改。 我在defaults/main/mysql_config.yml中有以下YAML代码,它由Jinja2模板解析到my.cnf文件中: performance_tuning: query_cache_type: 1 query_cache_size:

我试图找出是否有任何方法可以覆盖group_vars在默认值中声明的变量的属性。
确切的问题是,我自动化了MySQL服务器的部署,并在playbook的默认设置中创建了默认配置选项,作为键值对列表,但有时需要更改某些值,但不一定全部更改。

我在defaults/main/mysql_config.yml中有以下YAML代码,它由Jinja2模板解析到my.cnf文件中:

performance_tuning:
  query_cache_type: 1
  query_cache_size: 512M
  query_cache_limit: 2M
  query_cache_strip_comments: 1
  thread_pool_size: "{{ ansible_processor_vcpus  }}"
  wait_timeout: 15
  interactive_timeout: 15
  max_connections: 151
我想在不重复整个代码的情况下更改组_vars/all中的其中一个属性

我尝试了以下方法,但似乎不是这样做的,模板仍然使用默认值:

performance_tuning.query_cache_size: 128M
此外,如果我将performance_tuning变量添加到只有一个属性的group_变量中,那么它显然只将一个属性部署到配置文件中:

performance_tuning:
  query_cache_size: 128M
Jinja2模板:

{% if performance_tuning is defined and performance_tuning|length %}
{% for key, value in performance_tuning.items() %}
{{ key }}={{ value }}
{% endfor %}
{% endif %}

你可以用几种方法来做。这里有几个

1。Jinga模板中的逻辑非常简单

{% for x in performance_tuning %}
{% if x == 'query_cache_size' and env == 'dev'%}
{{x}} : 128M
{% elif x == 'query_cache_size' and env == 'uat'%}
{{x}} : 256M
{% else %}
{{ x }} : {{performance_tuning[x]}}
{% endif %}
{% endfor %}
2。通过变量

不同集群或不同环境的不同变量

env: "dev"

query_cache_dev_size: "128M"
query_cache_uat_size: "256M"
query_cache_prod_size: "512M"


performance_tuning:
  query_cache_size: "{{ vars['query_cache_'+ env + '_size'] }}"
  ...
  ...
像这样的

就像我说的,还有其他几种方法。希望这有帮助

已编辑 3.使用组变量

就像你说的,如果包含父级和子级,它只加载一个配置。但我找到了解决办法,也许这会有帮助

您的正常
vars文件

performance_tuning:
  query_cache_type: 1
  query_cache_size: 512M
  query_cache_limit: 2M
  query_cache_strip_comments: 1
  thread_pool_size: "{{ ansible_processor_vcpus  }}"
  wait_timeout: 15
  interactive_timeout: 15
  max_connections: 151

performance_tuning:
  query_cache_type: 1
  query_cache_size: 512M
  query_cache_limit: 2M
  query_cache_strip_comments: 1
  thread_pool_size: 10
  wait_timeout: 15
  interactive_timeout: 15
  max_connections: 151

你的小组成员

---
query_cache_size: "128M"
cluster_performance_tuning:
  query_cache_size: 64M
您的Jinga模板应该有一个if条件,用于从组变量中选择正常的
查询\u缓存\u大小
,如下所示

{% for x in performance_tuning %}
{% if x == 'query_cache_size'%}
{{x}} : {{query_cache_size}}
{% else %}
{{ x }} : {{performance_tuning[x]}}
{% endif %}
{% endfor %}
因此,当循环迭代时,它会不断检查键是否为
query\u cache\u size
,然后它会选择
group\u vars
中定义的正常变量,您可以通过查看模板了解该变量

希望这有帮助

编辑(即兴第三种方法)

根据这些评论,我可以提出一些建议。同样,这是哈奇,而不是你可能正在寻找的具体答案

vars文件

performance_tuning:
  query_cache_type: 1
  query_cache_size: 512M
  query_cache_limit: 2M
  query_cache_strip_comments: 1
  thread_pool_size: "{{ ansible_processor_vcpus  }}"
  wait_timeout: 15
  interactive_timeout: 15
  max_connections: 151

performance_tuning:
  query_cache_type: 1
  query_cache_size: 512M
  query_cache_limit: 2M
  query_cache_strip_comments: 1
  thread_pool_size: 10
  wait_timeout: 15
  interactive_timeout: 15
  max_connections: 151

组变量


new_performance_tuning:
  query_cache_type: 10
  query_cache_size: 256M
  query_cache_limit: 12M

Jinga模板


{% for x in performance_tuning %}
{% if x in new_performance_tuning %}
{{ x }} : {{ new_performance_tuning[x] }}
{% else %}
{{ x }} : {{performance_tuning[x]}}
{% endif %}
{% endfor %}

如果您不想对每个要重用的变量使用
if
条件,则这更符合逻辑


希望这有帮助。:)

基于@BinaryBulletin建议的最终解决方案:
默认值/mariadb_config.yml

default_performance_tuning:
  query_cache_type: 1
  query_cache_size: 128M
  query_cache_limit: 2M
  query_cache_strip_comments: 1
  thread_pool_size: "{{ ansible_processor_vcpus  }}"
  wait_timeout: 15
  interactive_timeout: 15
  max_connections: 151
组变量/所有

---
query_cache_size: "128M"
cluster_performance_tuning:
  query_cache_size: 64M
模板/my.cnf.j2

[mysqld]
{% for key, value in default_performance_tuning.items() %}
{% if cluster_performance_tuning is defined %}
{% if key in cluster_performance_tuning %}
{{ key }}={{ cluster_performance_tuning[key] }}
{% else %}
{{ key }}={{ value }}
{% endif %}
{% else %}
{{ key }}={{ value }}
{% endif %}
{% endfor %}

模板正确地从集群性能调优变量中拾取“query\u cache\u size:64M”。

如果我得到正确的结果,这可能与ansible中的变量优先级有关。您希望从何处替换该值
host vars
extr vars
希望使用Ansible代码作为一种IaC,因此我不会使用额外的vars,而是尝试更改某个集群的库存组\u vars/all文件中的变量。不幸的是,这些都不能解决我的问题,因为在我的情况下,我没有像那样分离集群。可以使用不同的配置部署更多的生产服务器,这就是为什么我想在组_vars中覆盖,并且值根据每个集群中虚拟机的RAM大小而变化。@TamásJuhász,酷!我找到了一种绕过你的方法,我在编辑部分的答案检查中添加了同样的方法。希望这有帮助,让我知道更多信息。啊,是的,这只适用于一个变量,但query\u cache\u大小只是一个例子,它可以是列表中的任何其他变量,必须进行更改才能调整特定集群的性能。或者我必须为每个可能的性能调优属性创建“如果”部分,但仍然可以向列表中添加更多属性,在这种情况下,模板也应该再次更新。@TamásJuhász,根据您的评论添加了一个临时方法,希望对您有所帮助。如果您需要进一步澄清,请告诉我。