Ansible树莓派Docker主机补丁剧本:容器数对比任务异常失败排查
问题描述
我正在编写Ansible剧本,为树莓派Docker主机执行补丁操作,步骤如下:
- 更新apt缓存
- 获取运行中容器
- 显示主机名及运行容器数量
- 停止容器
- 更新软件包
- 重启主机
- 再次获取运行中容器
- 显示主机名及运行容器数量
- 对比重启前后的容器数,不匹配则任务失败
现在第9步存在问题:即使重启前后容器数均为5,对比任务仍会触发失败。以下是我的main.yml代码及错误输出:
--- - name: Patching Raspberry pi hosts: all become: true gather_facts: true # vars_files: # ./vars/vars.yml pre_tasks: - name: Update Apt Cache ansible.builtin.apt: update_cache: true cache_valid_time: 3600 - name: Get running containers community.docker.docker_host_info: containers: true register: docker_info - name: Show Number of running containers ansible.builtin.debug: msg: "Hostname - {{ docker_info.host_info.Name }} has {{ docker_info.host_info.ContainersRunning }} Containers running" register: docker_pre_reboot - name: Stop running containers community.docker.docker_container: name: "{{ item }}" state: stopped loop: "{{ docker_info.containers | map(attribute='Id') | list }}" tasks: - name: Update all packages ansible.builtin.apt: name: "*" state: latest - name: Reboot ansible.builtin.reboot: - name: Get running containers community.docker.docker_host_info: containers: true register: docker_info # - name: Check Containers are running after reboot # ansible.builtin.debug: # msg: "Hostname - {{ docker_info.host_info.Name }} has {{ docker_info.host_info.ContainersRunning }} Containers running" # failed_when: docker_info.host_info.ContainersRunning != docker_pre_reboot.host_info.ContainersRunning " - name: Compare Pre - Post Containers ansible.builtin.debug: msg: "Pre-reboot Containers {{ docker_pre_reboot.host_info.ContainersRunning }} - Post-reboot Containers {{ docker_info.host_info.ContainersRunning }}" failed_when: docker_info.host_info.ContainersRunning != docker_pre_reboot.host_info.ContainersRunning
错误输出:
TASK [Compare values] ************************************************************************************************************************************************* fatal: [pi-dev]: FAILED! => { "msg": "Pre-reboot Containers 5 - Post-reboot Containers 5" }
问题原因及修复
问题核心在于docker_pre_reboot的注册逻辑错误:你用debug模块注册的变量,并不包含host_info字段——debug模块的注册结果是自身的输出结构,而非docker_host_info的返回值。
正确的做法是直接从docker_host_info的注册结果中提取容器数,用set_fact保存为独立变量,避免依赖debug模块的无效注册数据。
修复后的剧本示例
--- - name: Patching Raspberry pi hosts: all become: true gather_facts: true pre_tasks: - name: Update Apt Cache ansible.builtin.apt: update_cache: true cache_valid_time: 3600 - name: Get running containers (pre-reboot) community.docker.docker_host_info: containers: true register: docker_pre_reboot_info - name: Save pre-reboot container count ansible.builtin.set_fact: pre_reboot_container_count: "{{ docker_pre_reboot_info.host_info.ContainersRunning }}" - name: Show pre-reboot container count ansible.builtin.debug: msg: "Hostname - {{ docker_pre_reboot_info.host_info.Name }} has {{ pre_reboot_container_count }} Containers running" - name: Stop running containers community.docker.docker_container: name: "{{ item }}" state: stopped loop: "{{ docker_pre_reboot_info.containers | map(attribute='Id') | list }}" tasks: - name: Update all packages ansible.builtin.apt: name: "*" state: latest - name: Reboot ansible.builtin.reboot: - name: Get running containers (post-reboot) community.docker.docker_host_info: containers: true register: docker_post_reboot_info - name: Show post-reboot container count ansible.builtin.debug: msg: "Hostname - {{ docker_post_reboot_info.host_info.Name }} has {{ docker_post_reboot_info.host_info.ContainersRunning }} Containers running" - name: Compare Pre - Post Containers ansible.builtin.debug: msg: "Pre-reboot Containers {{ pre_reboot_container_count }} - Post-reboot Containers {{ docker_post_reboot_info.host_info.ContainersRunning }}" failed_when: docker_post_reboot_info.host_info.ContainersRunning != pre_reboot_container_count
关键修改点
- 用
set_fact将预重启容器数保存为独立变量pre_reboot_container_count,确保取值直接来自docker_host_info的有效返回 - 预重启和重启后的Docker信息使用不同变量名(
docker_pre_reboot_info、docker_post_reboot_info),避免变量覆盖 - 对比逻辑直接使用有效字段,确保数值类型和取值的准确性
修改后即可正常对比重启前后的容器数量,不会出现数值相等但任务失败的情况。
内容的提问来源于stack exchange,提问作者0lzi
相关产品推荐
相关产品推荐

