使用 Ansible,如果 docker 拉动抛出 404 错误,是否有办法拉动另一个 docker 图像?
Using Ansible, is there a way to pull another docker image if the docker pull throws a 404 error?
我想实现一个解决方案,如果一个 docker 图像由于不存在而无法拉取,则拉取另一个图像。
- name: Pull an image
docker_image:
name: repository:stable
with_list: "{{ repository }}"
例如,通常,如果带有标签 stable
的图像不存在,则会抛出 404 Client Error: Not Found
,有没有办法捕获此错误并拉取 repository:latest?或者如果第一个不存在,实现拉第二个的可能解决方法?
我还使用它从存储库列表中提取许多 docker 图像。
如能就此问题提供任何帮助,我们将不胜感激。
您可以使用错误处理 block
来捕获这种情况,如果您使用 Ansible 2.1+,新的 ansible_failed_result
变量也可以更轻松地检查失败任务的结果。
如果您需要对失败采取多项任务操作,我会尝试这样的事情:
- block
- name: Pull an image
docker_image:
name: repository:stable
with_list: "{{ repository }}"
rescue:
- name: Do some etc parsing to determine the issue
set_fact:
pull_latest: '{{ "404" in ansible_failed_result }}'
- name: Pull second image
docker_image:
name: repository:latest
when: pull_latest
潜在的问题是从 block
返回的错误消息包含一个数组字典。其中每个数组对应于该执行的错误字典。
因此我无法使用 in
,因为这是在 属性 上搜索精确匹配值,但 属性 嵌套在数组中。幸运的是,父对象上还有另一个布尔值 属性 为我提供了信息,所以我改用了 failed
键。 failed
在 docker 图片拉取失败时为真。
- block:
- name: Pull an image
docker_image:
name: repository:stable
with_list: "{{ repositories }}"
loop_control:
loop_var: repository
rescue:
- name: Pull latest-dev image
docker_image:
name: repository:latest
when: ansible_failed_result.failed
with_list: "{{ repositories }}"
loop_control:
loop_var: repository
主线是ansible_failed_result.failed
,它指的是字典中的一个元素,它有Failed: true
。
我想实现一个解决方案,如果一个 docker 图像由于不存在而无法拉取,则拉取另一个图像。
- name: Pull an image
docker_image:
name: repository:stable
with_list: "{{ repository }}"
例如,通常,如果带有标签 stable
的图像不存在,则会抛出 404 Client Error: Not Found
,有没有办法捕获此错误并拉取 repository:latest?或者如果第一个不存在,实现拉第二个的可能解决方法?
我还使用它从存储库列表中提取许多 docker 图像。
如能就此问题提供任何帮助,我们将不胜感激。
您可以使用错误处理 block
来捕获这种情况,如果您使用 Ansible 2.1+,新的 ansible_failed_result
变量也可以更轻松地检查失败任务的结果。
如果您需要对失败采取多项任务操作,我会尝试这样的事情:
- block
- name: Pull an image
docker_image:
name: repository:stable
with_list: "{{ repository }}"
rescue:
- name: Do some etc parsing to determine the issue
set_fact:
pull_latest: '{{ "404" in ansible_failed_result }}'
- name: Pull second image
docker_image:
name: repository:latest
when: pull_latest
潜在的问题是从 block
返回的错误消息包含一个数组字典。其中每个数组对应于该执行的错误字典。
因此我无法使用 in
,因为这是在 属性 上搜索精确匹配值,但 属性 嵌套在数组中。幸运的是,父对象上还有另一个布尔值 属性 为我提供了信息,所以我改用了 failed
键。 failed
在 docker 图片拉取失败时为真。
- block:
- name: Pull an image
docker_image:
name: repository:stable
with_list: "{{ repositories }}"
loop_control:
loop_var: repository
rescue:
- name: Pull latest-dev image
docker_image:
name: repository:latest
when: ansible_failed_result.failed
with_list: "{{ repositories }}"
loop_control:
loop_var: repository
主线是ansible_failed_result.failed
,它指的是字典中的一个元素,它有Failed: true
。