diff --git a/.ci.env b/.ci.env new file mode 100644 index 0000000..76a8f58 --- /dev/null +++ b/.ci.env @@ -0,0 +1,3 @@ +export ANSIBLE_LINT_EXCLUSION="--exclude collections/ansible_collections --exclude .gitea" +export ANSIBLE_INVENTORY="-i 127.0.0.1, --connection=local" +export ANSIBLE_PLAYBOOK="playbooks/gather_ipv4.yml" diff --git a/.gitea/workflows/ansible-lint.yml b/.gitea/workflows/ansible-lint.yml new file mode 100644 index 0000000..11eb93c --- /dev/null +++ b/.gitea/workflows/ansible-lint.yml @@ -0,0 +1,29 @@ +name: Ansible Lint Pipeline +run-name: ${{ gitea.actor }} is running Ansible Code Pipeline +on: + push: + branches: + - main + +jobs: + Ansible-Development-Pipeline: + strategy: + matrix: + os: [ ansible-dev-centos ] + runs-on: ${{ matrix.os }} + steps: + - name: Clone repository + uses: actions/checkout@v3 + + - name: Ansible Environment Verify + run: | + . ./.ci.env + env + git log -1 + ansible --version + ansible-lint --version + + - name: Run Ansible-Lint + run: | + . ./.ci.env + ansible-lint --offline $ANSIBLE_LINT_EXCLUSION diff --git a/.gitignore b/.gitignore index 538ca27..9c9faf7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ deprecated collections/ansible_collections roles/verified_reboot roles/requirements.yml +inventory diff --git a/ansible.cfg b/ansible.cfg index d4b7671..03b4684 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -1,3 +1,4 @@ [defaults] +inventory = inventory/hosts collections_path = collections roles_path = roles diff --git a/roles/rear_remove/tasks/cleanup.yml b/roles/rear_remove/tasks/cleanup.yml new file mode 100644 index 0000000..86e57ac --- /dev/null +++ b/roles/rear_remove/tasks/cleanup.yml @@ -0,0 +1,39 @@ +--- +- name: Check previous log file + ansible.builtin.stat: + path: "/var/log/rear/rear-{{ ansible_hostname }}.log" + register: old_rear_log + +- name: Backup ReaR log file + when: + - old_rear_log['stat']['exists'] + block: + - name: Copy old ReaR log file to backup location + ansible.builtin.copy: + src: "/var/log/rear/rear-{{ ansible_hostname }}.log" + dest: "/var/log/rear-{{ ansible_hostname }}-{{ '%Y-%m-%d' | strftime(old_rear_log['stat']['mtime']) }}.log" + mode: "0644" + backup: true + remote_src: true + + - name: Remove old ReaR log file + ansible.builtin.file: + path: "/var/log/rear/rear-{{ ansible_hostname }}.log" + state: absent + + - name: Remove files + ansible.builtin.file: + path: "{{ item }}" + state: absent + loop: + - "/boot/rear-initrd.cgz" + - "/boot/rear-kernel" + - "/etc/rear" + - "/tmp/rear" + - "/var/lib/rear" + + - name: Remove Grub2 config + ansible.builtin.file: + path: "/etc/grub.d/45_rear" + state: absent + notify: Rebuild Grub2 diff --git a/roles/rear_remove/tasks/main.yml b/roles/rear_remove/tasks/main.yml index 86e57ac..c70e58f 100644 --- a/roles/rear_remove/tasks/main.yml +++ b/roles/rear_remove/tasks/main.yml @@ -1,39 +1,29 @@ --- -- name: Check previous log file +- name: Check ReaR Output details ansible.builtin.stat: - path: "/var/log/rear/rear-{{ ansible_hostname }}.log" - register: old_rear_log + path: "/var/lib/rear" + register: file_details -- name: Backup ReaR log file +- name: Set current Date and ReaR file date + ansible.builtin.set_fact: + date_now: "{{ ansible_date_time['date'] }}" + file_date: "{{ '%Y-%m-%d' | strftime(file_details['stat']['mtime']) }}" when: - - old_rear_log['stat']['exists'] - block: - - name: Copy old ReaR log file to backup location - ansible.builtin.copy: - src: "/var/log/rear/rear-{{ ansible_hostname }}.log" - dest: "/var/log/rear-{{ ansible_hostname }}-{{ '%Y-%m-%d' | strftime(old_rear_log['stat']['mtime']) }}.log" - mode: "0644" - backup: true - remote_src: true + - file_details['stat']['exists'] | bool - - name: Remove old ReaR log file - ansible.builtin.file: - path: "/var/log/rear/rear-{{ ansible_hostname }}.log" - state: absent +- name: Compare Dates to delete based on our 2 days retention policy + ansible.builtin.set_fact: + file_status: "{{ ((date_now | to_datetime('%Y-%m-%d')) - (file_date | to_datetime('%Y=%m-%d'))).days }}" + when: + - file_details['stat']['exists'] | bool - - name: Remove files - ansible.builtin.file: - path: "{{ item }}" - state: absent - loop: - - "/boot/rear-initrd.cgz" - - "/boot/rear-kernel" - - "/etc/rear" - - "/tmp/rear" - - "/var/lib/rear" +# - name: Include Cleanup if the ReaR file is created 6 days back +# ansible.builtin.include_tasks: cleanup.yml +# when: +# - not (file_status|int <= 6) - - name: Remove Grub2 config - ansible.builtin.file: - path: "/etc/grub.d/45_rear" - state: absent - notify: Rebuild Grub2 +- name: Print Msg if ReaR file doesnt exist + ansible.builtin.debug: + msg: "The ReaR files don't exist on this server." + when: + - not file_details['stat']['exists'] | bool