# Changing IPs on servers?

**URL:** <https://forum.ansible.com/t/changing-ips-on-servers/11770>\
**Category:** Ansible Project\
**Created:** [August 7, 2012, 10:39pm UTC](https://forum.ansible.com/t/changing-ips-on-servers/11770 "2012-08-07T22:39:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![James8](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@James8](https://forum.ansible.com/u/James8)\
**Post date:** [August 7, 2012, 10:39pm UTC](https://forum.ansible.com/t/changing-ips-on-servers/11770/1 "2012-08-07T22:39:41Z")

</div>

Hi,  
Is there anything cool I can do to change the IPs of 10 servers? Or  
should I do it by hand?

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [August 7, 2012, 10:41pm UTC](https://forum.ansible.com/t/changing-ips-on-servers/11770/2 "2012-08-07T22:41:16Z")

</div>

DHCP?

---

<div class="post-metadata">

**Author:** ![James8](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@James8](https://forum.ansible.com/u/James8)\
**Post date:** [August 8, 2012, 3:28am UTC](https://forum.ansible.com/t/changing-ips-on-servers/11770/3 "2012-08-08T03:28:58Z")

</div>

No static IPs, just wondering if there was some way to build a  
playbook around it, but probably not?

James

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [August 8, 2012, 11:28am UTC](https://forum.ansible.com/t/changing-ips-on-servers/11770/4 "2012-08-08T11:28:50Z")

</div>

> No static IPs, just wondering if there was some way to build a  
> playbook around it, but probably not?
> 
> James
> 
> > DHCP?
> > 
> > > Hi,  
> > > Is there anything cool I can do to change the IPs of 10 servers? Or  
> > > should I do it by hand?
> > > 
> > > Thanks,  
> > > James

For permanently changing the IPs, you could use the template module to  
render the new IPs in the network configuration file of your distro.  
Then you can run the distro's network scripts to 'restart' the network  
interfaces with the new IPs.  
For the latter, you could use an action invoking either the command  
module or the service module if your distro provides proper init scripts  
for network configuration.

So, edit ansible's inventory and for each host add a variable named  
'new\_ip' or similar, holding the new static IP.  
Then, for each group of hosts with configuration files of same syntax,  
write a template that uses the $new\_ip variable. In your playbook, you would  
then need a play for each host group, rendering the corresponding  
template, and running the appropriate 'restart network' action.

You should also pay attention to the following things:  
- Errors in your network configuration files could leave you without  
remote access to the servers.  
&nbsp;&nbsp;So, you should first make sure that your templates render ok and/or  
possibly have second a route to the servers if something goes wrong.  
- Ansible host inventory would need an update with the new addresses (if  
not using dns names).  
- DNS would also need updating after changing the IPs (and doing that  
smoothly may not be so trivial as it may sound).

> From an administration point of view, in most cases, moving your network

configuration to DHCP would make tasks such as changing IPs easier and  
cleaner, since only changing the DHCP and master DNS server  
configuration would be enough.

So, if you really want a cool and clean way to do what you want, move  
all your hosts to DHCP, use DNS names in your ansible inventory (and  
everywhere else), and write an ansible playbook that uses the template  
module to create your DHCP/DNS configs from templates, possibly using  
variables from ansible inventory or external variable files.

---

<div class="post-metadata">

**Author:** ![James8](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@James8](https://forum.ansible.com/u/James8)\
**Post date:** [August 8, 2012, 7:43pm UTC](https://forum.ansible.com/t/changing-ips-on-servers/11770/5 "2012-08-08T19:43:21Z")

</div>

Hi,  
These were static IPs and I did it manually but I should have done it  
with a template. Since it just seems like it takes a long time to do  
it on 12 machines at midnight :-).

Thanks for your responses.

James
