First Commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
---
|
||||
- name: Check and Report Status of Docker Containers
|
||||
hosts: Docker
|
||||
gather_facts: yes
|
||||
vars:
|
||||
exited_containers: []
|
||||
|
||||
tasks:
|
||||
- name: Check container status
|
||||
ansible.builtin.shell: |
|
||||
docker ps -a --format "{{ '{{' }}.Names{{ '}}' }}\t{{ '{{' }}.Status{{ '}}' }}\t{{ '{{' }}.Ports{{ '}}' }}"
|
||||
register: container_status
|
||||
|
||||
- name: Collect exited containers
|
||||
set_fact:
|
||||
exited_containers: "{{ exited_containers + [item.split('\t')[0]] }}"
|
||||
loop: "{{ container_status.stdout_lines }}"
|
||||
when: "'Exited' in item.split('\t')[1]"
|
||||
|
||||
- name: Display container status for each host
|
||||
ansible.builtin.debug:
|
||||
msg: "Container: {{ item.split('\t')[0] }}, Status: {{ item.split('\t')[1] }}, Ports: {{ item.split('\t')[2] or 'None' }}"
|
||||
loop: "{{ container_status.stdout_lines }}"
|
||||
|
||||
- name: Fail with summary of exited containers
|
||||
ansible.builtin.fail:
|
||||
msg: "Exited containers found: {{ exited_containers | join(', ') }}"
|
||||
when: exited_containers | length > 0
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
- name: Update Docker Containers With New Images
|
||||
hosts: Docker
|
||||
gather_facts: yes
|
||||
|
||||
tasks:
|
||||
- name: Stop and remove Docker containers
|
||||
ansible.builtin.command: docker compose down
|
||||
args:
|
||||
chdir: "{{ compose_file_path | dirname }}"
|
||||
loop: "{{ compose_file_paths }}"
|
||||
ignore_errors: yes
|
||||
loop_control:
|
||||
loop_var: compose_file_path
|
||||
|
||||
- name: Run docker compose up
|
||||
ansible.builtin.command: docker compose up -d
|
||||
args:
|
||||
chdir: "{{ compose_file_path | dirname }}"
|
||||
loop: "{{ compose_file_paths }}"
|
||||
loop_control:
|
||||
loop_var: compose_file_path
|
||||
|
||||
- name: Pause for 60 seconds to allow containers to stabilize
|
||||
ansible.builtin.pause:
|
||||
seconds: 60
|
||||
|
||||
- name: Check container status
|
||||
ansible.builtin.shell: docker compose ps -q | xargs -n1 docker container inspect --format '{{ "{{" }} .State.Running {{ "}}" }}'
|
||||
args:
|
||||
chdir: "{{ compose_file_path | dirname }}"
|
||||
loop: "{{ compose_file_paths }}"
|
||||
register: container_status
|
||||
ignore_errors: yes
|
||||
loop_control:
|
||||
loop_var: compose_file_path
|
||||
|
||||
- name: Conditional restart of containers
|
||||
ansible.builtin.command: docker compose up -d
|
||||
args:
|
||||
chdir: "{{ result_item.item.path | dirname }}"
|
||||
loop: "{{ container_status.results }}"
|
||||
when: "'false' in result_item.stdout"
|
||||
register: restart_status
|
||||
loop_control:
|
||||
loop_var: result_item
|
||||
|
||||
- name: Pause for 60 seconds to allow containers to stabilize
|
||||
ansible.builtin.pause:
|
||||
seconds: 60
|
||||
|
||||
- import_playbook: docker_status.yml
|
||||
@@ -0,0 +1,22 @@
|
||||
- name: Build OCI Stack
|
||||
hosts: Docker
|
||||
tasks:
|
||||
- name: Clone repo
|
||||
command: wget http://192.168.1.157:3000/jeet/OCI_Build.git
|
||||
|
||||
- name: Start container using Docker Compose
|
||||
ansible.builtin.command:
|
||||
cmd: docker compose up -d
|
||||
chdir: ~/docker
|
||||
ignore_errors: yes
|
||||
|
||||
- name: Pause for 30 seconds to allow containers to stabilize
|
||||
ansible.builtin.pause:
|
||||
seconds: 60
|
||||
|
||||
- name: Check container status
|
||||
ansible.builtin.shell: docker compose ps -q | xargs -n1 docker container inspect --format '{{ "{{" }} .State.Running {{ "}}" }}'
|
||||
args:
|
||||
chdir: ~/firstContainer
|
||||
register: container_status
|
||||
ignore_errors: yes
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
- name: Install various utilities on Debian/Ubuntu
|
||||
hosts: all
|
||||
become: yes
|
||||
gather_facts: yes
|
||||
|
||||
tasks:
|
||||
- name: Update apt cache (Debian/Ubuntu)
|
||||
apt:
|
||||
update_cache: yes
|
||||
cache_valid_time: 3600 # Cache valid for 1 hour
|
||||
|
||||
- name: Install packages for Debian/Ubuntu
|
||||
apt:
|
||||
name:
|
||||
- build-essential
|
||||
- git
|
||||
- curl
|
||||
- wget
|
||||
- htop
|
||||
- tar
|
||||
- net-tools
|
||||
- unzip
|
||||
state: present
|
||||
|
||||
# This will run after the previous play is completed
|
||||
- import_playbook: install_docker.yml
|
||||
@@ -0,0 +1,59 @@
|
||||
- name: Install Docker on Debian, Ubuntu, or Raspbian
|
||||
hosts: all
|
||||
become: yes
|
||||
gather_facts: yes
|
||||
|
||||
tasks:
|
||||
- name: Install Docker dependencies for Debian/Ubuntu/Raspbian
|
||||
apt:
|
||||
name:
|
||||
- apt-transport-https
|
||||
- ca-certificates
|
||||
- curl
|
||||
- gnupg
|
||||
- lsb-release
|
||||
state: present
|
||||
update_cache: yes
|
||||
when: ansible_os_family == "Debian"
|
||||
|
||||
- name: Add Docker GPG key for Debian/Ubuntu/Raspbian
|
||||
apt_key:
|
||||
url: https://download.docker.com/linux/ubuntu/gpg
|
||||
state: present
|
||||
when: ansible_os_family == "Debian"
|
||||
|
||||
- name: Add Docker repository for Debian/Ubuntu
|
||||
apt_repository:
|
||||
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
|
||||
state: present
|
||||
update_cache: yes
|
||||
when: ansible_os_family == "Debian" and ansible_distribution == "Ubuntu"
|
||||
|
||||
- name: Add Docker repository for Raspbian/Debian
|
||||
apt_repository:
|
||||
repo: "deb [arch=arm64] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable"
|
||||
state: present
|
||||
update_cache: yes
|
||||
when: ansible_os_family == "Debian" and ansible_architecture == "aarch64"
|
||||
|
||||
- name: Install Docker for Debian/Ubuntu/Raspbian
|
||||
apt:
|
||||
name: docker-ce
|
||||
state: present
|
||||
update_cache: yes
|
||||
|
||||
- name: Add authenticated user to Docker group
|
||||
user:
|
||||
name: "{{ ansible_user }}"
|
||||
groups: docker
|
||||
append: yes
|
||||
|
||||
- name: Ensure Docker service is enabled and started
|
||||
systemd:
|
||||
name: docker
|
||||
enabled: yes
|
||||
state: started
|
||||
|
||||
- name: Reset connection to refresh user group membership
|
||||
meta: reset_connection
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
- hosts: all
|
||||
tasks:
|
||||
- name: Test Ping
|
||||
ping:
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
- hosts: all
|
||||
become: yes
|
||||
gather_facts: true
|
||||
|
||||
tasks:
|
||||
- name: "Updating and Upgrading Apt Packages"
|
||||
apt:
|
||||
update_cache: yes
|
||||
upgrade: safe
|
||||
|
||||
# Specific adjustments for Raspbian can be made here, if necessary
|
||||
# Raspbian will typically be covered by the Debian task, but if you have specific needs, you can specify them here.
|
||||
Reference in New Issue
Block a user