Skip to content

Instantly share code, notes, and snippets.

@electrofelix
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save electrofelix/7c0a3c391bf87a1f8e7b to your computer and use it in GitHub Desktop.

Select an option

Save electrofelix/7c0a3c391bf87a1f8e7b to your computer and use it in GitHub Desktop.
Experimenting with trying to use delegate_to to farm out multiple tasks to the same remote host using ansible forks behaviour.
[vagrant]
build
[parallel]
build1
build2
build3
build4
build5
---
- name: get the hostname
command: hostname
register: hostname
- debug: msg="hello from {{ hostname.stdout }}"
- name: debug service name
debug: var=service
- hosts: localhost
tasks:
- name: setup parallel workers
add_host: name=task{{ item.0 }} groups=parallel service={{ item.1 }}
with_indexed_items:
- one
- two
- three
- four
- five
- six
- seven
- eight
- nine
- ten
- hosts: parallel
gather_facts: false
connection: local
# without this, ansible still attempts to connect to the taskX host to run the
# 'get the hostname' task even though it's been told to delegate_to the localhost
tasks:
- name: build step
delegate_to: localhost
include: include-parallel-step.yml
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [setup parallel workers] ************************************************
ok: [localhost] => (item=(0, 'one'))
ok: [localhost] => (item=(1, 'two'))
ok: [localhost] => (item=(2, 'three'))
ok: [localhost] => (item=(3, 'four'))
ok: [localhost] => (item=(4, 'five'))
ok: [localhost] => (item=(5, 'six'))
ok: [localhost] => (item=(6, 'seven'))
ok: [localhost] => (item=(7, 'eight'))
ok: [localhost] => (item=(8, 'nine'))
ok: [localhost] => (item=(9, 'ten'))
PLAY [parallel] ***************************************************************
TASK: [get the hostname] ******************************************************
fatal: [build1] => SSH Error: ssh: Could not resolve hostname build1: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [build3] => SSH Error: ssh: Could not resolve hostname build3: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [build4] => SSH Error: ssh: Could not resolve hostname build4: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [build2] => SSH Error: ssh: Could not resolve hostname build2: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [build5] => SSH Error: ssh: Could not resolve hostname build5: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task0] => SSH Error: ssh: Could not resolve hostname task0: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task3] => SSH Error: ssh: Could not resolve hostname task3: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task2] => SSH Error: ssh: Could not resolve hostname task2: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task1] => SSH Error: ssh: Could not resolve hostname task1: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task4] => SSH Error: ssh: Could not resolve hostname task4: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task7] => SSH Error: ssh: Could not resolve hostname task7: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task8] => SSH Error: ssh: Could not resolve hostname task8: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task5] => SSH Error: ssh: Could not resolve hostname task5: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task6] => SSH Error: ssh: Could not resolve hostname task6: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
fatal: [task9] => SSH Error: ssh: Could not resolve hostname task9: Name or service not known
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
get the hostname -------------------------------------------------------- 6.46s
setup parallel workers -------------------------------------------------- 0.03s
-------------------------------------------------------------------------------
Total: ------------------------------------------------------------------ 7.31s
to retry, use: --limit @xxxxxxxxxx/test-parallel2.retry
build1 : ok=0 changed=0 unreachable=1 failed=0
build2 : ok=0 changed=0 unreachable=1 failed=0
build3 : ok=0 changed=0 unreachable=1 failed=0
build4 : ok=0 changed=0 unreachable=1 failed=0
build5 : ok=0 changed=0 unreachable=1 failed=0
localhost : ok=2 changed=0 unreachable=0 failed=0
task0 : ok=0 changed=0 unreachable=1 failed=0
task1 : ok=0 changed=0 unreachable=1 failed=0
task2 : ok=0 changed=0 unreachable=1 failed=0
task3 : ok=0 changed=0 unreachable=1 failed=0
task4 : ok=0 changed=0 unreachable=1 failed=0
task5 : ok=0 changed=0 unreachable=1 failed=0
task6 : ok=0 changed=0 unreachable=1 failed=0
task7 : ok=0 changed=0 unreachable=1 failed=0
task8 : ok=0 changed=0 unreachable=1 failed=0
task9 : ok=0 changed=0 unreachable=1 failed=0
@electrofelix
Copy link
Author

Since I really just want to delegate to a vagrant 'build' host the following change to the test-parallel.yml works in my case:

- hosts: localhost
  tasks:
    - name: setup parallel hosts
      add_host:
        name: task{{ item.0 }}
        groups: parallel
        service: "{{ item.1 }}"
        ansible_ssh_host: "{{ hostvars.build.ansible_ssh_host }}"
        ansible_ssh_private_key_file: "{{ hostvars.build.ansible_ssh_private_key_file }}"
        ansible_ssh_user: "{{ hostvars.build.ansible_ssh_user }}"
        ansible_ssh_port: "{{ hostvars.build.ansible_ssh_port }}"
      with_indexed_items:
          - one
          - two
          - three
          - four
          - five
          - six
          - seven
          - eight
          - nine
          - ten

- hosts: parallel
  gather_facts: false
  tasks:
    - name: build step
      include: include-parallel-step.yml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment