Python is configured according to your needs hostname

Source: Internet
Author: User

#!/usr/bin/env python# _*_coding:utf-8 _*_ "If you want to run a batch configuration: Run this script with Ansible Ansible testserver  -m script -a  "chage_hostname.py"   "import osshell = "/sbin/ifconfig  -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk  ' {print $2} ' |tr  -d  ' addr: '   #定义获取IP地址的shell命令res  = os.popen (Shell, ' R ', 0)   #执行shell命令, and return value IPADDR  = str (Res.read ())   #读取返回值, i.e. IP address str_ip = ipaddr.replace ('. ', '-')   #将IP地址中的 "." Replace with "-" hostnames =  ' ambari-%s '  % str_ip  #组成完整的计算机名os. environ[' new_name ']=str (hostnames   #将python变量可传递给shellos. System (' hostname  $new _name ')   #修改计算机名str_hosts  =  "%s %s"  %  (ipaddr,hostnames)   #组成 "Ip hostname" form, write/etc/hosts file os.environ[' ip_host ']=str (str_hosts )   #将python变量可传递给shellos. System (' rm -rf /etc/hosts ') os.system (' echo  $ip _host >>  /etc/hosts ') OS.SYstem ("echo  ' 127.0.0.1   localhost localhost.localdomain localhost4  Localhost4.localdomain4 '  >> /etc/hosts ") os.system (" echo  '::1          localhost localhost.localdomain localhost6  Localhost6.localdomain6 '  >> /etc/hosts ")


This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1875874

Python is configured according to your needs hostname

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.