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

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:

Hi,

One way to do this is to append your tasks with a when statement in conjunction the special variables 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)

That’s a great idea. Thank you.

Mike

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.

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