How to install ansible on AWS-setup ansible lab in aws with ec2 instances
How to install ansible on AWS ec2 instances
to install ansible on Amazon Linux or to setup ansible lab in aws we need two or three ec2 instances. one is ansible master ec2 instance remaining ec2 instances are clients. in the master ec2 instance only we will install ansible.
Launch three or two ubuntu 16.04 instances
give Name one ubuntu ec2 instances as ansible-master
give remaining ec2 instances names as client1, clinet2
in both ansible master and clients security groups
open ssh port no 22 from anywhere
ansible built on python so install python in all machines
install python in ansible master and clients instances
ansible and its modules are built on python, so we have to install python in all master and client machines.
to install python execute below commands as root user
sudo -i apt-get install python-minimal apt-get install python3
check python version with
python --version Python 2.7.12
installing ansible in ansible master instance
run below commands as root user
sudo -i apt-get update apt-get install software-properties-common apt-add-repository ppa:ansible/ansible apt-get update apt-get install ansible
check ansible with
ansible --version ansible 2.6.3 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/dist-packages/ansible executable location = /usr/bin/ansible python version = 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609]
establish ssh connection between ansible master and clients
to establish a connection between master and clients we have to generate the id_rsa.pub key in master and paste this key in authorized_keys file of client machines. This file exists in the .ssh directory. So if the .ssh directory has not existed in client ec2 instances, We have to create the .ssh directory and inside that, we have to create the authorized_keys file.
generating id_rsa.pub public key in ansible master instance
in master, ec2 instance execute below commands
sudo –i ssh-keygen -t rsa
It will create the id_rsa.pub key in the .ssh directory
cd .ssh ls id_rsa id_rsa.pub known_hosts cat id_rsa.pub
Copy this id_rsa.pub key
In All Client Ec2 Instances
Sudo -i
cd .ssh
ls
Here you can see the authorized_keys file. [If the file is not existed here create the file with touch command
touch authorized_keys and paste the id_rsa.pub key in this file
Vi authorized_keys
Paste id_rsa.pub key of the master here
now we have shared ssh keys between master and clients
Adding clients to ansible master
to add clients to ansible master machine, we need to add all IP's of clients in master machine /etc/ansible/hosts file
now go to ansible master machine
Ansible AWS Inventory
cd /etc/ansible
vi hosts
add like below
[web]
10.0.0.14
here 10.0.0.14 is private IP of the client1 machine
here you can mention all client machines private IP's
the first time it will ask are you sure you want to continue connecting yes/no
write yes and click on enter
you can see the output in green color.
now we have successfully configured ansible practice lab in aws.
now you can run your playbooks and roles in clients.
IMP POINTS
- allow ssh port forwarding between clients and master by opening port no 22
- install python in all master and client ec2 instances
- install ansible in master ec2 instance
- generate the public key in ansible master instance
- copy id_rsa.pub key and paste in all clients instances authorized_keys file
- enter all client IPs in master /etc/ansible/hosts file
- now you can run your playbooks
- install ansible in aws ec2 instances
- ansible aws
- ansible training in dilsukhnagar
- aws ansible
- install ansible on Amazon Linux
- ansible aws example
- ansible connect to the ec2 instance
- how to install ansible on aws ec2 instance
I am creating and working Ansible roles, followed the procedure till the end (procedure copy pasted below) receiving the error message I would like to request for a resolution and assistance to this issue.
Procedure:
Create two aws ec2 (ubuntu) instances, for Ansible Master and Ansible Client
On Ansible Master
** Do not use root privileges (NO sudo su)
On Ansible Client
** Do not use root privileges (NO sudo su)
On Ansible Master
On Ansible Client
On Ansible Master
On Ansible Master
On Ansible Master
Creating Ansible Roles
Inside tasks folder create
install.yml
configure.yml
service.yml
Error:
[WARNING]: Updating cache and auto-installing missing dependency: python-apt
fatal: [client1]: FAILED! => {“changed”: false, “cmd”: “apt-get update”, “msg”:
“E: Could not open lock file /var/lib/apt/lists/lock – open (13: Permission denied)\nE: Unable to lock directory /var/lib/apt/lists/\nW: Problem unlinking the file /var/cache/apt/pkgcache.bin – RemoveCaches (13: Permission denied)\nW: Problem unlinking the file /var/cache/apt/srcpkgcache.bin – RemoveCaches (13: Permission denied)”, “rc”: 100, “stderr”:
“E: Could not open lock file /var/lib/apt/lists/lock – open (13: Permission denied)\nE: Unable to lock directory /var/lib/apt/lists/\nW: Problem unlinking the file /var/cache/apt/pkgcache.bin – RemoveCaches (13: Permission denied)\nW: Problem unlinking the file /var/cache/apt/srcpkgcache.bin – RemoveCaches (13: Permission denied)\n”, “stderr_lines”:
[“E: Could not open lock file /var/lib/apt/lists/lock – open (13: Permission denied)”, “E: Unable to lock directory /var/lib/apt/lists/”, “W: Problem unlinking the file /var/cache/apt/pkgcache.bin – RemoveCaches (13: Permission denied)”, “W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin – RemoveCaches (13: Permission denied)”], “stdout”: “Reading package lists…\n”, “stdout_lines”: [“Reading package lists…”]}
PLAY RECAP ********************************************************************************************************************************************
client1 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
How to resolve it, can you please assist me.