Execution
Date 24 Jul 2025 10:14:34 +0100
Duration 00:00:03.92
Controller ssh-gw-4.layershift.com
User root
Versions
Ansible 2.16.11
ara 1.7.2 / 1.7.2
Python 3.10.10
Summary
1 Hosts
10 Tasks
10 Results
1 Plays
1 Files
0 Records

File: /home/ssh-gateway/ansible/kuly/RM10092_agent360_conf_change.yaml

---
- name: Search for diskusage block in file and replace it
  hosts: all
  gather_facts: false
  vars:
    config_file: "/etc/agent360.ini"
  tasks:
    - name: Check if file exists
      ansible.builtin.stat:
        path: "{{ config_file }}"
      register: file_stat

    - name: Fail if file doesn't exist
      ansible.builtin.fail:
        msg: "File {{ config_file }} not found"
      when: not file_stat.stat.exists

    - name: Extract diskusage block using awk
      ansible.builtin.shell: |
        awk '
        /^\[diskusage\]$/ { found=1; print; next }
        found && /^\[.*\]$/ && !/^\[diskusage\]$/ { found=0; next }
        found { print }
        ' "{{ config_file }}"
      register: block_content
      changed_when: false

    - name: Display diskusage block content
      ansible.builtin.debug:
        msg: |
          Diskusage block content:
          =======================
          {{ block_content.stdout }}
      when: block_content.stdout != ""

    - name: Check if block was found
      ansible.builtin.shell: "grep -q '^\\[diskusage\\]$' {{ config_file }} && echo 'found' || echo 'not found'"
      register: block_exists
      changed_when: false

    - name: Display message if block not found
      ansible.builtin.debug:
        msg: "Block [diskusage] not found in the file"
      when: block_exists.stdout == "not found"

    - name: Replace diskusage block with new content
      ansible.builtin.replace:
        path: "{{ config_file }}"
        regexp: |
          (?sm)^\[diskusage\].*?^(?=\[.*\]|$)
        replace: |
          [diskusage]
          enabled = true
          exclude = /dev/loop,/dev/snap,/squashfs,/cagefs-skeleton,/restore

        backup: true
        owner: agent360
        mode: '0600'
      when: block_exists.stdout == "found"
      register: replace_result

    - name: Display backup file location
      ansible.builtin.debug:
        msg: "Backup created at: {{ replace_result.backup_file }}"
      when: 
        - block_exists.stdout == "found"
        - replace_result.backup_file is defined

    - name: Display replacement result
      ansible.builtin.debug:
        msg: "Successfully replaced diskusage block"
      when:
        - block_exists.stdout == "found"
        - replace_result.changed

    - name: Display message when no changes were needed
      ansible.builtin.debug:
        msg: "No changes made to the file"
      when:
        - block_exists.stdout == "found"
        - not replace_result.changed