#!/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