Changing a computer IP address using fabric freezing

0 votes

I'm trying to run a fabric script to change 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)

But whenever the IP address changes, fabric freezes with 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'd like to run other tasks after I change IP tasks. Is there anything I can do to run this?

Jul 20, 2018 in DevOps & Agile by ajs3033
• 7,300 points
735 views

1 answer to this question.

0 votes
Best answer

I used the following script to make it work:

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 executes the

fab -f fabfile.py change_ip test_task

command to produce this 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:
answered Jul 20, 2018 by ajs3033
• 7,300 points

Related Questions In DevOps & Agile

+1 vote
1 answer

How do I get a Docker container's IP address from the host?

Save your docker image as a tar ...READ MORE

answered Aug 29, 2018 in DevOps & Agile by shubham
• 7,340 points
997 views
+13 votes
2 answers
0 votes
1 answer
0 votes
1 answer

Get the Ip of a docker container

Here is what you need to do.  Firstly, ...READ MORE

answered Dec 10, 2018 in DevOps & Agile by Damon Salvatore
• 5,980 points
1,173 views
+2 votes
1 answer
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
1,004 views
0 votes
1 answer

How to set up a VM with KVM/qemu without VitualBox using Vagrant

Start the vagrant box using: vagrant up --provider=kvm But ...READ MORE

answered Apr 11, 2018 in DevOps & Agile by ajs3033
• 7,300 points
2,402 views
+2 votes
2 answers

how to set different provisioning profiles for different targets using Xcode Build

For multiple targets, each task has to ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by ajs3033
• 7,300 points

edited Oct 16, 2018 by Kalgi 4,819 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP