Skip to content

Instantly share code, notes, and snippets.

@ashcrow
Created April 3, 2017 14:59
Show Gist options
  • Save ashcrow/cbd37b521018fc335d165af4c3712a70 to your computer and use it in GitHub Desktop.
Save ashcrow/cbd37b521018fc335d165af4c3712a70 to your computer and use it in GitHub Desktop.
Prototype System Container Ansible
# Copyright (C) 2016-2017 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
---
# Apply to all hosts; a custom inventory is provided for each play.
- hosts: all
name: Install System Container
serial: 1
gather_facts: no
tasks:
- name: Ensure the atomic command is available
shell: "which atomic"
register: atomic_bin
ignore_errors: True
- assert:
that: atomic_bin.rc == 0
msg: "The atomic command is required for System Containers"
- set_fact:
container_suffix: 0
# The following would be passed into the playbook
container_name: mine
image_name: fedora
image_tag: latest
# Check to see if a 0 or 1 container is there so we can set variables accordingly
- name: Check if a 0 container is present
command: "systemctl cat {{ container_name }}-0"
register: container_0_present
ignore_errors: True
- name: Check if a 1 container is present
command: "systemctl cat {{ container_name }}-1"
register: container_1_present
ignore_errors: True
- set_fact:
container_suffix: 0
old_container: "{{ container_name }}-1"
when:
- container_0_present.rc != 0
- container_1_present.rc == 0
- set_fact:
container_suffix: 1
old_container: "{{ container_name }}-0"
when:
- container_0_present.rc == 0
- container_1_present.rc != 0
# Create the full name for easier usage
- set_fact:
container_full_name: "{{ container_name }}-{{ container_suffix }}"
- name: Install the System Container
command: "atomic install --name {{ container_full_name }} --system {{ image_name }}:{{ image_tag }}"
register: container_install
ignore_errors: True
- assert:
that: container_install.rc == 0
msg: "The container failed to install: stdout={{ container_install.stdout }},stderr={{ container_install.stderr }}"
- name: Check container service
command: "systemctl status {{ container_full_name }}"
register: container_status
ignore_errors: True
- name: Ensure the container is running
assert:
that: container_status.rc == 0
msg: "The container did not start: stdout={{ container_status.stdout }},stderr={{ container_status.stderr }}"
- name: Uninstall the old container
command: atomic uninstall {{ old_container }}
when: old_container is defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment