2

I have a small fabric script that changes the IP of a server:

from cStringIO import StringIO
import os

import fabric
import fabric.api
import jinja2

fabric.state.env['hosts'] = '10.1.0.4'

def render(tpl_path, context):
    path, filename = os.path.split(tpl_path)
    template_stream = jinja2.Environment(loader=jinja2.FileSystemLoader(path or './')).get_template(filename).stream(context)
    output_stream = StringIO()
    for chunk in template_stream:
        output_stream.write(chunk)
    return output_stream

def change_ip():
    ifcfg_ens192 = render("ifcfg.j2", {
        "device": "ens192",
        "ip_address": "10.1.0.20",
        "prefix": "24"
    })

    fabric.operations.put(ifcfg_ens192, "/etc/sysconfig/network-scripts/ifcfg-ens192", use_sudo=True, mirror_local_mode=True)

    fabric.operations.sudo("service network restart")

fabric.tasks.execute(change_ip)

Unfortunately, when the IP address is changed, fabric hangs with the following output.

[10.1.0.4] Executing task 'change_ip'
[10.1.0.4] Login password for 'root':
[10.1.0.4] put: <file obj> -> /etc/sysconfig/network-scripts/ifcfg-ens192
[10.1.0.4] sudo: service network restart
[10.1.0.4] out: Restarting network (via systemctl):

I would like to execute other task after the change IP task. Is there a good way to fix this?

9000
  • 39,899
  • 9
  • 66
  • 104
Tim Ludwinski
  • 2,704
  • 30
  • 34

1 Answers1

0

The following script seemed to do the trick:

from cStringIO import StringIO
import os

import fabric
import fabric.api
import jinja2

change_host = '10.1.0.4'
change_to = '10.1.0.20'

fabric.state.env['hosts'] = [change_host]
fabric.state.env['user'] = 'user'

def render(tpl_path, context):
    path, filename = os.path.split(tpl_path)
    template_stream = jinja2.Environment(loader=jinja2.FileSystemLoader(path or './')).get_template(filename).stream(context)
    output_stream = StringIO()
    for chunk in template_stream:
        output_stream.write(chunk)
    return output_stream

def change_ip():
    ifcfg_ens192 = render("ifcfg.j2", {
        "device": "ens192",
        "ip_address": change_to,
        "prefix": "24"
    })

    fabric.operations.put(ifcfg_ens192, "/etc/sysconfig/network-scripts/ifcfg-ens192", use_sudo=True, mirror_local_mode=True)

    try:
        fabric.operations.sudo("service network restart", timeout=0.5)
    except fabric.exceptions.CommandTimeout:
        pass

    fabric.state.env['hosts'] = [change_to]

def test_task():
    fabric.operations.run("echo 'hi'")

It runs with the following command: fab -f fabfile.py change_ip test_task

And produces the following output:

[10.1.0.4] Executing task 'change_ip'
[10.1.0.4] Login password for 'user':
[10.1.0.4] put: <file obj> -> /etc/sysconfig/network-scripts/ifcfg-ens192
[10.1.0.4] sudo: service network restart
[10.1.0.4] out: sudo password:
[10.1.0.4] out: Restarting network (via systemctl):  [10.1.0.20] Executing task 'test_task'
[10.1.0.20] run: echo 'hi'
[10.1.0.20] out: hi
[10.1.0.20] out:
Tim Ludwinski
  • 2,704
  • 30
  • 34