Dynamic inventory Ansible

一个土办法,简单有效。

#!/bin/bash
set -x  
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH

ansible_hosts='/tmp/hosts'  
ipaddress=$1

#clean hosts file
echo '' > $ansible_hosts  
#update hosts file
echo '[init]' >> $ansible_hosts  
echo $ipaddress >> $ansible_hosts  
#for passwd
echo 'ansible_ssh_pass=yaodawang' >> $ansible_hosts

#run test task
ansible -i $ansible_hosts  $ipaddress -a 'w'

retval=$?

if [ $retval -eq 0 ]; then  
   echo "The task run successfully."
   exit 0
else  
  echo "The task was wrong."
  exit 1
fi