From c235d1ebd2778344f464228f832993c7d8f0cf3e Mon Sep 17 00:00:00 2001 From: Yurii Pavlov Date: Thu, 7 Mar 2024 14:48:08 +0100 Subject: [PATCH] update ansible configuration --- Makefile | 2 +- iac/ansible/inventory.ini | 11 ---- iac/ansible/inventory.yml | 28 ++++++++++ iac/ansible/partials/setup-swap.yml | 44 +++++++++------ iac/ansible/playbook-alpine.yml | 82 ---------------------------- iac/ansible/playbook-debian.yml | 83 ----------------------------- 6 files changed, 57 insertions(+), 193 deletions(-) delete mode 100644 iac/ansible/inventory.ini create mode 100644 iac/ansible/inventory.yml delete mode 100644 iac/ansible/playbook-alpine.yml delete mode 100644 iac/ansible/playbook-debian.yml diff --git a/Makefile b/Makefile index 7d67d7f..37f811b 100644 --- a/Makefile +++ b/Makefile @@ -131,7 +131,7 @@ terraform: terraform -chdir=iac/terraform $(PARAMS) ansible: - ansible-playbook -i iac/ansible/inventory.ini iac/ansible/playbook.yml $(PARAMS) + ansible-playbook -i iac/ansible/inventory.yml iac/ansible/playbook.yml $(PARAMS) # docker build|docker push|docker clean docker: diff --git a/iac/ansible/inventory.ini b/iac/ansible/inventory.ini deleted file mode 100644 index 7c42dbc..0000000 --- a/iac/ansible/inventory.ini +++ /dev/null @@ -1,11 +0,0 @@ -[development] -develop.starter-kit.io ansible_user=ubuntu - -[development2] -develop2.starter-kit.io ansible_user=admin - -[staging] -staging.starter-kit.io ansible_user=ubuntu - -[production] -prod.starter-kit.io ansible_user=ubuntu diff --git a/iac/ansible/inventory.yml b/iac/ansible/inventory.yml new file mode 100644 index 0000000..1aab9ab --- /dev/null +++ b/iac/ansible/inventory.yml @@ -0,0 +1,28 @@ +all: + children: + development: + hosts: + develop.starter-kit.io: + os: ubuntu + ansible_user: ubuntu + + development2: + hosts: + develop2.starter-kit.io: + os: debian + ansible_user: admin + swap_vars: + size: 2G + swappiness: 20 + + staging: + hosts: + staging.starter-kit.io: + os: ubuntu + ansible_user: ubuntu + + production: + hosts: + prod.starter-kit.io: + os: ubuntu + ansible_user: ubuntu diff --git a/iac/ansible/partials/setup-swap.yml b/iac/ansible/partials/setup-swap.yml index 04e9325..46ec58b 100644 --- a/iac/ansible/partials/setup-swap.yml +++ b/iac/ansible/partials/setup-swap.yml @@ -1,37 +1,49 @@ # Setup swap - - name: Check if swap is already enabled - shell: swapon --show | grep -q "^" - register: swap_exists - changed_when: false - ignore_errors: true + - name: Gather system facts + setup: + # Ensures Ansible facts are up to date. + + - name: Set fact for swap existence based on Ansible facts + set_fact: + swap_exists: "{{ ansible_swaptotal_mb > 0 }}" + # Checks if swap space is already enabled. - name: Create swap file - command: fallocate -l 1G /swapfile - when: swap_exists.rc != 0 - become: yes + command: fallocate -l "{{ swap_vars.size }}" /swapfile + when: not swap_exists + # Creates a swap file with the specified size from your main playbook's vars. - name: Set swap file permissions file: path: /swapfile mode: '0600' - when: swap_exists.rc != 0 - become: yes + when: not swap_exists + # Sets appropriate permissions for the swap file. - name: Set up swap space command: mkswap /swapfile - when: swap_exists.rc != 0 - become: yes + when: not swap_exists + # Initializes the swap file. - name: Enable swap command: swapon /swapfile - when: swap_exists.rc != 0 - become: yes + when: not swap_exists + # Activates the swap file. - name: Add swap to fstab blockinfile: path: /etc/fstab block: "/swapfile none swap sw 0 0" marker: "# {mark} ANSIBLE MANAGED BLOCK" - when: swap_exists.rc != 0 - become: yes + when: not swap_exists + # Ensures the swap setup persists across reboots. + + - name: Adjust swappiness + sysctl: + name: vm.swappiness + value: "{{ swap_vars.swappiness }}" + state: present + reload: yes + # This task is optional and adjusts the system's swappiness parameter. + diff --git a/iac/ansible/playbook-alpine.yml b/iac/ansible/playbook-alpine.yml deleted file mode 100644 index be76faa..0000000 --- a/iac/ansible/playbook-alpine.yml +++ /dev/null @@ -1,82 +0,0 @@ -- name: Install Docker with Docker Compose plugin on Alpine Server - hosts: - - development2 - become: yes - become_method: doas - tasks: - - name: Update apk cache - command: apk update - - - name: Install required system packages - apk: - name: "{{ packages }}" - state: present - vars: - packages: - - bash - - docker - - docker-cli-compose - - make - - rsync - - - name: Add docker service to start at boot - command: rc-update add docker default - - - name: Start docker service - service: - name: docker - state: started - enabled: yes - - - name: Add current user to the docker group - user: - name: "{{ ansible_user }}" - groups: docker - append: yes - - - name: Verify Docker installation - command: docker --version - register: docker_version - changed_when: false - - - name: Output Docker version - debug: - var: docker_version.stdout - - - name: Verify Docker Compose installation - command: docker compose version - register: compose_version - changed_when: false - - - name: Output Docker Compose plugin version - debug: - var: compose_version.stdout - - - name: Ensure ansible_user owns the deploy folder - file: - path: /srv - owner: "{{ ansible_user }}" - state: directory - recurse: yes - - - name: Update hostname - hostname: - name: "{{ inventory_hostname }}" - - - name: Update hostname with command - command: hostname "{{ inventory_hostname }}" - - - name: Ensure ~/.profile exists - file: - path: "/home/{{ ansible_user }}/.profile" - state: touch - owner: "{{ ansible_user }}" - group: "{{ ansible_user }}" - - - name: Update command prompt (PS1) to show user@hostname in .profile - lineinfile: - path: "/home/{{ ansible_user }}/.profile" - line: "export PS1='\\u@$(hostname):\\w\\$ '" - regexp: '^export PS1=' - owner: "{{ ansible_user }}" - group: "{{ ansible_user }}" diff --git a/iac/ansible/playbook-debian.yml b/iac/ansible/playbook-debian.yml deleted file mode 100644 index 5ecd6eb..0000000 --- a/iac/ansible/playbook-debian.yml +++ /dev/null @@ -1,83 +0,0 @@ -- name: Install Docker with docker compose plugin on Debian/Ubuntu Server - hosts: - - development2 - become: yes - tasks: - - name: Update apt cache - apt: - update_cache: yes - cache_valid_time: 3600 - - - name: Install required system packages - apt: - name: "{{ packages }}" - vars: - packages: - - ca-certificates - - curl - - gnupg - - make - - rsync - - - name: Add Docker’s official GPG key - apt_key: - url: https://download.docker.com/linux/debian/gpg - state: present - - - name: Set up the stable repository - apt_repository: - repo: "deb [arch=arm64] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable" - state: present - - - name: Install Docker Engine - apt: - name: docker-ce - state: latest - - - name: Add current user to the docker group - user: - name: "{{ ansible_user }}" - groups: docker - append: yes - - - name: Ensure Docker service is running - service: - name: docker - state: started - enabled: yes - - - name: Verify Docker installation - command: docker --version - register: docker_version - changed_when: false - - - name: Output Docker version - debug: - var: docker_version.stdout - - - name: Verify Docker Compose plugin installation - command: docker compose version - register: compose_version - changed_when: false - - - name: Output Docker Compose plugin version - debug: - var: compose_version.stdout - - - name: Ensure ansible_user owns the deploy folder - file: - path: /srv - owner: "{{ ansible_user }}" - state: directory - recurse: yes - - - name: Update hostname - hostname: - name: "{{ inventory_hostname }}" - - - name: Update PS1 to show full hostname - replace: - path: /home/{{ ansible_user }}/.bashrc - regexp: '\\u@\\h' - replace: '\\u@\\H' - backup: yes