def host(ipaddress, hostnames, use_sudo=False):
 res = run_as_root(('cat /etc/hosts | egrep "^%(ipaddress)s"' % locals()))
 if res.succeeded:
  m = re.match(('^%(ipaddress)s (.*)' % locals()), res)
  if m:
   toadd = list()
   hostnames = hostnames.split(' ')
   inthehosts = m.group(1).split(' ')
   for h in hostnames:
    if (h not in inthehosts):
     toadd.append(h)
   if (len(toadd) > 0):
    print ('ADD: %s' % toadd)
    print res
    hostline = ('%s %s' % (res, ' '.join(toadd)))
    with hide('stdout', 'warnings'):
     sed('/etc/hosts', res, hostline, use_sudo=use_sudo)
  else:
   hostline = ('%s %s' % (res, hostnames))
   append('/etc/hosts', hostline, use_sudo=use_sudo)

def host ( ipaddress , hostnames , use_sudo = False ) : res = run_as_root ( ( 'cat /etc/hosts | egrep "^%(ipaddress)s"' % locals ( ) ) ) if res . succeeded : m = re . match ( ( '^%(ipaddress)s (.*)' % locals ( ) ) , res ) if m : toadd = list ( ) hostnames = hostnames . split ( ' ' ) inthehosts = m . group ( 1 ) . split ( ' ' ) for h in hostnames : if ( h not in inthehosts ) : toadd . append ( h ) if ( len ( toadd ) > 0 ) : print ( 'ADD: %s' % toadd ) print res hostline = ( '%s %s' % ( res , ' ' . join ( toadd ) ) ) with hide ( 'stdout' , 'warnings' ) : sed ( '/etc/hosts' , res , hostline , use_sudo = use_sudo ) else : hostline = ( '%s %s' % ( res , hostnames ) ) append ( '/etc/hosts' , hostline , use_sudo = use_sudo )
add a ipadress and hostname(s) in /etc/hosts file example:: from fabtools import require require .