fix: ensure the hosts definition is always defined

Ensure that the `hosts` definition is always defined, defaulting to an
empty set. Due to the lack of an Ansible inventory file this is most
likely meant to be set as an Ansible extra variable [1].

We also rename the variable named `hosts` to `my_hosts` because `hosts`
is a reserved name in Ansible. Reserved names in Ansible are for example
all class variable names of plays, roles, blocks, and tasks [2], and
`hosts` for example is used by plays [3].

[1] https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#defining-variables-at-runtime
[2] https://github.com/ansible/ansible/blob/v2.16.4/lib/ansible/vars/reserved.py#L39
[3] https://github.com/ansible/ansible/blob/v2.16.4/lib/ansible/playbook/play.py#L58
This commit is contained in:
Christoph Schug 2024-02-23 12:19:48 +01:00
parent 4c4e302c15
commit 746aa1d06a
15 changed files with 15 additions and 15 deletions

View File

@ -1,6 +1,6 @@
---
- name: install fail2ban and configure sshd
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: install fail2ban

View File

@ -1,6 +1,6 @@
---
- name: add ssh key
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: install public keys

View File

@ -1,6 +1,6 @@
---
- name: deploy portainer-ce latest
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
become_user: "{{ lookup('env','USER') }}"
tasks:

View File

@ -1,6 +1,6 @@
---
- name: deploy traefik v2.5
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: deploy traefik

View File

@ -1,5 +1,5 @@
- name: install core packages
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: install core packages

View File

@ -1,6 +1,6 @@
---
- name: install docker
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: install docker dependencies

View File

@ -1,6 +1,6 @@
---
- name: install microk8s
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: install microk8s

View File

@ -1,6 +1,6 @@
---
- name: install core packages for virtual machines
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: install packages

View File

@ -1,6 +1,6 @@
---
- name: install wireguard
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: install wireguard

View File

@ -1,6 +1,6 @@
---
- name: install zsh
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: install zsh

View File

@ -1,6 +1,6 @@
---
- name: check disk space
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
tasks:
- name: get disk usage
ansible.builtin.command: df -h

View File

@ -1,6 +1,6 @@
---
- name: clean docker
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
tasks:
- name: prune non-dangling images
community.docker.docker_prune:

View File

@ -1,6 +1,6 @@
---
- name: check if system reboot is required
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: check if system reboot is required

View File

@ -1,6 +1,6 @@
---
- name: reboot machine
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
become: yes
tasks:
- name: reboot machine

View File

@ -11,7 +11,7 @@
- name: notify discord
hosts: "{{ hosts }}"
hosts: "{{ my_hosts | d([]) }}"
vars:
# The name that will be shown as sender of the notification. Note