first commit

This commit is contained in:
ronny 2024-11-19 10:44:58 +01:00
commit f702c9c45a
13 changed files with 309 additions and 0 deletions

38
README.md Normal file
View file

@ -0,0 +1,38 @@
Role Name
=========
A brief description of the role goes here.
Requirements
------------
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
Role Variables
--------------
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
Dependencies
------------
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
- hosts: servers
roles:
- { role: username.rolename, x: 42 }
License
-------
BSD
Author Information
------------------
An optional section for the role authors to include contact information, or a website (HTML is not allowed).

36
defaults/main.yml Normal file
View file

@ -0,0 +1,36 @@
---
# defaults file for container
containers_path: "/opt/podman"
containers_force_restart: ""
containers:
- name: "" # name
# rootless_user: "" # name of user
# rootless_userid: 5000
image: "" # image name
#privileged_ports_start: # privileged_port if needed
# - 53
# publish: # ports to publish
# - "53:53/tcp"
# - "53:53/udp"
# - "10080:80/tcp"
# expose:
# - "3000/tcp"
# env:
# TZ: 'Europe/Berlin'
# WEBPASSWORD: '{{ pihole_password }}'
# FTLCONF_REPLY_ADDR4: "{{ ansible_facts['default_ipv4']['address'] }}"
# VIRTUAL_HOST: "{{ pihole_hostname }}"
# VIRTUAL_PORT: 80
# PROXY_LOCATION: "{{ pihole_hostname }}"
# PIHOLE_DNS_: "193.17.47.1;185.43.135.1"
# DNSSEC: "true"
# dns:
# - 127.0.0.1
# - 193.17.47.1
# volume:
# - "{{ containers_path }}/pihole/etc-pihole/:/etc/pihole/"
# - "{{ containers_path }}/pihole/etc-dnsmasq.d/:/etc/dnsmasq.d/"
# network: "backend"
# restart_policy: "always"
# recreate: "true"
# state: "started"

2
handlers/main.yml Normal file
View file

@ -0,0 +1,2 @@
---
# handlers file for container

52
meta/main.yml Normal file
View file

@ -0,0 +1,52 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.1
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
#
# Provide a list of supported platforms, and for each platform a list of versions.
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
# To view available platforms and versions (or releases), visit:
# https://galaxy.ansible.com/api/v1/platforms/
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99
galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.

View file

@ -0,0 +1,60 @@
---
- name: Pull needed image for {{ item.name }} (rootless)
containers.podman.podman_image:
name: "{{ item.image }}"
force: true # there is no other way to say "newer" :-/
become: true
become_user: "{{ item.rootless_user }}"
when: not item.rootless_user == ""
register: image_pull
- name: Pull needed image for {{ item.name }}
containers.podman.podman_image:
name: "{{ item.image }}"
become: true
register: image_pull
when: image_pull is not defined
- name: Print image pull var
ansible.builtin.debug:
var: image_pull
tags:
- never
- debug
- name: Re-create container for {{ item.name }} (rootless)
containers.podman.podman_container:
name: "{{ item.name | mandatory(msg='Name of container is required.') }}"
image: "{{ item.image | mandatory(msg='Image is required.')}}"
publish: "{{ item.publish | default(omit) }}"
expose: "{{ item.expose | default(omit) }}"
env: "{{ item.env | default(omit) }}"
dns: "{{ item.dns | default(omit) }}"
volume: "{{ item.volume | default(omit) }}"
restart_policy: "{{ item.restart_policy | default('unless-stopped') }}"
recreate: "{{ item.recreate | default(omit) }}"
network: "{{ item.network | default(omit) }}"
state: "{{ item.state | default('started') }}"
when:
- image_pull is changed or not containers_force_restart == ""
- not item.rootless_user == ""
become: true
become_user: "{{ item.rootless_user }}"
- name: Re-create container
containers.podman.podman_container:
name: "{{ item.name | mandatory(msg='Name of container is required.') }}"
image: "{{ item.image | mandatory(msg='Image is required.')}}"
publish: "{{ item.publish | default(omit) }}"
expose: "{{ item.expose | default(omit) }}"
env: "{{ item.env | default(omit) }}"
dns: "{{ item.dns | default(omit) }}"
volume: "{{ item.volume | default(omit) }}"
restart_policy: "{{ item.restart_policy | default('unless-stopped') }}"
recreate: "{{ item.recreate | default(omit) }}"
network: "{{ item.network | default(omit) }}"
state: "{{ item.state | default('started') }}"
when:
- image_pull is changed or not containers_force_restart == ""
- item.rootless_user == "" or not item.rootless_user is defined
become: true

View file

@ -0,0 +1,13 @@
---
- name: Create folder structure for {{ item.name }}
ansible.builtin.file:
path: "{{ volume | split(':') | first }}"
state: directory
mode: 0755
owner: "{{ item.rootless_user }}"
group: "{{ item.rootless_user }}"
loop: "{{ item.volume }}"
loop_control:
loop_var: volume
when: not item.volume == ""
become: true

39
tasks/lingering.yml Normal file
View file

@ -0,0 +1,39 @@
---
- name: Check if user is lingering for {{ item.name }}
ansible.builtin.stat:
path: "/var/lib/systemd/linger/{{ item.rootless_user }}"
register: lingering
when: not item.rootless_user == ""
- name: Enable lingering is needed for {{ item.name }}
ansible.builtin.command:
cmd: "loginctl enable-linger {{ item.rootless_user }}"
creates: /var/lib/systemd/linger/{{ item.rootless_user }}
register: lingeringchange
failed_when: lingeringchange.rc != 0
changed_when: lingeringchange.rc == 0
when:
- not lingering.stat.exists
- not item.rootless_user == ""
- name: Configure XDG_RUNTIME_DIR for {{ item.rootless_user }} for {{ item.name }}
ansible.builtin.lineinfile:
path: "/home/{{ item.rootless_user }}/.bash_profile"
line: export XDG_RUNTIME_DIR=/run/user/$UID
owner: "{{ item.rootless_user }}"
group: "{{ item.rootless_user }}"
create: yes
when: not item.rootless_user == ""
become_user: "{{ item.rootless_user }}"
become: true
- name: Configure DBUS_SESSION_BUS_ADDRESS for {{ item.rootless_user }} user for {{ item.name }}
ansible.builtin.lineinfile:
path: "/home/{{ item.rootless_user }}/.bash_profile"
line: export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus
owner: "{{ item.rootless_user }}"
group: "{{ item.rootless_user }}"
create: yes
when: not item.rootless_user == ""
become_user: "{{ item.rootless_user }}"
become: true

21
tasks/main.yml Normal file
View file

@ -0,0 +1,21 @@
---
# tasks file for container
- name: Create users
include_tasks: user.yml
loop: "{{ containers }}"
- name: Prepare folders
include_tasks: folder_structure.yml
loop: "{{ containers }}"
- name: Network
include_tasks: network.yml
loop: "{{ containers }}"
- name: Lingering
include_tasks: lingering.yml
loop: "{{ containers }}"
- name: Deploy containers
include_tasks: create_container.yml
loop: "{{ containers }}"

28
tasks/network.yml Normal file
View file

@ -0,0 +1,28 @@
---
# TODO: This is from which port it start, so it's port -> till the end
# so if 53 is defined, then it allows all ports from 53 to end for users to use
# maybe redirect of ports would be better?
- name: Allow podman privileged ports for non root users for {{ item.name }}
ansible.posix.sysctl:
name: net.ipv4.ip_unprivileged_port_start
value: "{{ item.privileged_ports_start | int }}"
sysctl_file: /etc/sysctl.conf
sysctl_set: true
state: present
reload: true
when: item.privileged_ports_start is defined and not item.privileged_ports_start == ""
- name: Create a network (rootless)
containers.podman.podman_network:
name: "{{ item.network }}"
become: true
become_user: "{{ item.rootless_user }}"
when:
- not item.rootless_user == ""
- name: Create a network
containers.podman.podman_network:
name: "{{ item.network }}"
become: true
when:
- item.rootless_user == "" or not item.rootless_user is defined

11
tasks/user.yml Normal file
View file

@ -0,0 +1,11 @@
---
- name: Create user {{ item.rootless_user }} for {{ item.name }}
ansible.builtin.user:
name: "{{ item.rootless_user }}"
uid: "{{ item.rootless_userid | default(omit) }}"
shell: "/bin/false"
password: "!"
become: true
when: not item.rootless_user == ""
tags:
- user

2
tests/inventory Normal file
View file

@ -0,0 +1,2 @@
localhost

5
tests/test.yml Normal file
View file

@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- container

2
vars/main.yml Normal file
View file

@ -0,0 +1,2 @@
---
# vars file for container