You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the docker_container module it does not seem to be possible to fail a task in a straightforward fashion like with the command module as it only returns an exit code if it is non-zero.
Tested in both macOS 10.15.4 as well as Ubuntu 18.04 (using 2.9.7-1ppa~bionic)
STEPS TO REPRODUCE
- hosts: localhostconnection: localtasks:
- name: run a command in a docker container and fail based on its exit codedocker_container:
name: "hello"image: alpineentrypoint: /bin/shcommand: -c 'exit 1'detach: nostate: startedfailed_when: hello.ansible_facts.docker_container.State.ExitCode not in [0, 1]register: hello
EXPECTED RESULTS
The expectation is for this playbook to complete successfully because 1 should be a non-failing exit code as per failed_when.
ACTUAL RESULTS
The playbook fails because ansible_facts is not populated.
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *****************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [run a command in a docker container and fail based on it's exit code] ******************************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'hello.ansible_facts.docker_container.State.ExitCode not in [0, 1]' failed. The error was: error while evaluating conditional (hello.ansible_facts.docker_container.State.ExitCode not in [0, 1]): 'dict object' has no attribute 'ansible_facts'"}
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
When I modify my playbook to use a subsequent fail task the result is the same:
(...)
- name: run a command in a docker container and fail based on its exit codedocker_container:
name: "hello"image: alpineentrypoint: /bin/shcommand: -c 'exit 1'detach: nostate: startedfailed_when: falseregister: hello
- name: to fail or not to failfail:
when: hello.ansible_facts.docker_container.State.ExitCode not in [0, 1]
Result:
PLAY [localhost] *****************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [run a command in a docker container and fail based on its exit code] *******************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [to fail or not to fail] ****************************************************************************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'hello.ansible_facts.docker_container.State.ExitCode not in [0, 1]' failed. The error was: error while evaluating conditional (hello.ansible_facts.docker_container.State.ExitCode not in [0, 1]): 'dict object' has no attribute 'ansible_facts'\n\nThe error appears to be in '/Users/x/test/bugreport.yml': line 17, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: to fail or not to fail\n ^ here\n"}
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
I've done a little bit more digging by debugging the variable:
All this digging has subsequently led me to a workaround:
- name: run a command in a docker container and fail based on its exit codedocker_container:
name: "hello"image: alpineentrypoint: /bin/shcommand: -c 'exit 1'detach: nostate: startedfailed_when: hello.status is defined and hello.status != 1register: hello
Is this expected behaviour? Shouldn't the docker_container task always return the status (also - shouldn't it be called rc like for the command module)?
Thanks!
The text was updated successfully, but these errors were encountered:
I have no idea why it was implemented this way. I've never used the module to run containers that are not detached.
I agree that it would make sense that status is always returned if detached=False, and also the inspection result.
BTW: you should NOT use ansible_facts.docker_container. That has been deprecated in Ansible 2.12 and will be removed ~next year. Register the task result as result and use reslut.docker_container instead.
SUMMARY
When using the
docker_container
module it does not seem to be possible to fail a task in a straightforward fashion like with thecommand
module as it only returns an exit code if it is non-zero.ISSUE TYPE
COMPONENT NAME
docker_container module
ANSIBLE VERSION
CONFIGURATION
OS / ENVIRONMENT
Tested in both macOS 10.15.4 as well as Ubuntu 18.04 (using
2.9.7-1ppa~bionic
)STEPS TO REPRODUCE
EXPECTED RESULTS
The expectation is for this playbook to complete successfully because 1 should be a non-failing exit code as per
failed_when
.ACTUAL RESULTS
The playbook fails because
ansible_facts
is not populated.When I modify my playbook to use a subsequent
fail
task the result is the same:Result:
I've done a little bit more digging by debugging the variable:
Note that the
status
is set to 1. When I change my docker_container command toexit 3
...So... when the container fails, the
status
field is populated in the registered variable. However when it doesn't fail, the field is not populated:All this digging has subsequently led me to a workaround:
Is this expected behaviour? Shouldn't the
docker_container
task always return the status (also - shouldn't it be calledrc
like for thecommand
module)?Thanks!
The text was updated successfully, but these errors were encountered: