Execution
Date 24 Jul 2025 11:17:23 +0100
Duration 00:00:03.61
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
8 Tasks
8 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: 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: Configure diskusage block
      when: block_exists.stdout == "found"
      block:
        - 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\n
            backup: true
            owner: agent360
            mode: '0600'
          register: replace_result
          notify: Handle successful replacement

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

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

  handlers:
    - name: Handle successful replacement
      ansible.builtin.include_tasks: RM10092_replacement_handler.yml