{"id":2990,"date":"2022-09-13T12:07:38","date_gmt":"2022-09-13T12:07:38","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2990"},"modified":"2023-08-04T16:04:59","modified_gmt":"2023-08-04T16:04:59","slug":"ansible-script-ec2","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/ansible-script-ec2","title":{"rendered":"Ansible script to get state of EC2 instances"},"content":{"rendered":"<p><strong>Ansible is a configuration management tools<\/strong> that handles a wide range of Daily IT operational, automate configuration management, including <strong><a href=\"https:\/\/www.checkmateq.com\/aws-cloud\">cloud<\/a> provisioning<\/strong>, <strong>package management<\/strong>, <strong><a href=\"https:\/\/www.checkmateq.com\/full-stack-development\">application<\/a> deployment<\/strong>, and <strong>intra-service orchestration<\/strong>.<\/p>\n<p>In this blog, we will create an ansible script to get the state of EC2 instances. First we will install awscli in our host machine so that we can create a profile to connect to AWS infrastructure.<\/p>\n<h3>Step1: Install awscli and configure a user profile<\/h3>\n<ul>\n<li>Use the following commands to install awscli.<\/li>\n<\/ul>\n<pre>curl \"https:\/\/awscli.amazonaws.com\/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\r\nunzip awscliv2.zip\r\nsudo .\/aws\/install\r\n<\/pre>\n<ul>\n<li>Create a user profile with following command.<\/li>\n<\/ul>\n<pre>aws configure\r\n<\/pre>\n<ul>\n<li>You will need to provide<strong> access key id<\/strong> and <strong>secret access key<\/strong> of an IAM account to configure user profile.<\/li>\n<\/ul>\n<h3><strong>Step2: Install boto and boto3<\/strong><\/h3>\n<ul>\n<li>Use the following command to install boto, boto3, and botocore<\/li>\n<\/ul>\n<pre>pip3 install boto boto3\r\n<\/pre>\n<p><strong>Step3: Run the ansible script<\/strong><\/p>\n<ul>\n<li>Create a file in your directory and paste the following script there.<\/li>\n<\/ul>\n<pre><code>\r\n---\r\n- name: test aws ec2\r\n  hosts: localhost\r\n  connection: local\r\n  gather_facts: False\r\n\r\n  vars_prompt:\r\n          name: region\r\n          prompt: Enter the region\r\n          private: no\r\n  tasks:\r\n\r\n    - name: Gathers facts (instance metadata) about remote hosts remote ec2 (all)\r\n      ec2_instance_facts:\r\n        region: \"{{ region }}\"\r\n      register: ec2_metadata\r\n\r\n    - debug:\r\n        msg: \"  Instance ID: {{ item.instance_id }}         State: {{ item.state.name }} \"\r\n      with_items: \"{{ ec2_metadata.instances }}\"\r\n      loop_control:\r\n        label: \"{{ item.instance_id }}\"\r\n\r\n<\/code><\/pre>\n<ul>\n<li>Run the script with the ansible playbook command.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2993\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-2-300x119.png\" alt=\"Ansible \" width=\"746\" height=\"296\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-2-300x119.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-2-1024x405.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-2-1200x474.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-2.png 1473w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>This blog is written by Amit Kumar, Director of engineering, at Checkmate Global Technologies. he has worked with worked with various other startups related to mobile app development, Web Development, and<span class=\"Apple-converted-space\">\u00a0 <\/span>Cloud DevOps production operation management.<span class=\"Apple-converted-space\">\u00a0<\/span><a href=\"https:\/\/www.checkmateq.com\/\">Please contact<\/a> with him to discuss cloud infrastructure and SaaS based product engineering.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ansible is a configuration management tools that handles a wide range of Daily IT operational, automate configuration management, including cloud provisioning, package management, application deployment, and intra-service orchestration. In this blog, we will create an ansible script to get the state of EC2 instances. First we will install awscli in our host machine so that &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/ansible-script-ec2\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Ansible script to get state of EC2 instances&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":2995,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[71,70,69,68,7,11,8],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2990"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2990"}],"version-history":[{"count":8,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2990\/revisions"}],"predecessor-version":[{"id":4240,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2990\/revisions\/4240"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/2995"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}