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.
63 lines
1.5 KiB
YAML
63 lines
1.5 KiB
YAML
---
|
|
- hosts: lockhost00
|
|
vars:
|
|
lockfile: ~/ansible_testing/lock_timeout.test
|
|
gather_facts: no
|
|
tasks:
|
|
- name: Remove lockfile
|
|
file:
|
|
path: '{{ lockfile }}'
|
|
state: absent
|
|
run_once: yes
|
|
|
|
- name: Create lockfile
|
|
lineinfile:
|
|
line: '{{ inventory_hostname }}'
|
|
path: '{{ lockfile }}'
|
|
state: present
|
|
create: yes
|
|
|
|
- name: Lock lockfile with lockf and sleep 20s
|
|
command: python
|
|
args:
|
|
stdin: |
|
|
import time
|
|
from ansible.module_utils.common.file import open_locked
|
|
with open_locked('{{ lockfile | expanduser }}') as fd:
|
|
time.sleep(20)
|
|
async: 60
|
|
poll: 0
|
|
register: flock_waiter
|
|
|
|
- name: Remove inventory_hostname line from lockfile
|
|
lineinfile:
|
|
path: '{{ lockfile }}'
|
|
line: '{{ inventory_hostname }}'
|
|
state: absent
|
|
ignore_errors: yes
|
|
register: rm_line
|
|
|
|
- name: Assert that removal of inventory_hostname from lockfile failed
|
|
assert:
|
|
that:
|
|
- rm_line is failed
|
|
|
|
- name: Wait for flock job to finish
|
|
async_status:
|
|
jid: '{{ flock_waiter.ansible_job_id }}'
|
|
register: job_result
|
|
until: job_result.finished
|
|
retries: 30
|
|
|
|
- name: Inventory_hostname in lockfile
|
|
lineinfile:
|
|
path: '{{ lockfile }}'
|
|
line: '{{ inventory_hostname }}'
|
|
state: present
|
|
register: check_line
|
|
|
|
- name: Assert that lockfile is unchanged
|
|
assert:
|
|
that:
|
|
- check_line is not changed
|
|
- check_line is not failed
|