Tikejhya: Ashish Nepal

Knowledgebase

Category: python

dynamic inventory ec2.py with multiple inventory

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

While running  multiple inventory with ec2.py; You will also need to copy the ec2.ini file to location where you have ec2.py.

My structure was with 4 different environment and i didnt want to copy ec2.py all over the inventory rather use 1 and create ec2.ini according to requirement per environment. (which may be what many would like to do).

root@tikejhya:/etc/ansible# tree
.
├── development
│ └── ec2.ini
├── ec2.py
├── ext.py
├── ext.pyc
├── prod
│ └── ec2.ini
├── roles
├── staging
│ └── ec2.ini
└── uat
├── base
└── ec2.ini

Since there was not really out of the box solution; i created ext.py below and import into ec2.py.

#ec2.py

135 from ansible.module_utils import ec2 as ec2_utils
++ 136 from ext import mapper
137
138 HAS_BOTO3 = False

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

And:

487 help='Use boto profile for connections to EC2')
++ 488 parser.add_argument('--my_env', action='store', dest='my_env',
help='Use env for veriable')
490 self.args = parser.parse_args()
491
++ 492 mapper(self.args.my_env)

#ext.py
#!/usr/bin/python
import os
import sys

def mapper(self_args_my_env):
os.environ[“EC2_INI_PATH”] = “/etc/ansible/” + self_args_my_env + “/ec2.ini”
os.environ[“AWS_PROFILE”] = “profile_” + self_args_my_env

This made me able to run ansible with ec2.py followed by env parameter and use relevent boto profile.

ec2.py –my_env uat –list

virtualenv with centos

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Download Python and extract it
wget http://www.python.org/ftp/python/2.7.8/Python-2.7.8.tar.xz
xz -d Python-2.7.8.tar.xz
tar -xvf Python-2.7.8.tar

Installation process
Since we already installed all the dependencies we are ready to go:

# Enter the directory:
cd Python-2.7.8

# Run the configure:
./configure --prefix=/usr/local

# compile and install it:
make
make altinstall

# Checking Python version:
[root@tikejhya ~]# python2.7 -V
Python 2.7.8
If you need set PATH variable check the line below:

export PATH=”/usr/local/bin:$PATH”
Installing pip and virtualenv
Now we have Python installed, but something is missing isn’t? Yes! We need pip and virtualenv.

Install setuptools
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz

# Extract the files:
tar -xvf setuptools-1.4.2.tar.gz
cd setuptools-1.4.2

# Install setuptools using the Python 2.7.8:
python2.7 setup.py install
Install pip
curl https://bootstrap.pypa.io/get-pip.py | python2.7 -
And finally virtualenv
pip2.7 install virtualenv

virtualenv projectname
source projectname/bin/activate

python --version

#exit virtualenv
deactivate

#python binary location
/home/tikejhya/projectname/bin/python

Upgrade Python

If you are getting requirement issue and upgrade python, removing python is pain due to dependencies some times.

You may want to point symlink to solve the problem.

[root@ tikejhya]# ll /usr/bin/python*
-rwxr-xr-x 2 root root 8304 Feb 2 21:06 /usr/bin/python
lrwxrwxrwx 1 root root 6 Apr 7 19:30 /usr/bin/python2 -> python
-rwxr-xr-x 2 root root 8304 Feb 2 21:06 /usr/bin/python2.4
-rwxr-xr-x 2 root root 4736 Apr 2 22:00 /usr/bin/python26

Remove sym link
[root@tikejhya]# rm /usr/bin/python2

move 2.6 as python or create symlink pointing as python.
[root@tikejhya]# mv python2.6 python

Powered by WordPress & Theme by Anders Norén