This playbook deploys Fabric (Python 3) in Docker using Ansible. Suitable for CI/CD pipelines or disposable deployment nodes.
---
- name: Deploy Fabric in Docker
hosts: fabric_docker
become: true
vars:
fabric_workspace: /opt/fabric
fabric_version: "3.2.3"
python_version: "3.11"
tasks:
- name: Ensure Docker is installed
package:
name: docker.io
state: present
- name: Enable and start Docker service
systemd:
name: docker
state: started
enabled: true
- name: Create Fabric workspace directory
file:
path: "{{ fabric_workspace }}"
state: directory
mode: '0755'
- name: Create Docker Compose file for Fabric
copy:
dest: "{{ fabric_workspace }}/docker-compose.yml"
content: |
version: '3.8'
services:
fabric:
image: python:{{ python_version }}-slim
container_name: fabric-deploy
working_dir: /workspace
volumes:
- ./projects:/workspace
- $HOME/.ssh:/root/.ssh:ro
entrypoint: ["bash", "-c", "pip install fabric=={{ fabric_version }} && fab --version"]
networks:
- deploy-network
restart: "no"
networks:
deploy-network:
driver: bridge
mode: '0644'
- name: Create projects directory
file:
path: "{{ fabric_workspace }}/projects"
state: directory
mode: '0755'
- name: Deploy Fabric container
community.docker.docker_compose_v2:
project_src: "{{ fabric_workspace }}"
state: present
- name: Verify Fabric installation
command: docker run --rm python:{{ python_version }}-slim bash -c "pip install fabric=={{ fabric_version }} && fab --version"
register: fab_version
changed_when: false
- name: Display Fabric version
debug:
msg: "Fabric version: {{ fab_version.stdout_lines | last }}"
ansible-playbook -i inventory.ini fabric-docker.yml