the ansible way

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])

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

Thank you John. This approach looks perfect.