Ansible,with_subelements和skip_missing不起作用 [英] Ansible, with_subelements and skip_missing does not work
问题描述
我有以下问题:
一个host_var定义了我的Nginx网站(摘录):
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.
我尝试了这项艰巨的任务:
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 子元素.
如果您修改输入数据以列出位置列表,而不是命名属性(location1,location2等):
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屋!