To Install on MacOS

Add pip:

$ sudo easy_install pip

Install ansible:

$ sudo pip install ansible

Ping (EC2 Instance)

sudo mkdir /etc/ansible
cd /etc/ansible
sudo vi hosts

hosts (list of your hosts):

ec2-52-15-xxx-yyy.us-east-2.compute.amazonaws.com

use ssh-agent?

$ ssh-agent bash
$ ssh-add ~/aws_key.pem

Ping

To connect you will need to add the user, for ubuntu instances in aws it will be ubuntu

ansible all -m ping -u ubuntu

Inventory files

To connect you will need to add the user, for ubuntu instances in aws it will be ubuntu

ansible all -m ping -u ubuntu
bash-3.2$ ansible all -m ping -u ubuntu
ec2-52-15-xxx-yyy.us-east-2.compute.amazonaws.com | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}

If you get the error below on ubuntu 16.04 you probably need to specify the python interpreter to be used on the “target host”.

ec2-18-221-xxx-yyy.us-east-2.compute.amazonaws.com | FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Shared connection to ec2-18-221-xxx-yyy.us-east-2.compute.amazonaws.com closed.\r\n", 
    "module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n", 
    "msg": "MODULE FAILURE", 
    "rc": 0
}

An easy way is to add to the inventory file.

[webserver]
ec2-52-15-xxx-yyy.us-east-2.compute.amazonaws.com

[webserver:vars]
ansible_python_interpreter=/usr/bin/python3

you can also add the user to the hosts file, as a variable:

[webserver]
ec2-52-15-xxx-yyy.us-east-2.compute.amazonaws.com

[webserver:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu

Instead of having a global /etc/ansible/hosts file you can specify it, using -i <hosts_file_path>,  when running ansible commands
for example, to list all hosts in an inventory file:

ansible -i ~/aws/my-hosts all --list-hosts
bash-3.2$ ansible -i ~/aws/my-hosts all --list-hosts
  hosts (1):
    ec2-52-15-xxx-yyy.us-east-2.compute.amazonaws.com

References

Categories: TL;DR

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *