# Execute local task pre/pos ansible execution

**URL:** <https://forum.ansible.com/t/execute-local-task-pre-pos-ansible-execution/11847>\
**Category:** Ansible Project\
**Created:** [August 31, 2012, 10:21am UTC](https://forum.ansible.com/t/execute-local-task-pre-pos-ansible-execution/11847 "2012-08-31T10:21:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Vito\_Moscarito](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Marco\_Vito\_Moscarito](https://forum.ansible.com/u/Marco_Vito_Moscarito)\
**Post date:** [August 31, 2012, 10:21am UTC](https://forum.ansible.com/t/execute-local-task-pre-pos-ansible-execution/11847/1 "2012-08-31T10:21:25Z")

</div>

Hello all,  
&nbsp;&nbsp;as first requeest into this ml i need to thank Michel to writing  
ansible and all other people that help this project with articles,  
docs, code, patches, tests, .. 🙂

In my test i need to do some operation on machine before and after the  
single ansible request start.

The tipical use case is:

\* Disconnect machine from load balancer  
\* Execute maintenance tasks  
\* Connect machine to load balancer

I start to see the local\_action to use local machine tools (eg. clb¹,  
not available on nodes) to connect/disconnect the current machine from  
LB, but I need more control on stop next macihine execution on error,  
or do not disconnect more then one machine at time, ...

I'm just start to planning the work, so if someone have suggestion or  
similar request and have resolved, or can me put on the right way.. 😉

Thank you!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Marco

¹ clb: [https://github.com/calebgroom/clb](https://github.com/calebgroom/clb)

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [August 31, 2012, 10:36am UTC](https://forum.ansible.com/t/execute-local-task-pre-pos-ansible-execution/11847/2 "2012-08-31T10:36:41Z")

</div>

Marco,

> In my test i need to do some operation on machine before and after the  
> single ansible request start.

Maybe a simple shell script?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do stuff before  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# run Ansible  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible-playbook .....

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do stuff after?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

That's how I do it. Simple but effective. 🙂

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Marco\_Vito\_Moscarito](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Marco\_Vito\_Moscarito](https://forum.ansible.com/u/Marco_Vito_Moscarito)\
**Post date:** [August 31, 2012, 10:46am UTC](https://forum.ansible.com/t/execute-local-task-pre-pos-ansible-execution/11847/3 "2012-08-31T10:46:36Z")

</div>

> Marco,

Hello Jan-Piet,  
&nbsp;&nbsp;thank for reply.

> > In my test i need to do some operation on machine before and after the  
> > single ansible request start.
> 
> Maybe a simple shell script?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do stuff before  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# run Ansible  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible-playbook .....
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do stuff after?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
> 
> That's how I do it. Simple but effective. 🙂

Let me expand my example.

I have hosts:

frontend:  
&nbsp;&nbsp;192.168.0.1  
&nbsp;&nbsp;192.168.0.2  
&nbsp;&nbsp;192.168.0.3  
&nbsp;&nbsp;192.168.0.4  
&nbsp;&nbsp;192.168.0.5

database:  
&nbsp;&nbsp;192.168.1.1  
&nbsp;&nbsp;192.168.1.2

I need to execute:

ansible frontend -m application

this require to update my application on server, restart webserver and  
make some other operation on each node.

This operation can be "problematic" and require long time (some mins),  
so i need to:

\* detach machine 192.168.0.1 from LB  
\* execute ansible task on machine 192.168.0.1  
\* attach machine 192.168.0.1 to LB

\* detach machine 192.168.0.2 from LB  
\* execute ansible task on machine 192.168.0.2  
\* attach machine 192.168.0.2 to LB

\* ....

to make ansible executing on a single machine I can use --forks 1 to  
make sure a single machine will be detach from LB and run ansible  
operation on it, but i need to execute detac / attach operation.

I can also use -of course- bash script but I think is not the best way  
to use ansible.. or not?

Bye  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Marco

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [August 31, 2012, 11:08am UTC](https://forum.ansible.com/t/execute-local-task-pre-pos-ansible-execution/11847/4 "2012-08-31T11:08:33Z")

</div>

Marco Vito Moscaritolo wrote:

> > Marco,
> 
> Hello Jan-Piet,  
> &nbsp;&nbsp;thank for reply.
> 
> > > In my test i need to do some operation on machine before and after the  
> > > single ansible request start.
> > 
> > Maybe a simple shell script?
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do stuff before  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# run Ansible  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible-playbook .....
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do stuff after?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
> > 
> > That's how I do it. Simple but effective. 🙂
> 
> Let me expand my example.
> 
> I have hosts:
> 
> frontend:  
> &nbsp;&nbsp;192.168.0.1  
> &nbsp;&nbsp;192.168.0.2  
> &nbsp;&nbsp;192.168.0.3  
> &nbsp;&nbsp;192.168.0.4  
> &nbsp;&nbsp;192.168.0.5
> 
> database:  
> &nbsp;&nbsp;192.168.1.1  
> &nbsp;&nbsp;192.168.1.2
> 
> I need to execute:
> 
> ansible frontend -m application
> 
> this require to update my application on server, restart webserver and  
> make some other operation on each node.
> 
> This operation can be "problematic" and require long time (some mins),  
> so i need to:
> 
> \* detach machine 192.168.0.1 from LB  
> \* execute ansible task on machine 192.168.0.1  
> \* attach machine 192.168.0.1 to LB
> 
> \* detach machine 192.168.0.2 from LB  
> \* execute ansible task on machine 192.168.0.2  
> \* attach machine 192.168.0.2 to LB
> 
> \* ....
> 
> to make ansible executing on a single machine I can use --forks 1 to  
> make sure a single machine will be detach from LB and run ansible  
> operation on it, but i need to execute detac / attach operation.
> 
> I can also use -of course- bash script but I think is not the best way  
> to use ansible.. or not?

It sounds like a playbook with something like:  
- hosts: frontend  
&nbsp;&nbsp;serial: 1  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: detach from load balancer  
&nbsp;&nbsp;&nbsp;&nbsp;local\_action: ...  
&nbsp;&nbsp;- name: deploy application  
&nbsp;&nbsp;&nbsp;&nbsp;action: ...  
&nbsp;&nbsp;- name: attach to load balancer  
&nbsp;&nbsp;&nbsp;&nbsp;local\_action: ...

is what you want. serial: 1 will run the entire play on 1 host before  
continuing with the next one, ensuring only one of your nodes is out of  
the load balancer at any given time, and using local\_action to handle the  
load balancer.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [August 31, 2012, 11:50am UTC](https://forum.ansible.com/t/execute-local-task-pre-pos-ansible-execution/11847/5 "2012-08-31T11:50:51Z")

</div>

> It sounds like a playbook with something like:  
> - hosts: frontend  
> &nbsp;&nbsp;serial: 1  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name: detach from load balancer  
> &nbsp;&nbsp;&nbsp;&nbsp;local\_action: ...  
> &nbsp;&nbsp;- name: deploy application  
> &nbsp;&nbsp;&nbsp;&nbsp;action: ...  
> &nbsp;&nbsp;- name: attach to load balancer  
> &nbsp;&nbsp;&nbsp;&nbsp;local\_action: ...
> 
> is what you want. serial: 1 will run the entire play on 1 host before  
> continuing with the next one, ensuring only one of your nodes is out of  
> the load balancer at any given time, and using local\_action to handle the  
> load balancer.
> 
> --  
> Daniel Hokka Zakrisson

Daniel nailed it exactly.
