Execution
Date 24 Jul 2025 13:12:11 +0100
Duration 00:00:08.47
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 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: Run actions if block found
      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

            backup: true
            owner: agent360
            mode: '0600'
          register: replace_result
          notify: "Display replacement result"

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

        - name: Restart agent360 service
          ansible.builtin.systemd:
            name: agent360
            state: restarted
      always:
        - name: Check service status
          ansible.builtin.shell: |
            set -o pipefail
            service_time=$(systemctl show -p ActiveEnterTimestamp agent360.service | awk -F'=' '{print $2}')
            service_epoch=$(date -d "$service_time" +%s)
            current_time=$(date +%s)
            time_diff=$((current_time - service_epoch))
            echo $time_diff
          args:
            executable: /bin/bash
          register: time_difference_output
          changed_when: false
        - name: Determine if service was restarted recently
          ansible.builtin.debug:
            msg: "Service was restarted recently."
          when: time_difference_output.stdout | int > 1 and time_difference_output.stdout | int < 300
        - name: Get services status
          ansible.builtin.service_facts:
        - name: Write to a file the failed one
          when: '"running" not in ansible_facts.services["agent360.service"].state'
          ansible.builtin.lineinfile:
            path: /home/ssh-gateway/ansible/kuly/stopped.txt
            create: true
            mode: '0644'
            line: '{{ inventory_hostname }} is not running'
          delegate_to: localhost
  handlers:
    - name: Display replacement result
      ansible.builtin.debug:
        msg: "Successfully replaced diskusage block"