Deep Dive with Ansible Dynamic inventory for aws cloud

Aws Cloud is so vast in services and it will be great if we can integrate with Ansible : 






Requirment : 


  1.    setup and install ansible 
  2.    install boto  2.24.0 +
Note :   I am using  ubuntu  16.04  for  setting up amazon  dynamic  Inventory.

Steps :

  1.     Installing   ansible  and  boto
    root@adhoc:~#    sudo apt-add-repository ppa:ansible/ansible
    root@adhoc:~#    sudo apt  update
    root@adhoc:~#    sudo  apt  install ansible 

BOTO  Installation : 


     root@adhoc:~#    sudo  apt  install python-pip
     root@adhoc:~#    pip  install  boto

AWS CLI  Installation  : 
      root@adhoc:~#    pip  install  awscli  
      
AWS CLI  CONFIGURATION : 

      adhoc@adhoc:~$ sudo -i
      [sudo] password for adhoc: 
      root@adhoc:~# aws configure
       AWS Access Key ID [****************W5UQ]: 
       AWS Secret Access Key [****************0QTW]: 
       Default region name [us-west-2]: 
       Default output format [None]: 


Downloading  Ec2.py and  Ec2.ini  file  for Dynamic  Inventory 

  root@adhoc:~#   cd  /etc/ansible/

  root@adhoc:~#   wget  https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py

  root@adhoc:~#   wget  https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini

  root@adhoc:~#   chmod  +x   ec2.py  ec2.ini


exporting some variables : 

  root@adhoc:~# export ANSIBLE_HOSTS=/etc/ansible/ec2.py

root@adhoc:~# export ANSIBLE_INI=/etc/ansible/ec2.ini


Now get all running Instances details info : 

Important: make sure your ansible time is updated as per internet Time.

root@adhoc:/etc/ansible# /etc/ansible/ec2.py  --list

{
  "_meta": {
    "hostvars": {
      "52.41.118.0": {
        "ansible_ssh_host": "52.41.118.0", 
        "ec2__in_monitoring_element": false, 
        "ec2_account_id": "904182454435", 

        "ec2_ami_launch_index": "0", 




Checking  Ping  module if firewall is configured : 

root@adhoc:/etc/ansible# ansible  all -u ec2-user --private-key /home/adhoc/Downloads/class1.pem -m ping 

52.41.118.0 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}


Tip:    you can add private key location using ssh-agent so you don't have to specify ssh-keys

 root@adhoc:/etc/ansible#  ssh-agent  bash 
 root@adhoc:/etc/ansible#  ssh-add ~/.ssh/keypair.pem


Now you can ping to all ec2 instances

[root@localhost ~]# ansible all -m ping  -u ec2-user

[DEPRECATION WARNING]: ANSIBLE_HOSTS option, The variable is misleading as it can be a list of hosts and/or paths to
 inventory sources . This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
13.126.182.236 | SUCCESS => {
    "changed": false,
    "failed": false,
    "ping": "pong"
}

Note: ping only specific tag machine 
 
[root@localhost ~]# ansible  -m ping  -u ec2-user tag_ansible_machine[DEPRECATION WARNING]: ANSIBLE_HOSTS option, The variable is misleading as it can be a list of hosts and/or paths to
 inventory sources . This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
13.126.182.236 | SUCCESS => {
    "changed": false,
    "failed": false,
    "ping": "pong"
}
 


Some tasks :  


  1.   Installing  httpd  software  

root@adhoc:/etc/ansible# ansible  all -s -u ec2-user --private-key /home/adhoc/Downloads/class1.pem -m yum -a "name=httpd state=present"

Note :   here   -s  means  sudo     and  -m for module

52.41.118.0 | SUCCESS => {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [

        "Loaded plugins: priorities, update-motd, upgrade-helper\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.31-1.8.amzn1 will be installed\n--> Processing Dependency: httpd-tools = 2.2.31-1.8.amzn1 for package: httpd-2.2.31-1.8.amzn1.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.31-1.8.amzn1.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.31-1.8.amzn1.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.31-1.8.amzn1.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.5.1-1.12.amzn1 will be installed\n---> Package apr-util.x86_64 0:1.4.1-4.17.amzn1 will be installed\n---> Package apr-util-ldap.x86_64 0:1.4.1-4.17.amzn1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.31-1.8.amzn1 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package             Arch         Version                 Repository       Size\n================================================================================\nInstalling:\n httpd               x86_64       2.2.31-1.8.amzn1        amzn-main       1.2 M\nInstalling for dependencies:\n apr                 x86_64       1.5.1-1.12.amzn1        amzn-main       116 k\n apr-util            x86_64       1.4.1-4.17.amzn1        amzn-main        87 k\n apr-util-ldap       x86_64       1.4.1-4.17.amzn1        amzn-main        17 k\n httpd-tools         x86_64       2.2.31-1.8.amzn1        amzn-main      





Comments


  1. Thankyou for sharing Good information...
    AWS Training

    ReplyDelete
  2. Hey thanks for this amazing post! Thank you so much for sharing the good post, I appreciate your hard work.Keep blogging.
    DevOps Training in Electronic City

    ReplyDelete
  3. Thanks for one marvelous posting! I enjoyed reading it; you are a great author.
    Thanks for giving a great information about nice Article..
    I have learned a lot from this

    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    hadoop training in chennai

    hadoop training in bangalore



    ReplyDelete
  4. This article is a creative one and the concept is good to enhance our knowledge. Waiting for more updates.
    Devops Online Course
    JMeter Online Training

    ReplyDelete
  5. valuable blog,Informative content...thanks for sharing, Waiting for the next update…
    Google Flutter and Dart Online Training
    Flutter Online Courses

    ReplyDelete
  6. Valuable blog, Informative content...thanks for sharing, Waiting for the next update…

    Spanish Language Classes in Chennai
    Spanish Courses in Chennai

    ReplyDelete
  7. Really an informative blog...Thanks for sharing an informative article with us.

    Toefl Coaching in Madurai
    Toefl Training in Madurai

    ReplyDelete
  8. Really an informative blog...Thanks for sharing an informative article with us.

    French Classes in Bangalore
    French Language Classes in Bangalore

    ReplyDelete

  9. Nice to see this BLOG..keep updating More infromation Digital Lync offers one of the best Full Stack training in Hyderabad with a comprehensive course curriculum with Continuous Integration, Delivery, and Testing. Elevate your practical knowledge with quizzes, assignments, Competitions, and Hackathons to give a boost to your confidence with our hands-on Full Stack Training.
    DevOps Training Institute
    Python Training Institute
    AWS Training Institute
    Online Full Stack Developer Course Hyderabad
    Python Course Hyderabad
    Online AWS Training Course Hyderabad
    devops training in hyderabad
    angular training in hyderabad

    ReplyDelete
  10. Really nice blog. thanks for sharing such a useful information.
    Kotlin Online Course

    ReplyDelete
  11. This information really helped me a lot. It was very informative.
    Devops Services

    ReplyDelete

Post a Comment