Skip to content

Setup Ansible

Quick Start Ansible for AWS

Starter Ansible template for AWS

Create a host and a managed-node

Then install ansible inside host. Below command is for Amazon linux2.

sudo amazon-linux-extras install ansible2
Verify ansible is installed
ansible --version
Copy contents of pem file to host (Assuming you created managed-node on AWS and you downloaded your .pem file with which you can SSH to your managed-node). Read the contents of .pem file and copy-paste to a new file key.pem. You can use vim or nano editor to paste contents. You need to reduce the permission of your key file.
 chmod 400 key.pem 
Now in your ansible host machine. Add key file to ssh-agent.
ssh-agent bash
cp key.pem ~/.ssh/
chmod 400 ~/.ssh/key.pem
ssh-add ~/.ssh/key.pem
Create inventory file inventory.txt. Inventory file is used to store hosts meta-data. Here we are explicityly creating a host file but the default host file is located in /etc/ansible/hosts

Create host file

touch inventory.txt
Inside the file paste
myservers ansible_host=54.242.242.100 ansible_connection=ssh ansible_user=ec2-user
Try to ping the host and check if you have a successful connection
 ansible myservers -m ping -i inventory.txt

make sure you are in the same session in which you added bash key, if not you may want to do again ssh-agent bash & ssh-add ~/.ssh/key.pem

<!-- add your hosts to the file

[myservers]
35.175.171.151 -->
<!-- ``` -->
Now run the playbook
```bash
ansible-playbook install-nginx.yaml -vv
Ansible will start a nginx server in your managed-node. If Ansible successfully deployed nginx and you are still cannot open your nginx site on your managed-mode, try to check your security groups, network settings and firewall settings.