Saturday, July 13, 2013

Installing and Running Ansible on Mac OSX and pinging ec2 machines

We will be installing Ansible from Git.


Install Ansible

Download ez_setup.py.

Install ez_setup, pip, jinja2 and ansible.
sudo python ez_setup.py
sudo easy_install pip
sudo pip install ansible
sudo pip install jinja2

Define your host file

Create the file /etc/ansible/hosts.

Put the IP of each machine you want to ping.

Example:
[appservers]
255.255.255.255 ansible_ssh_private_key_file={your_key_path}.pem  ansible_ssh_user=ec2-user
Change the IP to your EC2 instance's IP. The [appservers] is just a label for grouping. You may have servers grouped as web servers, app servers, db servers, etc.


Run Ansible

ansible all -m ping

You will see a response similar to the following if it's successful.
255.255.255.255 | success >> {
    "changed": false,
    "ping": "pong"
Let's execute a command on all the machines:

ansible all -a "/bin/echo hello"

You will see:
255.255.255.255 | success | rc=0 >>
hello

Saving the key in memory

If you don't specify the ansible_ssh_private_key_file and ansible_ssh_user attributes in the inventory file above. You can either 1.) specify the key and user in the ansible command or 2.) use ssh-agent.

1.) Explicitly specifying the user and key:
ansible all -m ping -u ec2-user --private-key={your_key}.pem
2.) Using ssh-agent and ssh-add
ssh-agent bash
ssh-add ~/.ssh/{your_key}.pem
Then you can ping the ec2 server like this:
ansible all -m ping -u ec2-user

No comments:

Post a Comment