diff --git a/.github/README.md b/.github/README.md index b40cea4..e5c3885 100644 --- a/.github/README.md +++ b/.github/README.md @@ -58,7 +58,7 @@ git clone https://github.com/Xavier2p/infra-as-code.git && cd infra-as-code ### #2 Pre-deploy configuration (using Ansible) ```bash -# coming soon... +ansible-playbook -i ansible/hosts.yml -u -b ansible/playbook.yml ``` ### #3 Deploy infrastructure (using Terraform) diff --git a/.github/.prettierrc.yml b/.prettierrc.yml similarity index 100% rename from .github/.prettierrc.yml rename to .prettierrc.yml diff --git a/ansible/clone-repo.yml b/ansible/clone-repo.yml index 0bcb594..bac820e 100644 --- a/ansible/clone-repo.yml +++ b/ansible/clone-repo.yml @@ -2,7 +2,6 @@ - name: Clone Configuration Repositories hosts: all become: true - tasks: - name: Clone Homepage Configuration Files ansible.builtin.git: @@ -12,12 +11,3 @@ clone: true update: true force: true - - - name: Clone Infra As Code Configuration Files - ansible.builtin.git: - repo: https://github.com/Xavier2p/infra-as-code.git - dest: /infra - version: main - clone: true - update: true - force: true diff --git a/ansible/hosts.yml b/ansible/hosts.yml index cf9bb08..88effba 100644 --- a/ansible/hosts.yml +++ b/ansible/hosts.yml @@ -2,3 +2,5 @@ all: children: scariff: hosts: 192.168.1.71 + eadu: + hosts: 192.168.64.5 diff --git a/ansible/init-hosts.yml b/ansible/init-hosts.yml index 8377b20..47dab54 100644 --- a/ansible/init-hosts.yml +++ b/ansible/init-hosts.yml @@ -1,8 +1,7 @@ --- -- name: Initial Config for Hosts +- name: Create Folders for Configuration hosts: all become: true - tasks: - name: Create Root Docker Folder ansible.builtin.file: @@ -10,17 +9,8 @@ state: directory mode: '0757' recurse: true - owner: sysadmin - group: sysadmin - - - name: Create Infra Folder - ansible.builtin.file: - path: /infra - state: directory - mode: '0757' - recurse: true - owner: sysadmin - group: sysadmin + owner: '{{ ansible_user }}' + group: '{{ ansible_user }}' - name: Create tree in it ansible.builtin.file: @@ -30,12 +20,11 @@ loop: - uptime-kuma - traefik - - portainer + # - portainer - homepage - adguard - homer - - dozzle - - code-server + # - code-server - grafana - prometheus # - jellyfin @@ -47,8 +36,9 @@ - name: Assign good right to Traefik ansible.builtin.file: path: /docker/traefik/acme.json - owner: sysadmin - group: sysadmin + state: touch + owner: '{{ ansible_user }}' + group: '{{ ansible_user }}' mode: '0600' - name: Create Downloads Folder @@ -57,8 +47,8 @@ state: directory mode: '0757' recurse: true - owner: sysadmin - group: sysadmin + owner: '{{ ansible_user }}' + group: '{{ ansible_user }}' - name: Create tree in it ansible.builtin.file: diff --git a/ansible/install-docker.yml b/ansible/install-docker.yml index e6da61f..8da850c 100644 --- a/ansible/install-docker.yml +++ b/ansible/install-docker.yml @@ -4,40 +4,49 @@ become: true tasks: - - name: Update apt cache - ansible.builtin.apt: - update_cache: true - - - name: Download Docker Install Script - ansible.builtin.get_url: - url: https://get.docker.com/ - dest: /tmp/install-docker.sh - mode: '755' - - - name: Run Install Script - ansible.builtin.shell: /tmp/install-docker.sh - - - name: Create Docker Service - ansible.builtin.file: - path: /etc/systemd/system/docker.service.d - state: directory - mode: '0755' - - - name: Create Docker Service Override - ansible.builtin.copy: - dest: /etc/systemd/system/docker.service.d/startup_options.conf - content: | - [Service] - ExecStart= - ExecStart=/usr/bin/dockerd -H tcp://{{ ansible_default_ipv4.address }}:2375 -H unix:///var/run/docker.sock - mode: '0644' - - - name: Reload Systemd - ansible.builtin.systemd: - daemon_reload: true - - - name: Start Docker - ansible.builtin.service: - name: docker - state: restarted - enabled: true + - name: Update apt cache + ansible.builtin.apt: + update_cache: true + + - name: Download Docker Install Script + ansible.builtin.get_url: + url: https://get.docker.com/ + dest: /tmp/install-docker.sh + mode: '755' + + - name: Run Install Script + ansible.builtin.shell: + args: + cmd: /tmp/install-docker.sh + creates: /usr/bin/docker + + - name: Create Docker Service + ansible.builtin.file: + path: /etc/systemd/system/docker.service.d + state: directory + mode: '0755' + + - name: Create Docker Service Override + ansible.builtin.copy: + dest: /etc/systemd/system/docker.service.d/startup_options.conf + content: | + [Service] + ExecStart= + ExecStart=/usr/bin/dockerd -H tcp://{{ ansible_default_ipv4.address }}:2375 -H unix:///var/run/docker.sock + mode: '0644' + + - name: Reload Systemd + ansible.builtin.systemd: + daemon_reload: true + + - name: Restart Docker + ansible.builtin.service: + name: docker + state: restarted + enabled: true + + - name: Add user to Docker group + ansible.builtin.user: + name: '{{ ansible_user }}' + groups: docker + append: true diff --git a/ansible/playbook.yml b/ansible/playbook.yml index 8521573..aafc698 100644 --- a/ansible/playbook.yml +++ b/ansible/playbook.yml @@ -1,67 +1,12 @@ --- -- name: Install Raspberry Pi - hosts: all - become: true +- name: Prepare environment + ansible.builtin.import_playbook: preprocess.yml - tasks: - # Updates the repository cache - - name: Update - ansible.builtin.apt: - update_cache: true +- name: Install Docker Playbook + ansible.builtin.import_playbook: install-docker.yml - # Upgrades all packages - - name: Upgrade - ansible.builtin.apt: - upgrade: dist +- name: Init Hosts Playbook + ansible.builtin.import_playbook: init-hosts.yml - # Installs the required packages for Docker - - name: Install APT packages - ansible.builtin.apt: - name: - - apt-transport-https - - ca-certificates - - lsb-release - - gnupg - - zsh - - vim - state: latest - update_cache: true - - # Gets the Docker signing key - - name: Add signing key - Docker - ansible.builtin.apt_key: - url: 'https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg' - state: present - - # Adds the Docker repository to the sources list - - name: Add repository into sources list - Docker - ansible.builtin.apt_repository: - repo: 'deb [arch={{ ansible_architecture }}] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable' - state: present - filename: docker - - # Installs Docker - - name: Install Docker - ansible.builtin.apt: - name: - - docker - - docker.io - - docker-compose - - docker-registry - state: latest - update_cache: true - - # Downloads the binary for Terraform - - name: Download Terraform - ansible.builtin.get_url: - url: 'https://releases.hashicorp.com/terraform/1.4.6/terraform_1.4.6_linux_arm64.zip' - dest: /tmp/terraform.zip - mode: '0755' - - # Unzips the Terraform binary to the bin directory - - name: Unzip Terraform - ansible.builtin.unarchive: - src: /tmp/terraform.zip - dest: /usr/local/bin - remote_src: true - mode: '0755' +- name: Retrieve Configuration Files + ansible.builtin.import_playbook: clone-repo.yml diff --git a/ansible/preprocess.yml b/ansible/preprocess.yml new file mode 100644 index 0000000..f43e325 --- /dev/null +++ b/ansible/preprocess.yml @@ -0,0 +1,22 @@ +--- +- name: Prepare the environment + hosts: all + become: true + tasks: + # Updates the repository cache + - name: Update + ansible.builtin.apt: + update_cache: true + + # Upgrades all packages + # - name: Upgrade + # ansible.builtin.apt: + # upgrade: dist + + # Installs the packages + - name: Install packages + ansible.builtin.apt: + name: '{{ item }}' + state: present + loop: + - htop