0

I am trying to create a csv file that can be used to review certain systems details. One of these items is the system uptime, which is reflected in unix seconds. But in the os.csv output file I would like to see it as days, HH:MM:SS.

Below my yaml script:

---
- name: playbook query system and output to file
  hosts: OEL7_systems 
  vars:
    output_file: os.csv
  tasks:
     - block:
         # For permisison setup.
         - name: get current user
           command: whoami
           register: whoami
           run_once: yes
 
         - name: clean_file
           copy:
             dest: "{{ output_file }}"
             content: 'hostname,distribution,version,release,uptime'
             owner: "{{ whoami.stdout }}"
           run_once: yes
 
         - name: fill os information
           lineinfile:
             path: "{{ output_file }}"
             line: "{{ ansible_hostname }},\
               {{ ansible_distribution }},\
               {{ ansible_distribution_version }},\
               {{ ansible_distribution_release }},\
               {{ ansible_uptime_seconds }}"
           # Tries to prevent concurrent writes.
           throttle: 1
       delegate_to: localhost

Any help is appreciated.

tried several conversions but can't get it to work.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
JDFreez
  • 1
  • 1

2 Answers2

2

There is actually a (somewhat hard to find) example in the official documentation on complex data manipulations doing exactly what you are looking for (check at the bottom of the page).

Here is a full example playbook to run it on localhost

---
- hosts: localhost

  tasks:
    - name: Show the uptime in days/hours/minutes/seconds
      ansible.builtin.debug:
        msg: Uptime {{ now().replace(microsecond=0) - now().fromtimestamp(now(fmt='%s') | int - ansible_uptime_seconds) }}

which gives on my machine:

PLAY [localhost] ************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************
ok: [localhost]

TASK [Show the uptime in days/hours/minutes/seconds] ************************************************************************************************************
ok: [localhost] => {
    "msg": "Uptime 1 day, 3:56:34"
}

PLAY RECAP ******************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • tnx for the reply. That part I understood, but what I am trying to achieve is to output that answer into the output file. Which I have no idea how to do that. So the outcome of the task (in this case "Uptime 1 day, 3:56:34" needs to be part of the csv file. Any suggestions? – JDFreez Nov 07 '22 at 08:00
  • 1
    Well change the debug task in the example to an other that will write to a file like `lineinfile` or `copy`..... – Zeitounator Nov 07 '22 at 11:45
1
- name: Show the uptime in days/hours/minutes/seconds
  set_fact:
    uptime: "{{ ansible_facts['facter_system_uptime']['uptime'] }}"

- name: fill os information
  lineinfile:
    path: "{{ output_file }}"
    line: "{{ ansible_hostname }},\
      {{ ansible_distribution }},\
      {{ ansible_distribution_version }},\
      {{ ansible_distribution_release }},\
      {{ ansible_facts.kernel }},\
      {{ uptime }}"
  throttle: 1 # to try to prevent concurrent writes
  delegate_to: localhost
U880D
  • 8,601
  • 6
  • 24
  • 40