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.
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
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!

Further Learning
Expand your Ansible and Podman knowledge with real-life automation examples in my book Ansible By Examples:
Buy Now
Support the Project
Want to support more automation content? Consider donating: