# Using ansible to modify ip address on system

**URL:** <https://forum.ansible.com/t/using-ansible-to-modify-ip-address-on-system/23351>\
**Category:** Ansible Project\
**Created:** [December 30, 2016, 9:37pm UTC](https://forum.ansible.com/t/using-ansible-to-modify-ip-address-on-system/23351 "2016-12-30T21:37:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerald\_Spencer1](https://avatars.discourse-cdn.com/v4/letter/g/b782af/32.png) [@Gerald\_Spencer1](https://forum.ansible.com/u/Gerald_Spencer1)\
**Post date:** [December 30, 2016, 9:37pm UTC](https://forum.ansible.com/t/using-ansible-to-modify-ip-address-on-system/23351/1 "2016-12-30T21:37:26Z")

</div>

Okay, we’ve ran out of ip addresses, and got a larger subnet from our ip. I am currently in the process of switching them over, and decided it would be nice to do this with ansible, but I have ran into a little snag.

Currently I have:

- shell: “ifconfig | grep -B1 {{ old\_address }}| grep -E ‘^(\w+)[[:space:]]’ | sed ‘s/\s.\*$//’”  
args:  
executable: /bin/bash  
register: network\_interface

- template:  
src: files/linux\_network\_interfaces.j2  
dest: /etc/network/interfaces  
owner: root  
group: root  
mode: ‘0644’  
become: true  
when: item.key == inventory\_hostname  
with\_dict: “{{ new\_addresses }}”

- shell: “ifdown {{ network\_interface.stdout }} && ifup {{ network\_interface.stdout }}”  
args:  
executable: /bin/bash  
become: true

But it hangs in the last shell task… since the IP address was changed.

Is there a way I can tell this task to just run, then use set\_fact to modify each host to its new IP to ping and make sure it worked, then I’ll move forward and modify our DNS records to match the new hosts…

---

<div class="post-metadata">

**Author:** ![Gerald\_Spencer1](https://avatars.discourse-cdn.com/v4/letter/g/b782af/32.png) [@Gerald\_Spencer1](https://forum.ansible.com/u/Gerald_Spencer1)\
**Post date:** [December 31, 2016, 4:34am UTC](https://forum.ansible.com/t/using-ansible-to-modify-ip-address-on-system/23351/2 "2016-12-31T04:34:19Z")

</div>

Solved it via running the command as “fire and forget” with async and pool, using a bash command in the background. Then modifying the ansible\_ssh\_host to the new IP address, and waiting for it port 22 to come up on that host.

- name: Grab the network interface that the old IP is using and store it.  
shell: “ifconfig | grep -B1 {{ old\_address\_start }}| grep -E ‘^(\w+)[[:space:]]’ | sed ‘s/\s.\*$//’”  
args:  
executable: /bin/bash  
register: network\_interface

- name: Pushout new /etc/network/interface to hosts  
template:  
src: files/linux\_network\_interfaces.j2  
dest: /etc/network/interfaces  
owner: root  
group: root  
mode: ‘0644’  
become: true  
when: item.key == inventory\_hostname  
with\_dict: “{{ new\_addresses }}”

- name: Reload the hosts’ network interface  
shell: “(sleep 1; ifdown {{ network\_interface.stdout }} && ifup {{ network\_interface.stdout }}) &”  
args:  
executable: /bin/bash  
become: true  
async: 100  
poll: 0

- name: Change ansible’s ip address for each host  
set\_fact:  
ansible\_ssh\_host: “{{ item.value.address }}”  
when: item.key == inventory\_hostname  
with\_dict: “{{ new\_addresses }}”

- name: Wait for the hosts’ network interface to come back up  
local\_action:  
module: wait\_for  
host: “{{ ansible\_ssh\_host }}”  
port: 22  
delay: 10  
state: started  
register: wait\_result

- name: Modify all of the route53 records for ip changes that succeeded  
local\_action:  
module: route53  
command: create  
zone: xxxxx  
record: “{{ item.key }}”  
type: A  
value: “{{ item.value.address }}”  
overwrite: true  
when: item.key == inventory\_hostname and wait\_result|succeeded  
with\_dict: “{{ new\_addresses }}”

---

<div class="post-metadata">

**Author:** ![Michael\_Lee1](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Michael\_Lee1](https://forum.ansible.com/u/Michael_Lee1)\
**Post date:** [May 22, 2018, 9:59pm UTC](https://forum.ansible.com/t/using-ansible-to-modify-ip-address-on-system/23351/3 "2018-05-22T21:59:37Z")

</div>

Hi, I was wondering if you could provide us with the j2 template for your network interface? If not, what are the basics that you used for it?

Thank you!
