# the ansible way

**URL:** <https://forum.ansible.com/t/the-ansible-way/11599>\
**Category:** Ansible Project\
**Created:** [June 4, 2012, 10:07am UTC](https://forum.ansible.com/t/the-ansible-way/11599 "2012-06-04T10:07:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![damaex](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damaex](https://forum.ansible.com/u/damaex)\
**Post date:** [June 4, 2012, 10:07am UTC](https://forum.ansible.com/t/the-ansible-way/11599/1 "2012-06-04T10:07:21Z")

</div>

Hi,

I am pretty happy with ansible but I still don’t get my head fully around some of the concepts.  
Maybe somebody can give me input which helps me to handle ansible the correct way.

Task: I want to do the pub IP configuration of all my hosts via ansible (dhcp isn’t a option)  
Problem:

1. I am not sure if it makes more sense to do this via a playbook or via the API.
2. How do I handle the variables (interface, IP, netmask) correctly in API mode. (how to handover variables for template files?)

Here is my script:

#!/usr/bin/python

import ansible.runner  
import sys

hosts = {  
“graphite.foo.bar”:  
{“eth0”: (“46.0.0.0”, “255.255.255.255”),  
“eth0.1”: (“10.2.90.1”, “255.255.255.255”)},  
“backup201.foo.bar”:  
{“eth0”: (“78.0.0.0”, “255.255.255.255”)}  
}

def remote\_exe(host, iface, ip, net):  
results = ansible.runner.Runner(pattern=host, forks=2, module\_name=“template”, module\_args=“src=‘ip\_template’, dest=(‘/home/mw/%s.txt’ % (iface))”, remote\_user=“mw”,).run()  
return results

for host in hosts.keys():

for iface in hosts[host]:  
print “%s:\n%s =\> %s/%s” % (host, iface, hosts[host][iface][0], hosts[host][iface][1])  
print remote\_exe(host, iface, hosts[host][iface][0], hosts[host][iface][1])

---

<div class="post-metadata">

**Author:** ![John\_Kleint](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@John\_Kleint](https://forum.ansible.com/u/John_Kleint)\
**Post date:** [June 4, 2012, 2:19pm UTC](https://forum.ansible.com/t/the-ansible-way/11599/2 "2012-06-04T14:19:49Z")

</div>

I do this with a playbook and some templates.

First, I have a vars file that maps MAC address to ip and hostname:

# mac-addrs.yml

---

<div class="post-metadata">

**Author:** ![damaex](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@damaex](https://forum.ansible.com/u/damaex)\
**Post date:** [June 4, 2012, 2:43pm UTC](https://forum.ansible.com/t/the-ansible-way/11599/3 "2012-06-04T14:43:33Z")

</div>

Thank you John. This approach looks perfect.
