我目前正在构建一个使用ansible安装PHP的角色,我在合并字典方面遇到了一些困难.我已经尝试了几种方法,但我不能像我想要的那样让它工作:
# A vars file:
my_default_values:
key = value
my_values:
my_key = my_value
# In a playbook, I create a task to attempt merging the
# two dictionaries (which doesn't work):
- debug: msg="{{ item.key }} = {{ item.value }}"
with_dict: my_default_values + my_values
# I have also tried:
- debug: msg="{{ item.key }} = {{ item.value }}"
with_dict: my_default_values|union(my_values)
# I have /some/ success with using j2's update,
# but you can't use j2 syntax in "with_dict", it appears. …Run Code Online (Sandbox Code Playgroud) 我有一个分为2组的主机:pc和服务器我有2个group_vars(pc和服务器),每个文件packages.yml这些文件定义要在pc主机和服务器主机上安装的软件包列表
我有一个安装默认包的角色
问题是:角色任务只考虑group_vars/pc/packages.yml,未安装group_vars/servers/packages.yml中的软件包
当然我想要的是安装为pc和服务器定义的包
我不知道这是一个bug还是一个功能......
谢谢你的帮助
这是配置:
# file: production
[pc]
armen
kerbel
kerzo
[servers]
kerbel
---
# packages on servers
packages:
- lftp
- mercurial
---
# packages on pc
packages:
- keepassx
- lm-sensors
- hddtemp
Run Code Online (Sandbox Code Playgroud) 看起来 Ansible 无法从 group_vars 合并嵌套变量。我的结构是这样的
hosts.ini::
[common:children]
frontend
backend
[frontend]
server1
[backend]
server2
Run Code Online (Sandbox Code Playgroud)
在groups-vars目录中我有:
common.yaml:
start_of_nested variables:
var1: value1
var2: value2
Run Code Online (Sandbox Code Playgroud)
frontend.yaml:
start_of_nested variables:
var3: value3
var4: value4
Run Code Online (Sandbox Code Playgroud)
backend.yaml:
start_of_nested variables:
var5: value5
var6: value6
Run Code Online (Sandbox Code Playgroud)
当我检查server1变量时:
ansible server1 -m debug -a "var=hostvars[inventory_hostname]"
Run Code Online (Sandbox Code Playgroud)
我仅从以下位置获取变量frontend.yaml:
"start_of_nested": {
"var3": "value3",
"var4": "value4"
}
Run Code Online (Sandbox Code Playgroud)
但我期待它们会与common.yaml变量合并,我会得到类似的东西
"start_of_nested": {
"var1": "value1",
"var2": "value2",
"var3": "value3",
"var4": "value4"
}
Run Code Online (Sandbox Code Playgroud)
Ansible 有没有办法合并group_vars它所属主机的嵌套变量?
在我的 Ansible playbook 中,我有一个嵌套变量声明,如下所示在变量文件中。
repo:
branch: int
url: git@github:user/repo.git
dest: "/var/code"
Run Code Online (Sandbox Code Playgroud)
我将如何覆盖额外变量中的分支参数?我在下面尝试了类似的东西,但没有用。
--extra-vars "repo.branch=exec_refactor"
Run Code Online (Sandbox Code Playgroud)
既不是这个
--extra-vars "repo[branch]=exec_refactor"
Run Code Online (Sandbox Code Playgroud)
使用像下面这样的 JSON 表示会覆盖整个repo节点,因此 repo.branch 被成功覆盖,但 repo.url 和 repo.dest 都未定义。
--extra-vars '{"repo":{"branch":"exec_refactor"}}'
Run Code Online (Sandbox Code Playgroud) 我需要模板化配置文件,该文件本身具有YAML格式.这样做的好习惯是什么?
最终文件如下所示:
development:
adapter: mysql2
database: tracks
# set this if you are storing utf8 in your mysql database to handle strings
# like "Réné". Not needed for sqlite. For PostgreSQL use encoding: unicode
# encoding: utf8
host: localhost
username: root
password: qwerty
Run Code Online (Sandbox Code Playgroud)
应该定义大多数这些变量,并且一些变量需要非默认值.在变量和模板中都是YAML.所以我必须至少重复两次相同的结构:在模板和vars文件中.
一个真正的问题是可选参数.要设置正确的编码(或没有),我必须写一些类似的东西:
# tasks/configure.yml
- include: {tracks_database}.yml
# variables/mysql2.yml
tracks_database_encoding: utf8
# templates/site.yml
development:
database: "{{ tracks_database }}"
{% if tracks_database_use_utf8 %}
encoding: "{{ tracks_database_encoding }}"
{% endif %}
Run Code Online (Sandbox Code Playgroud)
所以我考虑了另一种方法:将配置存储在变量中,然后通过jijna过滤器 …