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

78 views
Skip to first unread message

Tuyen Nguyen

unread,
May 24, 2019, 1:32:35 PM5/24/19
to Ansible Project
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?


Barun kumar

unread,
May 24, 2019, 2:16:09 PM5/24/19
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/77cb5f2c-1277-4042-b43f-14bb40f3c10a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tuyen Nguyen

unread,
May 25, 2019, 12:39:01 AM5/25/19
to Ansible Project
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: <ipaddress>
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?


On Friday, May 24, 2019 at 2:16:09 PM UTC-4, Barun kumar wrote:
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.




On Fri, 24 May 2019, 23:02 Tuyen Nguyen, <daica...@gmail.com> wrote:
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 received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Tuyen Nguyen

unread,
May 25, 2019, 1:43:41 AM5/25/19
to Ansible Project
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.



On Friday, May 24, 2019 at 2:16:09 PM UTC-4, Barun kumar wrote:
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.




On Fri, 24 May 2019, 23:02 Tuyen Nguyen, <daica...@gmail.com> wrote:
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 received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Barun kumar

unread,
May 25, 2019, 2:10:13 AM5/25/19
to ansible...@googlegroups.com
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. 


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.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Karl Auer

unread,
May 25, 2019, 2:28:22 AM5/25/19
to ansible-project
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.



For more options, visit https://groups.google.com/d/optout.


--
Karl Auer

Email  : ka...@2pisoftware.com
Website: http://2pisoftware.com


GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
Previous:
958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816

Tuyen Nguyen

unread,
Jun 3, 2019, 11:46:41 AM6/3/19
to Ansible Project
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.

Tuyen Nguyen

unread,
Jun 3, 2019, 11:49:14 AM6/3/19
to Ansible Project
Hi

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


On Saturday, May 25, 2019 at 2:28:22 AM UTC-4, Karl Auer wrote:
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.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages