AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible with Podman: Manage Containers Using Inventory & Modules (Guide)

By Luca Berton · Published 2024-01-01 · Category: installation

How to use Ansible with Podman containers. Configure Podman connection plugin, dynamic inventory, and manage rootless containers with practical playbook.

Introduction

Ansible provides powerful automation capabilities for managing containerized workloads, including those running on Podman. One crucial step in automating Podman containers with Ansible is defining the inventory, which tells Ansible how to interact with managed hosts.

In this guide, we'll cover how to properly define a Podman container in Ansible inventory, using both static and dynamic inventory approaches.

See also: Ansible Builder & Execution Environments: Complete Guide (2026)

Setting Up Ansible Inventory for Podman

By default, Ansible uses SSH to connect to remote machines. However, when dealing with Podman containers, a more efficient way is to use the podman connection plugin instead of SSH.

1. Define a Static Inventory (inventory.ini)

The easiest way to specify a Podman container in the inventory is by directly listing it inside the inventory.ini file:

[podman_containers]
my_container ansible_connection=podman
my_container is the name of the running Podman container. • ansible_connection=podman tells Ansible to use the Podman connection plugin instead of SSH.

You can then run Ansible commands against the Podman container:

ansible -i inventory.ini podman_containers -m ping

2. Using a Dynamic Inventory Script

If you have multiple Podman containers and want to dynamically fetch their names and IPs, you can use a custom dynamic inventory script.

Example: Dynamic Inventory with Python

Create a script podman_inventory.py:

#!/usr/bin/env python3
import json
import subprocess

def get_podman_containers(): result = subprocess.run(["podman", "ps", "--format", "json"], capture_output=True, text=True) containers = json.loads(result.stdout)

inventory = { "podman_containers": { "hosts": [container["Names"][0] for container in containers], "vars": { "ansible_connection": "podman" } } } print(json.dumps(inventory, indent=4))

if __name__ == "__main__": get_podman_containers()

Make it executable:

chmod +x podman_inventory.py

Run it to check the output:

./podman_inventory.py

Then, use it as your inventory source:

ansible -i podman_inventory.py podman_containers -m ping

Writing an Ansible Playbook for Podman Containers

Once your inventory is ready, you can automate tasks inside the Podman containers.

Example Playbook: Managing a Web Server in Podman

---
- name: Manage Web Server in Podman Container
  hosts: podman_containers
  tasks:
    - name: Ensure Apache is installed
      ansible.builtin.yum:
        name: httpd
        state: present

- name: Ensure Apache service is running ansible.builtin.service: name: httpd state: started enabled: yes

Run the playbook:

ansible-playbook -i inventory.ini playbook.yml

See also: Ansible Execution Environments: Build Custom EEs for Enterprise Automation

Conclusion

Now you know how to configure Ansible inventory for Podman containers, both manually using inventory.ini and dynamically with a Python script. You can now automate tasks inside Podman containers efficiently!

Install Podman Collection

ansible-galaxy collection install containers.podman

See also: Ansible for Docker and Podman: Container Automation Complete Guide

Podman Connection Plugin

# inventory.yml
all:
  hosts:
    my-container:
      ansible_connection: containers.podman.podman
      ansible_podman_executable: podman

Managing Containers

Create and run a container

- name: Run nginx container
  containers.podman.podman_container:
    name: webserver
    image: docker.io/library/nginx:latest
    state: started
    ports:
      - "8080:80"
    volumes:
      - /opt/html:/usr/share/nginx/html:ro

Rootless container

- name: Run rootless container
  containers.podman.podman_container:
    name: myapp
    image: docker.io/myorg/myapp:latest
    state: started
    ports:
      - "3000:3000"
    env:
      DATABASE_URL: "postgresql://db:5432/app"
  become: false

Build image

- name: Build from Containerfile
  containers.podman.podman_image:
    name: myapp
    tag: latest
    path: /opt/myapp/
    build:
      file: Containerfile

Generate systemd service

- name: Create container with systemd service
  containers.podman.podman_container:
    name: myapp
    image: myapp:latest
    state: started
    generate_systemd:
      path: /etc/systemd/system/
      restart_policy: always
      names: true
  become: true

- name: Enable service ansible.builtin.systemd: name: container-myapp enabled: true daemon_reload: true become: true

Podman Pod Management

- name: Create a pod
  containers.podman.podman_pod:
    name: webapp-pod
    state: started
    ports:
      - "8080:80"

- name: Add containers to pod containers.podman.podman_container: name: "{{ item.name }}" image: "{{ item.image }}" pod: webapp-pod state: started loop: - { name: web, image: "nginx:latest" } - { name: db, image: "postgres:16" }

Podman vs Docker in Ansible

| Feature | Podman | Docker | |---------|--------|--------| | Collection | containers.podman | community.docker | | Daemon | Daemonless | Requires dockerd | | Rootless | Native | Requires config | | Pods | Kubernetes-style | Docker Compose |

FAQ

Can I use Docker modules with Podman?

Partially, but use the dedicated containers.podman collection for full support.

How do I pull from a private registry?

- name: Login to registry
  containers.podman.podman_login:
    registry: registry.example.com
    username: "{{ registry_user }}"
    password: "{{ registry_password }}"

Related Articles

Ansible inventory groups and variables

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home