Execution
Date 04 Sep 2024 13:12:47 +0100
Duration 00:00:05.74
Controller ssh-gw-4.layershift.com
User root
Versions
Ansible 2.16.4
ara 1.7.1 / 1.7.1
Python 3.10.10
Summary
1 Hosts
6 Tasks
6 Results
1 Plays
1 Files
0 Records

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

---
- name: Gather and save system facts
  hosts: all
  tasks:

    - name: Upload gather_facts.sh script to the remote host
      ansible.builtin.copy:
        src: files/gather_facts.sh
        dest: /tmp/gather_facts.sh
        mode: '0755'

    - name: Execute the gather_facts.sh script and save the output
      ansible.builtin.command: /tmp/gather_facts.sh
      register: script_output

    - name: Parse the script output and convert it into a JSON format
      ansible.builtin.set_fact:
        custom_facts: |
          {
            "os_version": "{{ script_output.stdout_lines[1].split(':')[1] | trim }}",
            "kernel_version": "{{ script_output.stdout_lines[2].split(':')[1] | trim }}",
            "cpu_architecture": "{{ script_output.stdout_lines[3].split(':')[1] | trim }}",
            "total_memory": "{{ script_output.stdout_lines[4].split(':')[1] | trim }}",
            "disk_space": "{{ script_output.stdout_lines[5].split(':')[1] | trim }}"
          }

    - name: Create the directory for storing custom facts
      ansible.builtin.file:
        path: /etc/ansible/facts.d
        state: directory
        mode: '0755'

    - name: Save the custom facts in JSON format
      ansible.builtin.copy:
        content: "{{ custom_facts | to_nice_json }}"
        dest: /etc/ansible/facts.d/custom_facts.fact
        mode: '0644'

    - name: Display the custom facts
      ansible.builtin.debug:
        var: custom_facts