提取ansible hosts分组IP

有时候机器太多,想预先看看需要执行的机器IP,看看有没有漏的,注释的机器不会列出来。默认查看ansible hosts文件路径/etc/ansible/hosts

#!/usr/bin/env python
##执行方式 python2.7 1.py bbg nnji
import ConfigParser
import re
import sys
room = sys.argv[1: ] ##获取room分组
for x in room:
        config = ConfigParser.RawConfigParser(allow_no_value=True) ##必须带TRUE参数
        config.read("/etc/ansible/hosts") ##读取配置文件
        print x
        str = config.items(x) ##读取配置组下内容
        for k,v in str: ##字典的方式展示
            str = re.findall("ansible_host=(.+?)ansible_user",v) ##正则匹配ip
            for i in str:
                print i
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐