【问题标题】:How to calculate ansible_uptime_seconds and output this in os.csv如何计算 ansible_uptime_seconds 并将其输出到 os.csv
【发布时间】:2022-11-04 23:09:07
【问题描述】:

我正在尝试创建一个可用于查看某些系统详细信息的 csv 文件。其中一项是系统正常运行时间,以 unix 秒为单位。但在 os.csv 输出文件中,我希望将其视为天数,HH:MM:SS。

在我的 yaml 脚本下面:

---
- 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

任何帮助表示赞赏。

尝试了几次转换,但无法使其正常工作。

【问题讨论】:

    标签: ansible uptime


    【解决方案1】:

    complex data manipulations 的官方文档中实际上有一个(有点难以找到)示例,完全符合您的要求(请查看页面底部)。

    这是在 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) }}
    

    在我的机器上给出:

    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 
    

    【讨论】:

      猜你喜欢
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      相关资源
      最近更新 更多