zuul-jobs/roles/collect-container-logs/tasks/main.yaml
Clark Boylan 4ed66807a0 Use unique loop vars to avoid conflicts
We have to be careful about avoiding outer loop loop_var conflicts in
ansible. Because the zuul-jobs roles are meant to be reconsumed
elsewhere we should not use 'item' loopvars and instead set them to
something a bit more unique.

We use a zj_ prefix to try and be unique to this repo and document this
convention.

Change-Id: I20b9327a914890e9eafcb2b36dc8c23fb472bc8f
2020-02-04 12:23:36 -08:00

27 lines
939 B
YAML

- name: List containers
command: "{{ container_command }} ps -a --format '{{ '{{ .Names }}' }}'"
register: docker_containers
ignore_errors: true
- name: Create container log dir
file:
path: "{{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}"
state: directory
- name: Save container logs
loop: "{{ docker_containers.stdout_lines | default([]) }}"
# We can't use the default 'item' because roles may be used in
# higher level loops and 'item' could conflict in that case.
loop_control:
loop_var: zj_container_name
shell: "{{ container_command }} logs {{ zj_container_name }} &> {{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}/{{ zj_container_name }}.txt"
args:
executable: /bin/bash
ignore_errors: true
- name: Open container logs permissions
file:
dest: "{{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}"
mode: u=rwX,g=rX,o=rX
recurse: yes