e152b277cf
* Use locking for concurrent file access This implements locking to be used for modules that are used for concurrent file access, like lineinfile or known_hosts. * Reinstate lock_timeout This commit includes: - New file locking infrastructure for modules - Enable timeout tests - Madifications to support concurrency with lineinfile * Rebase, update changelog and tests We need to specify ansible_python_interpreter to avoid running interpreter discovery and selecting the incorrect interpreter. Remove the import of lock in known_hosts since it is not used.
45 lines
1.1 KiB
YAML
45 lines
1.1 KiB
YAML
---
|
|
- hosts: lockhosts
|
|
gather_facts: no
|
|
vars:
|
|
lockfile: ~/ansible_testing/lock.test
|
|
tasks:
|
|
- name: Remove lockfile
|
|
file:
|
|
path: '{{ lockfile }}'
|
|
state: absent
|
|
run_once: yes
|
|
|
|
- name: Write inventory_hostname to lockfile concurrently
|
|
lineinfile:
|
|
path: '{{ lockfile }}'
|
|
line: '{{ inventory_hostname }}'
|
|
create: yes
|
|
state: present
|
|
|
|
- debug:
|
|
msg: File {{ lockfile }} has {{ lines|length }} lines for {{ ansible_play_batch|length }} instances
|
|
vars:
|
|
lines: "{{ lookup('file', lockfile).split('\n') }}"
|
|
run_once: yes
|
|
|
|
- name: Assert we get the expected number of lines
|
|
assert:
|
|
that:
|
|
- lines|length == ansible_play_batch|length
|
|
vars:
|
|
lines: "{{ lookup('file', lockfile).split('\n') }}"
|
|
run_once: yes
|
|
|
|
- name: Check lockfile for inventory_hostname entries
|
|
lineinfile:
|
|
path: '{{ lockfile }}'
|
|
line: '{{ inventory_hostname }}'
|
|
state: present
|
|
register: check_lockfile
|
|
|
|
- name: Assert locking results
|
|
assert:
|
|
that:
|
|
- check_lockfile is not changed
|
|
- check_lockfile is not failed
|