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

How to setup ansible practice lab in aws ec2 instances

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

setup ansible lab in aws ec2 instanaces

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

setup ansible in aws

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

configure ansible in aws ec2

Paste id_rsa.pub  key of the master here

setup ansible in aws ec2now 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

install ansible in aws ec2 instance

here 10.0.0.14 is private IP of the client1 machine

here you can mention all client machines private IP's

configure ansible in aws

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

 

 

2 Responses

  1. vamsi krishna dhulipala says:

     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)

    sudo apt-get update &&
    sudo apt install software-properties-common &&
    sudo apt-add-repository ppa:ansible/ansible &&
    sudo apt-get update &&
    sudo apt install ansible &&
    sudo apt-get update

    On Ansible Client
    ** Do not use root privileges (NO sudo su)

    sudo apt-get update &&
    sudo apt-get install python &&
    sudo apt-get update

    On Ansible Master

    cd .ssh
    ssh ubuntu@172.31.85.140 
    ls
    ssh-keygen
    ls
    sudo cat id_rsa.pub

    On Ansible Client

    cd .ssh
    ls
    sudo nano authorized_keys 
    Paste the key from Ansible Master  id_rsa.pub to authorized_keys in ansible client

    On Ansible Master

    ssh ubuntu@172.31.85.140  (—> Ansible client IP Address)
    Connected to Ansible Client
    exit (to close the connection)

    On Ansible Master

    sudo nano /etc/ansible/hosts
    Inside hosts

    [production]
    client1 ansible_ssh_host=172.31.85.140  (–> client ip address)

    save & exit
    ansible -m ping client1

    On Ansible Master  
    Creating Ansible Roles

    cd /etc/ansible/roles/
    inside roles Install tree
    sudo apt install tree
    To create Ansible Role
    sudo ansible-galaxy init <role name> –offline
    To see the tree
    tree web (–>role name)
    ubuntu@ip-172-31-84-177:/etc/ansible/roles$ tree web
    web(–> role name)
    ├── README.md
    ├── defaults
    │   └── main.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── tasks
    │   └── main.yml
    ├── templates
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
    └── main.yml

    cd web
    cd tasks
    ubuntu@ip-172-31-84-177:/etc/ansible/roles/web/tasks$  

    ls (shows main.yml file)
    sudo nano main.yml

    inside main.yml file

    # tasks file for web

    – include: install.yml

    – include: configure.yml

    – include: service.yml

    save & exit

     
    Inside tasks folder create
    install.yml
    configure.yml
    service.yml

    inside install.yml

      – name: install apache2

        apt: name=apache2 update_cache=yes state=latest

    inside configure.yml

      – name: configure website

        copy: src=index.html dest=/var/www/html

    inside service.yml

      – name: starting apache2 service

        service: name=apache2 state=started

     

    cd .. (inside the web directory)

    ubuntu@ip-172-31-84-177:/etc/ansible/roles/web$    

    ls

    cd files

    sudo nano index.html

    inside index.html

    <html> Hello world !</html>

    cd ..

    cd ..

    cd ..  (inside the ansible directory)

    ubuntu@ip-172-31-84-177:/etc/ansible$     

    sudo nano site.yml
    inside site.yml

      – hosts: client1

        roles:

     – web

    save & exit

     

    ansible-playbook site.yml  –syntax-check

    ansible-playbook site.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.

    • decoding devops says:
      1. first kill the process which started apt get update
      2. where did you paste the code i mean on which user(client) .ssh/authorized_keys file
      3. when u r connecting to the client machine u have to know through which user(client) u r connecting, check this article once https://www.decodingdevops.com/ansible-become-tutorial/
      4. if u want to run commands like apt get update or apt get install, the user(client) should be in sudo user

Leave a Reply

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