I am writing a playbook where I need to restart my Docker containers of a particular image. I have written something like below but it is failing. Something seems to be wrong with my loop i guess.
---
- name: restart app servers
hosts: shashank-VM
connection: local
become: yes
become_method: sudo
tasks:
- name: Get info on the Container
shell: docker ps | awk '/{{ item }}/{print $1}'
register: list_of_containers
with_items:
- ubuntu
- debug:
msg: "item.item={{item.item}}, item.stdout_lines={{item.stdout_lines}}, item.changed={{item.changed}}"
loop: "{{ list_of_containers.results }}"
- name: Restart Docker Service
docker_container:
name: "{{ item.stdout_lines }}"
image: ubuntu
state: started
restart: yes
register: result_of_action
loop: "{{ list_of_containers.results }}"
Error below -
*TASK [debug] ********************************************************************************************************************************* /usr/lib/python2.7/dist-packages/requests/init.py:80: RequestsDependencyWarning: urllib3 (1.25.9) or chardet (3.0.4) doesn't match a supported version! RequestsDependencyWarning) ok: [shashank-VM] => (item={'stderr_lines': [], 'ansible_loop_var': u'item', u'end': u'2020-04-23 10:36:26.885300', u'stderr': u'', u'stdout': u'466236fa1676\n74d1c2d3effb', u'changed': True, 'failed': False, u'delta': u'0:00:00.099078', u'cmd': u"docker ps | awk '/ubuntu/{print $1}'", 'item': u'ubuntu', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'strip_empty_ends': True, u'_raw_params': u"docker ps | awk '/ubuntu/{print $1}'", u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin_add_newline': True, u'stdin': None}}, 'stdout_lines': [u'466236fa1676', u'74d1c2d3effb'], u'start': u'2020-04-23 10:36:26.786222'}) => { "msg": "item.item=ubuntu, item.stdout_lines=[u'466236fa1676', u'74d1c2d3effb'], item.changed=True" }
TASK [Restart Docker Service] **************************************************************************************************************** /usr/lib/python2.7/dist-packages/requests/init.py:80: RequestsDependencyWarning: urllib3 (1.25.9) or chardet (3.0.4) doesn't match a supported version! RequestsDependencyWarning) [WARNING]: The value ['466236fa1676', '74d1c2d3effb'] (type list) in a string field was converted to u"['466236fa1676', '74d1c2d3effb']" (type string). If this does not look like what you expect, quote the entire value to ensure it does not change. failed: [shashank-VM] (item={'stderr_lines': [], 'ansible_loop_var': u'item', u'end': u'2020-04-23 10:36:26.885300', u'stderr': u'', u'stdout': u'466236fa1676\n74d1c2d3effb', u'changed': True, 'failed': False, u'delta': u'0:00:00.099078', u'cmd': u"docker ps | awk '/ubuntu/{print $1}'", 'item': u'ubuntu', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'strip_empty_ends': True, u'_raw_params': u"docker ps | awk '/ubuntu/{print $1}'", u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin_add_newline': True, u'stdin': None}}, 'stdout_lines': [u'466236fa1676', u'74d1c2d3effb'], u'start': u'2020-04-23 10:36:26.786222'}) => {"ansible_loop_var": "item", "changed": false, "item": {"ansible_loop_var": "item", "changed": true, "cmd": "docker ps | awk '/ubuntu/{print $1}'", "delta": "0:00:00.099078", "end": "2020-04-23 10:36:26.885300", "failed": false, "invocation": {"module_args": {"_raw_params": "docker ps | awk '/ubuntu/{print $1}'", "_uses_shell": true, "argv": null, "chdir": null, "creates": null, "executable": null, "removes": null, "stdin": null, "stdin_add_newline": true, "strip_empty_ends": true, "warn": true}}, "item": "ubuntu", "rc": 0, "start": "2020-04-23 10:36:26.786222", "stderr": "", "stderr_lines": [], "stdout": "466236fa1676\n74d1c2d3effb", "stdout_lines": ["466236fa1676", "74d1c2d3effb"]}, "msg": "Error creating container: 400 Client Error: Bad Request (\"Invalid container name (['466236fa1676', '74d1c2d3effb']), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed\")"}*
How do i loop with these values from my registered variable?
"stdout_lines": [ "466236fa1676", "74d1c2d3effb" ]