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

26 views
Skip to first unread message

Mike Eggleston

unread,
Aug 20, 2019, 9:41:38 AM8/20/19
to Ansible Project
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:

---
- hosts: v2app
  gather_facts: yes

  vars:
    qahost: "{{ qahost }}"
    version: "{{ version }}"
    remotepath: "{{ path }}"
    localpath: "/tmp/{{version}}.tar.gz"

  tasks:
    - name: bring file locally
      command: scp "{{qahost}}":"{{remotepath}}" "{{localpath}}"
      local: true

    # localpath referes to /tmp, so using it twice means once on the local server and once on the remote app server
    - name: copy the deployment file from QA locally
      copy:src={{localpath}} dest={{localpath}}

    - name: extract the deployment file on the app server
      command: tar -xzf {{localpath}}

    # still testing, so using /tmp for all updates
    - name: make a copy of the application
      shell: rm -rf /tmp/app ; cp -r -p /opt/app /tmp

    - name: sync the new files to the application
      shell: cd /tmp/app; rsync -a {{localpath}} .

    - name: clean up
      command: rm -f {{localpath}}

(I'm just typing from memory and haven't syntax checked the above.)

How can I execute a single task of a playbook on the local server and the rest of the tasks on the remote servers? (Is this even possible?)

TIA

Mike

Auralan

unread,
Aug 20, 2019, 10:36:24 AM8/20/19
to Ansible Project
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)

Mike Eggleston

unread,
Aug 20, 2019, 10:41:52 AM8/20/19
to Ansible Project
That's a great idea. Thank you.

Mike

Karl Auer

unread,
Aug 20, 2019, 11:18:58 AM8/20/19
to ansible-project
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.


On Wed, Aug 21, 2019 at 12:41 AM Mike Eggleston <mikee...@gmail.com> wrote:
That's a great idea. Thank you.

Mike

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/40b509a6-3f65-4060-8c2f-e06e59064163%40googlegroups.com.


--
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

Zolvaring

unread,
Aug 20, 2019, 12:29:56 PM8/20/19
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages