Ansible set_fact数组并从循环中填充 [英] Ansible set_fact array and populate it from in loop

查看:465
本文介绍了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}}'

或者如果您想要列表:

- debug: msg="{{ reversed_ip_results_list.results | map(attribute='ansible_facts.reversed_ip') | list }}"

这篇关于Ansible set_fact数组并从循环中填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆