# execute one task of a playbook locally and the rest on the remote hosts?

**URL:** <https://forum.ansible.com/t/execute-one-task-of-a-playbook-locally-and-the-rest-on-the-remote-hosts/31077>\
**Category:** Ansible Project\
**Created:** [August 20, 2019, 1:41pm UTC](https://forum.ansible.com/t/execute-one-task-of-a-playbook-locally-and-the-rest-on-the-remote-hosts/31077 "2019-08-20T13:41:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Eggleston](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Mike\_Eggleston](https://forum.ansible.com/u/Mike_Eggleston)\
**Post date:** [August 20, 2019, 1:41pm UTC](https://forum.ansible.com/t/execute-one-task-of-a-playbook-locally-and-the-rest-on-the-remote-hosts/31077/1 "2019-08-20T13:41:38Z")

</div>

I’m working on a playbook to deploy an internal application to internal hosts. This application is delivered by development to QA in a \*.tar.gz file. The file is staged and deployed to QA (by me) in that \*.tar.gz file. What I would like/am thinking of is something like:

$ ansible-playbook remote-group -k -e ‘{“qahost”:“qaserver”,“version”:2.75.26.1",“path”:“/home/app/app\_2.75.26.1.tar.gz”}’

and the playbook would be something like:

---

<div class="post-metadata">

**Author:** ![Auralan](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Auralan](https://forum.ansible.com/u/Auralan)\
**Post date:** [August 20, 2019, 2:36pm UTC](https://forum.ansible.com/t/execute-one-task-of-a-playbook-locally-and-the-rest-on-the-remote-hosts/31077/2 "2019-08-20T14:36:24Z")

</div>

Hi,

One way to do this is to append your tasks with a [when statement](https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement) in conjunction the [special variables](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html) inventory\_hostname and group\_names.

Something like this should work for you:

- name: “this only runs on localhost”  
shell: /foo  
when: (inventory\_hostname == ‘localhost’)

- name: “this runs on every host in the servers group”  
shell: /bar  
when: (‘servers’ in group\_names)

---

<div class="post-metadata">

**Author:** ![Mike\_Eggleston](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Mike\_Eggleston](https://forum.ansible.com/u/Mike_Eggleston)\
**Post date:** [August 20, 2019, 2:41pm UTC](https://forum.ansible.com/t/execute-one-task-of-a-playbook-locally-and-the-rest-on-the-remote-hosts/31077/3 "2019-08-20T14:41:51Z")

</div>

That’s a great idea. Thank you.

Mike

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [August 20, 2019, 3:18pm UTC](https://forum.ansible.com/t/execute-one-task-of-a-playbook-locally-and-the-rest-on-the-remote-hosts/31077/4 "2019-08-20T15:18:36Z")

</div>

Haven’t checked this, but I think you can have multiple sections in a playbook with different hosts. Maybe not.

Try

- hosts: localhost

…

- hosts: something\_else

…

I assume (always dangerous) that if it works at all the hosts sections would be processed in order, so you could rely on the localhost stuff happening first.

Regards, K.

---

<div class="post-metadata">

**Author:** ![Zolvaring](https://avatars.discourse-cdn.com/v4/letter/z/ce73a5/32.png) [@Zolvaring](https://forum.ansible.com/u/Zolvaring)\
**Post date:** [August 20, 2019, 4:29pm UTC](https://forum.ansible.com/t/execute-one-task-of-a-playbook-locally-and-the-rest-on-the-remote-hosts/31077/5 "2019-08-20T16:29:56Z")

</div>

I think what you’re looking for is the ‘delegate\_to’ task parameter. If you wanted the local action to only run once you can combine this with the ‘run\_once’ task parameter – for your example

- name: bring file locally  
command: scp “{{qahost}}”:“{{remotepath}}” “{{localpath}}”  
delegate\_to: localhost

run\_once: true
