{"id":2856,"date":"2014-11-19T17:49:17","date_gmt":"2014-11-19T23:49:17","guid":{"rendered":"http:\/\/benincosa.com\/blog\/?p=2856"},"modified":"2014-11-19T18:05:59","modified_gmt":"2014-11-20T00:05:59","slug":"remove-an-ec2-host-with-ansible","status":"publish","type":"post","link":"https:\/\/benincosa.com\/?p=2856","title":{"rendered":"Remove an EC2 host with Ansible"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone aligncenter\" src=\"http:\/\/cdn2.hubspot.net\/hub\/330046\/file-449187601-png\/ansible_badge.png?t=1416435938298\" alt=\"\" width=\"79\" height=\"79\" \/><\/p>\n<p>I spent forever trying to understand the built in Ansible ec2 modules. \u00a0What I was trying to figure out seemed simple: \u00a0How do you delete all your ec2 instances? \u00a0Nothing too clever. \u00a0Turns out it was easier to create the instances than delete it (for me anyway). \u00a0So I&#8217;m writing this down here so I remember.<\/p>\n<p>I&#8217;ve also created an <a href=\"https:\/\/github.com\/vallard\/Ansible\">Ansible playbook github repository<\/a> where I&#8217;ll be putting all the stuff I use. \u00a0 I also plan on doing a post where I show how my environment was set up.<\/p>\n<p>For now, here is the playbook:<\/p>\n<pre class=\"theme:sublime-text lang:yaml decode:true \" title=\"terminate-all-ec2.yml\"># Delete's AWS images\r\n# this post has way to get images. \r\n# call with: ansible-playbook terminate-ec2.yml\r\n# requires that you have boto installed and a ~\/.boto file installed.\r\n# the .boto file should have the following env variables: \r\n# aws_access_key_id = ABCD...\r\n# aws_secret_access_key = ZYXWVUTSR..\r\n\r\n- hosts: ec2\r\n  gather_facts: True\r\n  connection: local\r\n  vars: \r\n    - region: 'us-west-2'\r\n  tasks:\r\n    - name: Gather EC2 facts\r\n      local_action: ec2_facts\r\n\r\n    - debug: var=hostvars[inventory_hostname]\r\n    - debug: msg=\"{{ hostvars[inventory_hostname]['ec2_id'] }}\"\r\n\r\n    - name: Terminate instances \r\n      local_action: ec2\r\n        state='absent'\r\n        instance_ids='{{ ec2_id }}'\r\n        region='{{ region }}'\r\n        wait=True<\/pre>\n<p>The tricky part for me was trying to get the data from the ec2_facts module into the rest of the playbook. \u00a0It turns out that ec2_facts loads the data into the hostvars[inventory_hostname] variable. \u00a0I was looking for instance_id as the variable, but it didn&#8217;t populate this. \u00a0This may be that I have an older version of Ansible (1.2.7) that comes installed with homebrew on the mac.<\/p>\n<p>If you find this ec2_id variable doesn&#8217;t work for getting the instance ID of the AWS instance, then take a look at the debug statements to see what is populated in the hostsvars[inventory_hostname]<\/p>\n<p>I should also point out that the host group that it uses above &#8216;ec2&#8217; is from the ec2.py executable that comes with Ansible that I put in my inventory directory.<\/p>\n<p>More on that in a coming post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I spent forever trying to understand the built in Ansible ec2 modules. \u00a0What I was trying to figure out seemed simple: \u00a0How do you delete all your ec2 instances? \u00a0Nothing too clever. \u00a0Turns out it was easier to create the instances than delete it (for me anyway). \u00a0So I&#8217;m writing this down here so I&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1013,463,464,533],"tags":[530,531,534],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/2856"}],"collection":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2856"}],"version-history":[{"count":6,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/2856\/revisions"}],"predecessor-version":[{"id":2863,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/2856\/revisions\/2863"}],"wp:attachment":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}