playbook to start AWS instance, run some tasks on the AWS instance, then terminate the instance

Hi

Currently, to do what I want to do above, I have 3 different playbooks running with a shell script.

First the script runs a playbook aws.yml to start up an instance with a static IP address using the ec2 module, and I have it record the instance id to a local file

Next, the script runs a normal playbook task to connect to the AWS instance using the static IP address.

Lastly, after the tasks are complete on that AWS instance, the script calls a last playbook to terminate AWS instance, and I pass the instance id as a variable using --extra-vars

How can I create a single playbook that would run the normal playbook task against the AWS instance?

You can call facts variable in playbook ,
1- playbook to create a instace
2- register instance instance creation facts and call IP to normal tasks adding variable in same play then call terminate playbook below.

I have given you logic, more thought process you need to apply while you create a playbook taks.

Hi

I thought variables cannot go across different plays in a playbook ?

I know how to create a playbook to start up an AWS instance, and register the instance_id of the new instance, and then use the ec2 module again to terminate the instance_id.

Initially to start up the AWS instance, I need to run the play with hosts: localhost, and I register the instance_id as a variable in Ansible

If I do
hosts:
after that, and I can run the task against the client on the IP address,

but then next, if I do hosts: localhost again to use the ec2 module to terminate the instance, would I be able to call on the instance_id variable?

Hi

Thank you for your response. I didn’t realize that the variable stays persistent when the host the play is running against is the same. So when I registered the instance type as a variable to localhost, then I changed the hosts to run the tasks against the remote host, and then when I go back to hosts: localhost, I am able to use the variable I used to hold the instance_id, and in that way I can do what I wanted.

There will be way to do it, but let me tell you that ansible is mainly used for configuration and management not or orchestration, it has module limitations. If you want to do infrastructure orchestration and for AWS specially then I would request you to go for terraform for infrastructure provisioning, it has many feature and easy to use even you can call ansible playbooks to perform post build taks.

https://www.terraform.io/intro/index.html

Hope this will help you, I know u had asked for ansible and I am telling you to use terraform, once you use it, will love it. Try it.

Or look into AWS Batch. It’s also fantastic for parallelisable tasks or many identical tasks. Lambda for tiny short-lived tasks, Batch for larger or long-lived tasks. Takes a bit of setup, but well worth it.

Regards, K.

Thank you. I will look into this. Right now we are just doing some testing so what I was able to do with Ansible is sufficient.

Hi

Thank you. I wasn’t aware AWS had these tools as well.