Ansible、with_subelements 和 skip_missing 不起作用 [英] Ansible, with_subelements and skip_missing does not work
问题描述
我有以下问题:
定义我的 nginx 站点的 host_var(摘录):
I have the following problem:
A host_var defining my nginx sites (excerpt):
nginx_sites:
- server:
name: site1
location1:
config:
name: "/"
[...]
- server:
name: site2
location1:
config:
name: "/"
[...]
location2:
config:
name: "/secretspace"
[...]
htaccess:
username:
password: somepassword
在这个例子中,我有 2 个站点.第二个有两个位置,其中第二个有一个名为htaccess"的子元素.这是我想用来创建相应的 htaccess 文件的内容.
In this example I have 2 sites. The second one has two locations where the second one has a subelement named "htaccess". This is what I want to use in order to create an corresponding htaccess file.
我尝试了这个 ansible 任务:
I tried this ansible task:
- name: Creating htaccess files if nessecary
debug: msg="Huhu {{ item.0.name }} {{ item.1 }}"
with_subelements:
- "{{ nginx_sites }}"
- "htaccess"
- flags:
skip_missing: True
这个应该恕我直言打印一个调试语句,当nginx_sites
中有一个名为htaccess"元素的子元素时,如果它不存在则跳过.但什么也没发生:
This should imho print a debug statement, when there is a subelement named "htaccess" element in nginx_sites
or skip if it is not there.
But nothing happens:
TASK [nginx : Creating htaccess files if nessecary] ****************************
task path: /home/dakky/devzone/ansible-cfg-mgmt/roles/nginx/tasks/main.yml:65
PLAY RECAP *********************************************************************
hostname : ok=1 changed=0 unreachable=0 failed=0
知道我在这里做错了什么吗?
Any idea what I'm doing wrong here?
推荐答案
with_subelements
用于遍历 list 子元素.
如果您修改输入数据以创建位置列表而不是命名属性(位置 1、位置 2 等):
with_subelements
is used to iterate over list subelements.
If you modify your input data to make a list of locations instead of named properties (location1, location2, etc):
nginx_sites:
- server:
name: site1
locations:
- config:
name: "/"
- server:
name: site2
locations:
- config:
name: "/"
- config:
name: "/secretspace"
htaccess:
username:
password: somepassword
然后您可以迭代您的位置并选择那些定义了 htaccess 的位置:
Then you can iterate your locations and select those with htaccess defined:
- name: Creating htaccess files if nessecary
debug: msg="Huhu {{ item.0.server.name }} {{ item.1.htaccess }}"
when: item.1.htaccess is defined
with_subelements:
- "{{ nginx_sites }}"
- "server.locations"
另请注意,您应该为子元素指定完整路径".在您的原始示例中,htaccess
永远不会匹配任何内容,但 server.location2.htaccess
会(尽管此项目不是列表的上升错误).
Also note that you should specify "full path" for subelements. In your original example htaccess
will never match anything, but server.location2.htaccess
will (although rising error that this item is not a list).
这篇关于Ansible、with_subelements 和 skip_missing 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!