簡體   English   中英

使用date.stdout過濾器和stdout中的變量的Ansible劇本

[英]Ansible playbook using date.stdout filter with variable from stdout

我正在嘗試運行此任務:

---
- name: "{{ BANNER }}"

  shell: "rpm -qf /etc/issue"
  register: rpm
  changed_when: False
  ignore_errors: True

- shell: 'rpm -q -i "{{ rpm.stdout }}" | grep "Install Date:" | awk ''{ print $4 " " $5 " " $6 }'''
  register: rpm
  changed_when: False
  ignore_errors: True

- shell: 'date -d "{{ rpm.stdout }}" +''%Y-%d-%m'''
  register: date
  changed_when: False
  ignore_errors: True

- debug: var=date.stdout

- debug: var={{ (( date.stdout | to_datetime('%Y-%m-%d')) - ("2020-12-25" | to_datetime('%Y-%m-%d'))).days  }}

基本上,我需要傳遞data.stdout中包含的字符串以過濾to_datetime以進行日期減法,但是我收到此錯誤:

TASK [RH7-008 : debug] **********************************************************************************************************************************************
ok: [192.168.56.1] => {
    "date.stdout": "2019-14-03"
}

TASK [RH7-008 : debug] **********************************************************************************************************************************************
fatal: [192.168.56.1]: FAILED! => {"msg": "the field 'args' has an invalid value ([u'check_mode']), and could not be converted to an dict.The error was: time data '2019-14-03' does not match format '%Y-%m-%d'\n\nThe error appears to have been in '/root/ansible/roles/RH7-008/tasks/check_mode.yml': line 27, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug: var={{ (( date.stdout | to_datetime('%Y-%m-%d')) - (\"2020-12-25\" | to_datetime('%Y-%m-%d'))).days  }}\n  ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n\nexception type: <type 'exceptions.ValueError'>\nexception: time data '2019-14-03' does not match format '%Y-%m-%d'"}
        to retry, use: --limit @/root/ansible/main.retry

to_datetime('%Y-%m-%d')指定的格式輸入相比, date.stdout包含的格式輸入似乎是錯誤的。 我想念什么? 也許date.stdout有一些奇怪的角色?

提前致謝! 托馬索。

您要to_datetime解析%Y-%m-%d格式的日期。

您正在傳遞格式為2019-14-03數據。

一年中沒有14個月。

您希望to_datetime的format參數與您提供給date命令的format參數匹配:

to_datetime('%Y-%d-%m')

制作(為清晰起見,略微重新格式化,並且將硬編碼日期交換為與您的格式相匹配):

- debug:
    var: >-
      (
      (date.stdout | to_datetime('%Y-%d-%m')) -
      ("2020-25-12" | to_datetime('%Y-%d-%m'))
      ).days

或者,如果您確實需要%Y-%m-%d ,則將參數交換到date命令。 只要確保它們匹配即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM