Skip to content

Instantly share code, notes, and snippets.

@infernix
Created July 10, 2015 14:52
Show Gist options
  • Save infernix/d7fe285c17b22dfffdea to your computer and use it in GitHub Desktop.
Save infernix/d7fe285c17b22dfffdea to your computer and use it in GitHub Desktop.
Lineinfile race with connection: local
---
- name: Sanity check
hosts: all
connection: local
tasks:
- fail: msg="You need at least two hosts to run this play against, but more is better"
when: play_hosts|length < 2
run_once: yes
- name: Generate two tempfiles with run_once, and make a list
hosts: all
connection: local
tasks:
- shell: mktemp
register: parallel
run_once: true
- shell: mktemp
register: serial
run_once: true
- name: Write the hostname to our parallel tempfile
hosts: all
connection: local
tasks:
- lineinfile: dest={{ parallel.stdout }} line="{{ inventory_hostname }} at {{ ansible_date_time.date }} {{ ansible_date_time.time }} {{ ansible_date_time.tz }}" create=no state=present
- name: Write the hostname to our serial tempfile
hosts: all
connection: local
serial: 1
tasks:
- lineinfile: dest={{ serial.stdout }} line="{{ inventory_hostname }} at {{ ansible_date_time.date }} {{ ansible_date_time.time }} {{ ansible_date_time.tz }}" create=no state=present
- name: Cat both tempfiles and print them
hosts: all
connection: local
tasks:
- shell: cat {{ parallel.stdout }}
register: paralleldata
run_once: yes
- shell: cat {{ serial.stdout }}
register: serialdata
run_once: yes
- debug: var=paralleldata.stdout_lines
run_once: yes
- debug: var=serialdata.stdout_lines
run_once: yes
- debug: msg="Serial play has {{ serialdata.stdout_lines|length }} lines, parallel play has {{ paralleldata.stdout_lines|length }} lines"
run_once: yes
- fail: msg="Unexpected behaviour from lineinfiles module with connection local"
when: "serialdata.stdout_lines|length != paralleldata.stdout_lines|length"
run_once: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment