0

I have some simple Python, the goal of which is to deploy a hosts file. The hosts file is intended to look like this:

[master1]
*hostname*

[master2]
*hostname

I'm using Python to try and achieve this, first I retrieve the hostnames from my VMware build and put them into a file called tfhosts, it follows the format of /etc/hosts:

tfhosts

192.168.100.21 dc01-control-01
192.168.100.22 dc01-control-02
192.168.100.23 dc01-control-03
192.168.100.31 dc01-worker-01
192.168.100.32 dc01-worker-02

The Python code looks like this:

hostname.py

import jinja2
from tempfile import NamedTemporaryFile

def return_hosts():
        hosts = open('./tfhosts','r')
        x = ""
        for line in hosts:
                x = x + str(line.split()[1:]).strip('[]').strip("''") + '\n'
        return [x][0:]

inventory = """
[master1]
{{ host_master01 }}
[master2]
{{ host_master02 }}
"""

gethosts = return_hosts()

inventory_template = jinja2.Template(inventory)

for servers in (gethosts):
        rendered_inventory = inventory_template.render({
                'host_master01': servers[0],
                'host_master02': servers[1],
        })

hosts = NamedTemporaryFile(delete=False)
hosts.write(rendered_inventory)
hosts.close()

When I run my Python against tfhosts, it simply produces either the whole set of hosts as one array slice or if I attempt to limit the scope by using [0:]

Or if I use servers[0] | servers[1] in the code:

[master1]
d
[master2]
c

I get the first letter d (of the hostname) only and for master 2 c.

Can anyone spot the issue and offer some guidance?

Thanks in advance.

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
Stephen K.
  • 79
  • 10
  • What is the output with ``` rendered_inventory = inventory_template.render({ 'host_master01': gethosts[0], 'host_master02': gethosts[1], })``` ? – User Oct 04 '15 at 18:28
  • I get: Traceback (most recent call last): File "test.py", line 32, in 'host_master01': gethosts[1], IndexError: list index out of range – Stephen K. Oct 04 '15 at 18:33

1 Answers1

0

I have modified it and you can modify it further. Let me know if it does not work.

import jinja2
from tempfile import NamedTemporaryFile

def return_hosts():
    'return a list of host names'
    with open('./tfhosts') as hosts:
        return [host.split()[1].strip() for host in hosts]

inventory = """
[master1]
{{ host_master01 }}
[master2]
{{ host_master02 }}
"""

gethosts = return_hosts()

inventory_template = jinja2.Template(inventory)

rendered_inventory = inventory_template.render({ 
    'host_master01': gethosts[0], 
    'host_master02': gethosts[1], 
  })

hosts = NamedTemporaryFile(delete=False)
hosts.write(rendered_inventory)
hosts.close()
User
  • 14,131
  • 2
  • 40
  • 59
  • Thanks for the answer. I tried but I still have the same problem, the output file (with your amendment) produces: `[root@runner ~]# cat /tmp/tmpAuvNIF [master01]` `d` `[master02]` `c` Instead of putting the whole line, it's only putting the first 2 letters. It should be: `[root@runner ~]# cat /tmp/tmpAuvNIF` `[master01]` `dc01-control-01` `[master1]` `dc02-control-02` I can't get it to read each line separately until the end. – Stephen K. Oct 04 '15 at 18:55
  • Can you print `gethosts `? Mine is a list. – User Oct 04 '15 at 19:10
  • When I print is comes back as a single item in a list: `['dc01-control-01\ndc01-control-02\ndc01-control-03\ndc01-worker-01\ndc01-worker-02\n']` – Stephen K. Oct 04 '15 at 19:24
  • Sorry I ran the wrong version, using your changes I get: `['dc01-control-01', 'dc01-control-02', 'dc01-control-03', 'dc01-worker-01', 'dc01-worker-02']` I'm just looking to pull it entire line-by-line, how can I slice this list up to do that? So I get: `[master01] dc01-control-01 etc...` – Stephen K. Oct 04 '15 at 19:27
  • It appears to work now, I had a blip in brain function. Thank you! One last question, how can I set it up so that all other hosts by default go under `[master02]`? If I put `gethosts[3:]` I get a full list, and it won't get me split gethosts to impose line breaks. – Stephen K. Oct 04 '15 at 19:56
  • This is a totally different question. Have a look at http://stackoverflow.com/questions/9198334/jinja2-and-for-loop If you can not apply it, ask a new question. – User Oct 04 '15 at 20:03