You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Ansible检查主机是否已安装docker-compose

Checking for docker-compose Installation in Ansible

Great question! When you're building an Ansible playbook to manage docker-compose workflows, verifying if it's already present on your target hosts is a smart first step—no need to waste time reinstalling it unnecessarily. Here are a couple of reliable approaches to check, plus a complete playbook example that combines checking, installation, and running your services.

Method 1: Check via docker-compose --version

The most straightforward way is to run the version command and capture the result. If docker-compose is installed, this will return a version string; if not, it'll throw an error which we can safely ignore for the check.

- name: Check if docker-compose is installed
  command: docker-compose --version
  register: docker_compose_check
  ignore_errors: true  # Don't fail the play if it's not installed
  changed_when: false  # This is a read-only check, no state changes

You can then use the return code (rc) to determine the status:

  • docker_compose_check.rc == 0: docker-compose is installed
  • docker_compose_check.rc != 0: docker-compose is missing

Method 2: Check for the Executable File

If you want to verify the physical presence of the docker-compose binary (common paths are /usr/local/bin/docker-compose or /usr/bin/docker-compose), use the stat module:

- name: Check if docker-compose executable exists
  stat:
    path: /usr/local/bin/docker-compose
  register: docker_compose_stat

Use these variables in your conditions:

  • docker_compose_stat.stat.exists: True if the file exists
  • docker_compose_stat.stat.executable: True if the file has execution permissions (an extra sanity check)

Full Playbook Example: Check, Install, and Run docker-compose

Here's how to tie it all together into a single playbook that checks for docker-compose, installs it if missing, then runs your services:

---
- name: Manage docker-compose setup and services
  hosts: your_target_hosts
  become: true  # Need root privileges for installation
  tasks:
    # Step 1: Check if docker-compose is installed
    - name: Check docker-compose installation status
      command: docker-compose --version
      register: docker_compose_check
      ignore_errors: true
      changed_when: false

    # Step 2: Install docker-compose if not present
    - name: Install docker-compose
      get_url:
        # Replace with the latest version URL for your architecture
        url: "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-Linux-x86_64"
        dest: /usr/local/bin/docker-compose
        mode: '0755'  # Set executable permissions
      when: docker_compose_check.rc != 0

    # Optional: Verify installation succeeded
    - name: Confirm docker-compose version
      command: docker-compose --version
      register: docker_compose_version
      changed_when: false

    - name: Print installed version
      debug:
        msg: "docker-compose is ready! Version: {{ docker_compose_version.stdout }}"

    # Step 3: Run your docker-compose services
    - name: Start docker-compose services
      docker_compose:
        project_src: /path/to/your/docker-compose/directory
        state: present  # Starts services; use 'restarted' to force a restart

Notes:

  • For ARM64 systems, replace docker-compose-Linux-x86_64 in the URL with docker-compose-Linux-aarch64.
  • Always check the official docker-compose releases page for the latest version number to use in the download URL.
  • The docker_compose module requires the docker-compose Python package on the control node—you can install it with pip install docker-compose if needed.

内容的提问来源于stack exchange,提问作者avsr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:33:10