If you work with the AWS CLI and it's cousins, you notice quickly that there are different versions and functionality depending on which version of python you use to install them. Therefore, I recommend setting up a virtual environment for each version of python, so you can always be working with the most functional packages and not have to create confusion in your root environment.
OSX
Step 1: Install tooling
- brew: Covered in this link
- pip:
sudo easy_install pip
- virtualenv:
sudo pip install virtualenv
- Python3:
brew install python3
Step 2: Setup a virtual environment for your python versions
mkdir ~/Projects cd ~/Projects pyvenv 35-python virtualenv --system-site-packages 27-python
Step 3: Test your new virtual environments
cd ~/Projects/35-python source bin/activate (35-python) [HOSTNAME]$ python --version (35-python) [HOSTNAME]$ deactivate cd ~/Projects/27-python source bin/activate (27-python) [HOSTNAME]$ python --version (27-python) [HOSTNAME]$ deactivate
Step 4: Install your AWS toolkits into your virtual environments
cd ~/Projects/35-python source bin/activate easy_install pip pip install --upgrade pip pip install awscli deactivate cd ~/Projects/27-python source bin/activate easy_install pip pip install --upgrade pip pip install awscli deactivate
AWS Linux
yum install gcc openssl-devel.x86_64 -y rinse / repeat instructions from OSX steps 2, 3 & 4
Windows
Important Notes
- ANSIBLE DOESN'T RUN FROM PIP IN WINDOWS! There are lots of tools that will work, but Ansible isn't one of them.
- the AWS CLI requires Powershell 5. To check your version:
-
[User@frankenamr:SecureCloud-27]$ $psversiontable Name Value ---- ----- PSVersion 5.0.10586.117 PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.10586.117 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1
- If you don't have Powershell 5, you can get it here
Instructions
- download and install Python 2.7
- download and install Python 3.5
- add c:\python27 and c:\python27\Scripts to your PATH
- Install virtualenv:
pip install pip --upgrade (ignore errors) pip install virtualenv pip install virtualenvwrapper-powershell (optional)
- Create your Python-27 environment:
mkdir ~\Documents\Projects cd ~\Documents\Projects virtualenv 27-Python
- Create your Python-35 environment:
cd ~\Documents\Projects c:\Python35\python.exe -m venv 35-Python
- Test your environments as outlined above in step 3, using the following command to activate your environments:
[User@m-20eg-r90ha96z:35-Python]$ .\Scripts\activate
Test an ansible deployment against AWS
- Install packages using pip
- Python 2.7
pip install ansible --user python pip install boto
- Python 3.x
pip install ansible pip install boto
- Create your inventory
vim inventory.ini
Insert the following:[localhost] localhost
write and quit - Create your credentials
vim ~/.boto
Insert the following text:[Credentials] aws_access_key_id = ACCESS_KEY aws_secret_access_key = SECRET_KEY
Write and quit - Create a simple playbook
vim hello-world.yml
insert the following text:- name: Create a sandbox instance hosts: localhost gather_facts: False vars: key_pair: [KEY-NAME] instance_type: t2.micro image: ami-6869aa05 region: us-east-1 tasks: - name: Launch instance ec2: key_name: "{{ key_pair }}" instance_type: "{{ instance_type }}" image: "{{ image }}" wait: true region: "{{ region }}" vpc_subnet_id: subnet-5e7f9e17 assign_public_ip: yes register: ec2 - name: Add new instance to host group add_host: hostname={{ item.public_ip }} groupname=launched with_items: ec2.instances - name: Wait for SSH to come up wait_for: host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started with_items: ec2.instances
Write and quit - Run your playbook
ansible-playbook hello-world.yml