-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker_execute.yml
46 lines (44 loc) · 2.03 KB
/
docker_execute.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Run a command on a docker container
#
# Examples:
# To run a Django Command on Open edX LMS container:
# ansible-playbook -i nau-data/envs/<env>/hosts.ini docker_execute.yml --limit <a server> -e "docker_container_name=openedx_lms -e docker_exec_command='python manage.py lms export_course_certificates_pdfs course-v1:FCT+XPTO+2023_T1'"
#
---
- name: Run a command inside a docker container
hosts: all
become: True
gather_facts: True
vars:
commands_to_execute:
- docker_exec_name: Command
docker_container_name: "{{ docker_container_name | default('openedx_lms') }}"
docker_exec_command: "{{ docker_exec_command }}"
docker_exec_output_fact: command_output
docker_exec_ignore_errors: "{{ docker_exec_ignore_errors | default(False) }}"
tasks:
- name: Execute command
include_role:
name: docker_execute
vars:
docker_exec_name: "{{ exec.docker_exec_name }}"
docker_container_name: "{{ exec.docker_container_name }}"
docker_exec_command: "{{ exec.docker_exec_command }}"
docker_exec_ignore_errors: "{{ exec.docker_exec_ignore_errors | default(True) }}"
docker_exec_output_fact: "{{ exec.docker_exec_output_fact | default(None) }}"
when: exec.when | default(true)
loop_control:
loop_var: exec
label: "Execute {{ exec.docker_exec_name }}"
loop: "{{ commands_to_execute }}"
# needed only because we are ignoring the errors on previous task and now we are checking if
# each has finished with success.
- name: Check if above commands have finished with success
assert:
that: hostvars[inventory_hostname][check_cmd.docker_exec_output_fact].rc == 0 and "ERROR" not in hostvars[inventory_hostname][check_cmd.docker_exec_output_fact].stdout
quiet: true
when: ( check_cmd.when | default(true) ) and not ansible_check_mode
loop_control:
loop_var: check_cmd
label: Check if {{ check_cmd.docker_exec_name }} have finished with success
loop: "{{ commands_to_execute }}"