How to calculate ansible_uptime_seconds and output this in os.csv

1.8k Views Asked by At

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.

2

There are 2 best solutions below

2
Zeitounator On

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 
0
user22223187 On
- 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