7

I want to create an array and insert value from the the array IP_TO_DNS to reversed IP address.

The idea is to restructure the IP address given in the argument to be matchable later in my code.

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}}'

Output

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.

Any ideas ?

3 Answers 3

12

This is the one of the ways which I used

vars:
   my_new_list: []
tasks:
 - name: Get list of elements from list_vars
   set_fact:
     my_new_list: "{{ my_new_list + [item] }}"
  with_items: "{{ list_vars }}"
Sign up to request clarification or add additional context in comments.

2 Comments

You don't need the vars: part if you set up the fact like this: my_new_list: "{{ my_new_list | default([]) | union([item]) }}".
In such a case you'll get the empty element [""] as a part of the list. I've cleaned it up with: list: "{{ list | default([]) | union(['list_element']) | difference(['']) }}"
7

You are setting the same fact three times and it gets overwritten.

You should register the output:

- 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 }}"

1 Comment

Thanks techraf that's doing the jobs.
2

You can assign the default of reversed_ip as a list and append the item to the list.

- name: set convert ips from cli to matchable reversed ip
  set_fact: reversed_ip='{{ reversed_ip |default([]) + [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}}'

1 Comment

Answers which contains only code are not considered complete. Plese read stackoverflow.com/help/how-to-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.