Python implements 2 file replacement updates

Source: Internet
Author: User

Business requirements: Ansible synchronization, hosts need to be updated with changing zk-hosts files. And in the specified location to add and delete, in this case add and delete for [prod] this project.

Idea: Each [prod] square bracket begins with an item as a dictionary key, with the IP under it as a value. Make it into a dictionary. When updating, first go to the [] item name, then go to update its value, including delete, increase, determine whether there is.

[email protected] test]$ Cat hosts

[Test]

192.168.3.3

[Pre]

192.168.1.137

[PROD]

192.168.1.61

192.168.1.62

192.168.1.63

[PROD_CLI]

192.168.1.158

192.168.1.159

[Prod_admin]

192.168.1.8


[[email protected] test]$ cat zk-hosts changed files

[All]

-192.168.1.61

-192.168.1.62

10.0.0.1

10.0.0.2


# source Code

#!/usr/bin/env python

Import Os,sys,re

#os. System (' cat/root/hosts/test/hosts | tr ' \ n ' | xargs echo | tr ' [' \ n ' | tr '] ' &>hosts.txt ')

def update_ip (*project):

Project=raw_input ("Pleas input project")

Os.system ('/bin/bash a.sh ') #

d={}

Ip=[]

F=open (' Hosts.txt ', ' A + ')

A=f.readlines ()

F.close ()

For line in a:

env= (Line.split () [0])

Iplist=line.split () [1:]

#if Env==project:

D[env]=iplist

#print D

#print Project

Ciplist=d[project]

#print ciplist

#

Zk=[]

Z=open (' zk-hosts ')

For hosts in Z.readlines ():

Hosts=hosts.strip (' \ n ')

Zk.append (Hosts)

Zk.pop (0)

#print ZK

#

For I in ZK:

#print I

If I in Ciplist:

Print "%s is exists"% i

Elif Re.match (R '-(. *) ', i):

I=i.lstrip ('-')

If I in Ciplist:

Ciplist.remove (i)


Else

Ciplist.append (i)

#print ciplist

#print D.items ()

B=open (' hosts ', ' A + ')

B.truncate ()

For key in D:

B.write (' [' +key+ '] ' + ' \ n ')

For M in D[key]:

Print m

B.write (m+ ' \ n ')

B.close ()

UPDATE_IP ()


#python脚本中调用的 shell script.

[email protected] test]$ cat a.sh

#!/bin/bash

cat/root/hosts/test/hosts | Tr ' \ n ' | Xargs Echo | TR ' [' \ n ' | tr '] ' &>hosts.txt

Sed-i ' 1d ' hosts.txt

#cat/root/hosts/test/zk-hosts | Tr ' \ n ' | Xargs Echo | TR ' [' \ n ' | tr '] ' &>zk-hosts.txt

#sed-i ' 1d ' zk-hosts.txt




Post-execution results

[email protected] test]$ Cat hosts

[Test]

192.168.3.3

[Pre]

192.168.1.137

[PROD]

192.168.1.63

10.0.0.1

10.0.0.2

[Prod_admin]

192.168.1.8

[PROD_CLI]

192.168.1.158

192.168.1.159


This article is from the "Hand over Incense" blog, please be sure to keep this source http://19941018.blog.51cto.com/11889001/1983888

Python implements 2 file replacement updates

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.