Ansible script that will provision a set of AWS instances with Podman (Container), then run a Postgres container.

This playbook is scripted to provision a set of AWS instances with Podman.

What you can expect in this script?

The task will spin up an EC2 instance in “US-east-1” (change) region. Take a close look at the variable “ami_id”, which grabs the image, this can also be changed. The instance is added to a host group, then we need to wait for the SSH services to start. The next task will log in and download the Podman service. Then we start the services. The next task will install Postgres and start it. If no container matches the name, a container will be created and started.

- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - name: Provision a set of instances
      vars:
        ami_id: "ami-096fda3c22c1c990a"
      ec2:
        region: "us-east-1"
        key_name: some-name-aws
        group: launch-wizard-7
        instance_type: t2.micro
        assign_public_ip: yes
        vpc_subnet_id: subnet-cd5586e7
        image: "{{ ami_id }}"
        wait: true
        exact_count: 2
        count_tag:
          Name: dev_environment
        instance_tags:
          Name: dev_environment
      register: ec2
    - debug:
        msg: "System {{ ec2.instances }}"

    - name: Add new instance to host group
      add_host:
        hostname: "{{ item.public_ip }}"
        groupname: launched
      with_items: "{{ ec2.instances }}"

    - name: Wait for SSH to come up
      wait_for:
        host: "{{ item.public_ip }}"
        port: 22
        state: started
      with_items: "{{ ec2.instances }}"

- name: Configure instances
  hosts: launched
  tasks:

    - name: Download Podman-Docker
      yum:
        name: docker
        state: latest
      become: yes
      register: podman_docker_install_info
    - debug:
        msg: "Podman-Docker install info {{ podman_docker_install_info }}"

    - name: Start service Podman, if not started.
      ansible.builtin.service:
        name: podman
        state: started
        enabled: yes
      become: yes
      register: podman_service_info
    - debug:
        msg: "Podman service info {{ podman_service_info }}"

    - name: Pull image Postgres and create container
      containers.podman.podman_container:
        name: my_postgres_01
        image: postgres
        state: started # If no container matches the name, a container will be created and started. 
        volume:
          - /tmp/data
      become: yes
      become_user: ec2-user
      register: "podman_docker_container_info"
    - debug:
        msg: "Started Podman Container ID: {{ podman_docker_container_info.stdout_lines }}"

    - name: Get images and container informatoin
      ansible.builtin.shell:
        cmd: podman ps -a; podman images --all
      register: podman_info
    - debug:
        msg: "{{ podman_info.stdout_lines}}"