Working with Container Platforms
1. Managing Docker Containers
| Property | Description | Example |
|---|---|---|
| image | Docker image to use for the container | nginx:latest |
| name | Name of the container | web-server |
| state | State of the container (started, stopped, absent) | started |
| ports | Port mappings from host to container | [80:80] |
| volumes | Volume mounts from host to container | [/host/path:/container/path] |
Example: Start a Docker Container
- name: Start a Docker container
docker_container:
name: web-server
image: nginx:latest
state: started
ports:
- "80:80"
Example: Stop and Remove a Container
- name: Stop and remove a container
docker_container:
name: web-server
state: absent
Note: Ensure Docker daemon is running and Ansible has appropriate permissions to interact with the Docker API.
2. Building Docker Images
| Option | Values | Description |
|---|---|---|
| ansible.builtin.docker_image | build |
Module to build Docker images from a Dockerfile |
| ansible.builtin.docker_image | push |
Push built image to a registry |
Example: Building a Docker Image
- name: Build Docker image
ansible.builtin.docker_image:
build:
path: "/path/to/dockerfile"
tag: "myapp:latest"
push: true
registry: "https://myregistry.com"
Note: Ensure Docker daemon is running and Ansible has permissions to access Docker.
3. Managing Docker Networks
| Property | Description | Example |
|---|---|---|
| docker_network.name | Name of the Docker network to manage | ansible-net |
| docker_network.driver | Driver to use for the network (e.g., bridge, overlay) |
bridge |
| docker_network.attachable | Allow external containers to connect to the network | true |
| docker_network.internal | Prevent external access to the network | false |
| docker_network.ipv4_subnet | IPv4 subnet for the network (e.g., 172.20.0.0/16) |
172.20.0.0/16 |
| docker_network.ipv6_subnet | IPv6 subnet for the network (e.g., 2001:db8::/64) |
2001:db8::/64 |
Example: Create a Docker Network
- name: Create a Docker network
hosts: localhost
tasks:
- name: Ensure Docker network exists
community.docker.docker_network:
name: ansible-net
driver: bridge
attachable: true
internal: false
ipv4_subnet: 172.20.0.0/16
Example: Remove a Docker Network
- name: Remove a Docker network
hosts: localhost
tasks:
- name: Delete Docker network
community.docker.docker_network:
name: ansible-net
state: absent
Note: The
community.docker collection must be installed to use these modules.
4. Managing Docker Volumes
| Property | Description | Example |
|---|---|---|
| name | Name of the volume | my_volume |
| driver | Driver to use for the volume | local |
| driver_options | Options for the driver | { "o": "bind", "device": "/host/path" } |
| labels | Labels to apply to the volume | { "env": "production" } |
Example: Creating a Docker Volume
- name: Create a Docker volume
docker_volume:
name: my_volume
driver: local
driver_options:
o: bind
device: /host/path
labels:
env: production
Example: Removing a Docker Volume
- name: Remove a Docker volume
docker_volume:
name: my_volume
state: absent
Note: Volumes are managed by Docker and persist even if the container is removed.
5. Using Docker Login
| Option | Values | Notes |
|---|---|---|
| docker_login | yes |
Login to Docker registry before pulling images |
| docker_registry_url | https://index.docker.io/v1/ |
URL of the Docker registry to login to |
| docker_username | admin |
Username for Docker registry authentication |
| docker_password | secret |
Password for Docker registry authentication |
Example: Docker Login Configuration
---
- name: Login to Docker Registry
hosts: all
tasks:
- name: Ensure Docker login
docker_login:
registry_url: https://my-registry.com/v1/
username: "{{ docker_user }}"
password: "{{ docker_pass }}"
6. Managing Kubernetes Resources
| Resource Type | Module | Description |
|---|---|---|
| k8s | community.kubernetes.k8s |
Manage Kubernetes resources using the Kubernetes API |
| k8s_config | community.kubernetes.k8s_config |
Manage Kubernetes configuration files (kubeconfig) |
| k8s_auth | community.kubernetes.k8s_auth |
Authenticate to Kubernetes clusters |
Example: Deploying a Kubernetes Deployment
- name: Deploy application to Kubernetes
community.kubernetes.k8s:
state: present
src: /path/to/deployment.yaml
Example: Creating a Kubernetes Service
- name: Create Kubernetes Service
community.kubernetes.k8s:
state: present
definition:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: MyApp
ports:
- protocol: TCP
port: 80
targetPort: 9376
Note: Ensure the
kubeconfig is correctly configured or use the auth parameters to authenticate to your cluster.
Warning: Be cautious when managing resources directly with
k8s module as it can overwrite existing configurations.
Key Features
- Supports all Kubernetes resource types (Deployment, Service, ConfigMap, etc.)
- Can manage resources via YAML files or inline definitions
- Supports authentication via kubeconfig, token, or certificate
7. Using Helm Charts
| Property | Description | Example |
|---|---|---|
| chart | Name of the Helm chart to deploy | nginx/nginx-ingress |
| version | Version of the chart to deploy | 1.2.3 |
| values | Values file or inline values to override chart defaults | { "replicaCount": 2 } |
| namespace | Kubernetes namespace to deploy the chart into | production |
Example: Deploying a Helm Chart
---
- name: Deploy nginx-ingress chart
hosts: localhost
tasks:
- name: Install Helm chart
helm_chart:
chart: nginx/nginx-ingress
version: 1.2.3
values:
replicaCount: 2
namespace: production
Note: Ensure the
helm CLI is installed and configured on the control node.
Warning: Helm charts must be compatible with the target Kubernetes version.
8. Managing Pods
| Property | Description | Example |
|---|---|---|
| pod.name | Name of the pod | myapp-pod |
| pod.namespace | Kubernetes namespace | default |
| pod.spec.containers | List of containers in the pod | [{ name: 'app', image: 'nginx' }] |
| pod.spec.restartPolicy | Restart policy for the pod | Always |
Example: Create a Pod
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
namespace: default
spec:
containers:
- name: app
image: nginx
ports:
- containerPort: 80
restartPolicy: Always
Example: Scale Pod Replicas
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: app
image: nginx
Note: Pods are the smallest deployable units in Kubernetes and are managed by higher-level objects like Deployments or StatefulSets.
9. Managing Deployments
| Property | Description | Example |
|---|---|---|
| ansible.builtin.k8s | Module to manage Kubernetes resources including deployments. | |
| ansible.builtin.k8s_module | Module to manage Kubernetes resources with more granular control. | |
| ansible.builtin.docker_container | Module to manage Docker containers, including deployment lifecycle. | |
Example: Deploying a Kubernetes Deployment
---
- name: Deploy application to Kubernetes
hosts: localhost
tasks:
- name: Create deployment
ansible.builtin.k8s:
src: "deployment.yaml"
Example: Managing Docker Container Deployment
---
- name: Manage Docker container deployment
hosts: localhost
tasks:
- name: Start web server container
ansible.builtin.docker_container:
name: "web-server"
image: "nginx"
state: "started"
10. Managing ConfigMaps and Secrets
| Property | Description | Example |
|---|---|---|
| name | Name of the ConfigMap or Secret | app-config |
| data | Key-value pairs for ConfigMap data | {"config.json": "{\n \"port\": 8080\n}"} |
| stringData | String values to be stored in the ConfigMap or Secret | {"env": "production"} |
| type | Type of the Kubernetes object (e.g., Opaque, kubernetes.io/tls) |
Opaque |
Example: Creating a ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
config.json: |
{
"port": 8080,
"host": "localhost"
}
Example: Creating a Secret
apiVersion: v1
kind: Secret
metadata:
name: app-secret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
Example: Using ConfigMap in a Pod
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: app-container
image: nginx
envFrom:
- configMapRef:
name: app-config
Example: Using Secret in a Pod
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: app-container
image: nginx
envFrom:
- secretRef:
name: app-secret
Note: Secrets are base64 encoded in the Kubernetes API, but not encrypted. For sensitive data, consider using external secret management solutions like HashiCorp Vault or AWS Secrets Manager.