Ansible set_fact 数组并从循环中填充它 [英] Ansible set_fact array and populate it from in loop
本文介绍了Ansible set_fact 数组并从循环中填充它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个数组并将数组IP_TO_DNS
中的值插入到反向IP地址中.
I want to create an array and insert value from the the array IP_TO_DNS
to reversed IP address.
这个想法是重构参数中给出的 IP 地址,以便稍后在我的代码中进行匹配.
The idea is to restructure the IP address given in the argument to be matchable later in my code.
代码
- name: create array reversed
set_fact: reversed_ip=[]
- name: set convert ips from cli to matchable reversed ip
set_fact: reversed_ip='{{ item | regex_replace('^(?P<first_range>\d{1,3})\.(?P<second_range>\d{1,3})\.(?P<third_range>\d{1,3})\.', 'named.\\g<third_range>.\\g<second_range>.\\g<first_range>')}}'
with_items: '{{IP_TO_DNS}}'
- name: Match first block of results in path name
debug: var=item
with_items: '{{reversed_ip}}'
输出
TASK [dns : set convert ips from cli to matchable reversed ip] *****************
ok: [10.1.10.5] => (item=10.1.10.1)
ok: [10.1.10.5] => (item=10.1.10.2)
ok: [10.1.10.5] => (item=10.1.10.3)
TASK [dns : Match first block of results in path name] *************************
ok: [10.1.10.5] => (item=named.10.1.103) => {
"item": "named.10.1.103"
}
看起来我的变量没有设置为数组,只填充了第一个值.
It look like my variable is not set as an array and only the first value is populate.
有什么想法吗?
推荐答案
您设置了 3 次相同的事实,但它被覆盖了.
You are setting the same fact three times and it gets overwritten.
您应该注册输出:
- name: set convert ips from cli to matchable reversed ip
set_fact: reversed_ip='{{ item | regex_replace('^(?P<first_range>\d{1,3})\.(?P<second_range>\d{1,3})\.(?P<third_range>\d{1,3})\.', 'named.\\g<third_range>.\\g<second_range>.\\g<first_range>')}}'
with_items: '{{IP_TO_DNS}}'
register: reversed_ip_results_list
- name: Match first block of results in path name
debug: var=item.ansible_facts.reversed_ip
with_items: '{{reversed_ip_results_list.results}}'
或者如果你想要一个列表:
or if you want a list:
- debug: msg="{{ reversed_ip_results_list.results | map(attribute='ansible_facts.reversed_ip') | list }}"
这篇关于Ansible set_fact 数组并从循环中填充它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文