# How to get backup ip and assign to a variable

**URL:** <https://forum.ansible.com/t/how-to-get-backup-ip-and-assign-to-a-variable/34536>\
**Category:** Ansible Project\
**Created:** [January 29, 2021, 12:15pm UTC](https://forum.ansible.com/t/how-to-get-backup-ip-and-assign-to-a-variable/34536 "2021-01-29T12:15:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramakrishna\_V](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@Ramakrishna\_V](https://forum.ansible.com/u/Ramakrishna_V)\
**Post date:** [January 29, 2021, 12:15pm UTC](https://forum.ansible.com/t/how-to-get-backup-ip-and-assign-to-a-variable/34536/1 "2021-01-29T12:15:17Z")

</div>

Hi,

How to search for a IPv4 starting with “192.168.x.x” from ansible\_all\_ipv4\_addresses and assign to variable. If not, assign default ipv4.

The use case is, I need to get backup ip and assign to a variable. I need to use this variable in some configuration file.

[root@xxxx agents\_deploy]# cat get\_backupip.yml

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 29, 2021, 1:02pm UTC](https://forum.ansible.com/t/how-to-get-backup-ip-and-assign-to-a-variable/34536/2 "2021-01-29T13:02:01Z")

</div>

> Hi,
> 
> How to search for a IPv4 starting with "192.168.x.x" from ansible\_all\_ipv4\_addresses and assign to variable. If not,  
> assign default ipv4.
> 
> The use case is, I need to get backup ip and assign to a variable. I need to use this variable in some configuration file.
> 
> [root@xxxx agents\_deploy]# cat get\_backupip.yml  
> ---  
> - hosts: "{{ hosts\_prompt }}"  
> become: yes  
> gather\_facts: no
> 
> vars\_files:  
> - /var/lib/ansible\_playbooks/inventory/password.yml
> 
> vars\_prompt:  
> - name: "hosts\_prompt"  
> prompt: "hostname or host group need to run with Playbook"  
> private: no  
> default: dev
> 
> pre\_tasks:  
> - setup:  
> filter: ansible\_\*
> 
> tasks:  
> - name: backup ip  
> debug:  
> msg: "{{ ansible\_all\_ipv4\_addresses }}"
> 
> - name: search backup ip which starts with '192.168',assign to some other variable. If not, assign primary ip  
> set\_fact:  
> backupip: "{% for ip in {{ ansible\_all\_ipv4\_addresses }} %}  
> {% if ip | regex\_search('192.168.\*.\*') %}  
> {{ ip }}  
> {% else % }  
> {{ ip[0] }}  
> {% endif %}  
> {% endfor %}"

This should be do the trick:

"{{ ansible\_all\_ipv4\_addresses | select('search', '192.168.\*.\*') | first | default(ansible\_all\_ipv4\_addresses[0]) }}"

However, the regular expression is not accurate as means ".\*" match ANY character.

Your original error code is caused by using {{ ... }} inside of the {% for ... %}.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Ramakrishna\_V](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@Ramakrishna\_V](https://forum.ansible.com/u/Ramakrishna_V)\
**Post date:** [January 29, 2021, 2:51pm UTC](https://forum.ansible.com/t/how-to-get-backup-ip-and-assign-to-a-variable/34536/3 "2021-01-29T14:51:46Z")

</div>

Hi Racket/All,

Ok, thanks for letting me know the other way of the trick. I’ll verify what you suggested and update in the group.

I wanted to know is there any syntax or logic issues in my original post. Any valuable inputs are appreciated.

- set\_fact:  
backupip: “{% for ip in {{ ansible\_all\_ipv4\_addresses }} %}  
{% if ip | regex\_search(‘192.168._._’) %}  
{{ ip }}  
{% else % }  
{{ ip[0] }}  
{% endif %}  
{% endfor %}”
