Prepare_Workshop/create_inventory_lxc.yml

194 lines
4.8 KiB
YAML
Raw Normal View History

2020-11-28 15:12:53 +00:00
---
- hosts: all
gather_facts: no
serial: 1
vars:
# List(!) of SSH keys for authorized_keys. Set here or in group_vars/all.yml
#ssh_keys: []
2021-10-09 21:14:03 +00:00
#
# root password, default: "root"
#root_password: root
#
# Normal user account (with sudo)
#service_username: service
#service_password: {{ service_username }}
#service_ssh_keys: {{ ssh_keys }}
2020-11-28 15:12:53 +00:00
# "*cmdline_" must be listed AFTER "^cmdline_"!
2020-11-28 15:12:53 +00:00
cmdline_python:
alpine:
- "apk add -U python3"
archlinux:
- "pacman -Sy"
- "pacman -S --noconfirm python"
2020-11-28 15:12:53 +00:00
centos: &cmdline_python_centos
- "yum clean all"
- "yum makecache"
- "yum install -y python3 || true"
almalinux: *cmdline_python_centos
fedora: *cmdline_python_centos
2023-03-20 20:44:16 +00:00
oracle: *cmdline_python_centos
rockylinux: *cmdline_python_centos
2020-11-28 15:12:53 +00:00
debian: &cmdline_python_debian
- "apt-get -y update"
- "apt-get install -y python3 python3-apt"
devuan: *cmdline_python_debian
ubuntu: *cmdline_python_debian
2020-11-28 15:12:53 +00:00
opensuse:
- "zypper --gpg-auto-import-keys --no-gpg-checks -n refresh"
- "zypper --gpg-auto-import-keys --no-gpg-checks -n install python3"
voidlinux:
- "xbps-install -Suy python3 libgcc"
2023-03-20 20:44:16 +00:00
cmdline_fixes:
oracle:
8:
- '[ ! -f /usr/bin/python3 ] && ln -s /usr/libexec/platform-python /usr/bin/python3'
2020-11-28 15:12:53 +00:00
ssh_package_name:
alpine: openssh
archlinux: openssh
2020-11-28 15:12:53 +00:00
opensuse: openssh
voidlinux: openssh
ssh_config_filename:
opensuse-tumbleweed: /etc/ssh/sshd_config.d/permitrootlogin.conf
2020-11-28 15:12:53 +00:00
ssh_service_name:
devuan: ssh
user_shell:
alpine: /bin/ash
2020-11-28 15:12:53 +00:00
tasks:
- name: End for non-LXContainer
meta: end_host
when: inventory_hostname in groups.lxc_hosts
- name: Check for no-validate parameter in download template
shell: "/usr/share/lxc/templates/lxc-download --help | grep no-validate || true"
changed_when: false
register: lxc_download_validate
delegate_to: "{{ ansible_host|default('localhost') }}"
2020-11-28 15:12:53 +00:00
- name: Create LXContainer
lxc_container:
name: "{{ inventory_hostname }}"
state: started
template: download
template_options: "-a amd64 -d {{ os_d }} -r {{ os_r }} {% if 'no-validate' in lxc_download_validate.stdout %}--no-validate{% endif %}"
config: "{{ lxc_config_file|default('/etc/lxc/ansible.conf') }}"
2020-11-28 15:12:53 +00:00
register: lxc_created
2021-09-30 14:42:32 +00:00
delegate_to: "{{ ansible_host|default('localhost') }}"
2020-11-28 15:12:53 +00:00
- pause:
seconds: 10
when: lxc_created is changed
- name: Raw-Install Python
raw: "{{ item }}"
loop: "{{ cmdline_python[os_d] }}"
2023-03-20 20:44:16 +00:00
- name: OS-dependent fixes
raw: "{{ item }}"
loop: "{{ cmdline_fixes[os_d][os_r]|default([]) }}"
2020-11-28 15:12:53 +00:00
- setup:
- name: Set root password
2021-10-09 21:14:03 +00:00
shell: "echo root:{{ root_password|default('root') }} | chpasswd -c SHA256"
- name: Add SSH keys
authorized_key:
user: root
key: "{{ item }}"
loop: "{{ ssh_keys }}"
- name: "[BLOCK] when 'service_username' is set"
when: service_username is defined
block:
- name: 'Add normal user "{{ service_username }}"'
user:
name: "{{ service_username }}"
shell: "{{ user_shell[os_d]|default('/bin/bash') }}"
2021-10-09 21:14:03 +00:00
- name: 'Set password for user "{{ service_username }}"'
shell: "echo {{ service_username }}:{{ service_password|default(service_username) }} | chpasswd -c SHA256"
- name: Add SSH keys
authorized_key:
user: "{{ service_username }}"
key: "{{ item }}"
loop: "{{ ssh_keys_service|default(ssh_keys) }}"
- name: Install sudo
package:
name: sudo
- name: Add sudo line for service
lineinfile:
path: /etc/sudoers
regexp: "^service"
line: "{{ service_username }} ALL=(ALL:ALL) NOPASSWD: ALL"
backup: yes
2020-11-28 15:12:53 +00:00
- name: Install SSH
package:
name: "{{ ssh_package_name[os_d]|default('openssh-server') }}"
state: latest
- name: "Set «PermitRootLogin» to «yes»"
lineinfile:
dest: "{{ ssh_config_filename[os_d + '-' + os_r|string]|default('/etc/ssh/sshd_config') }}"
2020-11-28 15:12:53 +00:00
regexp: '^#? *PermitRootLogin'
line: "PermitRootLogin yes"
create: yes
2020-11-28 15:12:53 +00:00
backup: yes
notify: "Restart SSH"
- name: Enable SSH
service:
name: "{{ ssh_service_name[os_d]|default('sshd') }}"
enabled: yes
- name: Start SSH
service:
name: "{{ ssh_service_name[os_d]|default('sshd') }}"
state: started
ignore_errors: yes
handlers:
- name: Restart SSH
service:
name: "{{ ssh_service_name[os_d]|default('sshd') }}"
state: restarted