需要输出的帮助: 输出的下面一行需要逐行打印,而不需要 ..请帮助..代码和实际输出粘贴在这篇文章中
"msg": "19:19:11.445 UTC Thu Jul 07 2022\n1657235951 \n1657235051 \n19:04:11 EDT Thu Jul 07 2022\n2022 Jul 07 19:04:11\n"
---
- name: Cisco NXOS
hosts: all
connection: network_cli
gather_facts: false
vars:
- cmdlist1: sh clock
- ansible_python_interpreter: /usr/bin/python3
- ansible_network_os: nxos
- cmdlist2: sh logging | include 2/3
tasks:
- name: Execute command
nxos_command:
commands: "{{ cmdlist1 }}"
register: output
- set_fact:
arr: "{{ output.stdout_lines[0][1] }}"
- debug:
msg: |
{{ arr | trim }}
{{ t1 }}
{{ t2 }}
{{ t3 }}
{{ t4 }}
# msg: "{{ msg.split('\n') }}"
vars:
t1: "{{ (arr|to_datetime('%H:%M:%S.%f %Z %a %b %d %Y')).strftime('%s') }}"
t2: "{{ t1|int - 15 * 60 }}"
t3: "{{ '%H:%M:%S %Z %a %b %d %Y'|strftime(t2) }}"
t4: "{{ '%Y %b %d %H:%M:%S' | strftime(t2) }}"
================
OUTPUT
[LABPC@lab-jump-host dow]$ ansible-playbook interfaceflappingdup.yml -i inventory1.txt --limit nxos --verbose
Using /etc/ansible/ansible.cfg as config file
PLAY [Cisco NXOS] ******************************************************************************************************************************************************************************************
TASK [Execute command] *************************************************************************************************************************************************************************************
ok: [nxos] => {"changed": false, "stdout": ["Time source is NTP\n19:19:11.445 UTC Thu Jul 07 2022"], "stdout_lines": [["Time source is NTP", "19:19:11.445 UTC Thu Jul 07 2022"]]}
TASK [set_fact] ********************************************************************************************************************************************************************************************
ok: [nxos] => {"ansible_facts": {"arr": "19:19:11.445 UTC Thu Jul 07 2022"}, "changed": false}
TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [nxos] => {
"msg": "19:19:11.445 UTC Thu Jul 07 2022\n1657235951 \n1657235051 \n19:04:11 EDT Thu Jul 07 2022\n2022 Jul 07 19:04:11\n"
}
PLAY RECAP *************************************************************************************************************************************************************************************************
nxos : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- debug:
msg:
- "currenttime: {{arr}}"
- " timetostring: {{ t1 }}"
- " currentimeless15min: {{t2}}"
- " back15min: {{t3}}"
- "needmin: {{t4}}"
我已经更改了我的代码的上述部分并按预期获得输出。 任务[调试] ************************************************** ****************************************************** ****************************************************** **************************************************
ok: [nxos] => {
"msg": [
"currenttime: 06:47:12.579 UTC Fri Jul 08 2022",
" timetostring: 1657277232",
" currentimeless15min: 1657276332",
" back15min: 06:32:12 EDT Fri Jul 08 2022",
"needmin: 2022 Jul 08 06:32:12"
]
}
啊,我看到你在自己的答案中发布了解决方案,但我只想指出最初问题的原因。
在 YAML 中,使用
|
指定多行字符串会在每个换行符处产生换行符 (\n
)。在 Ansible 调试输出中,字符串按字面解析,因此它包含换行符,而不是实际的换行符。
您的解决方法是将每条消息作为馈送到
msg
的列表中的单个项目,适用于您的情况。我认为没有办法通过使用其他多行 YAML 说明符来获得您正在寻找的结果。这是一个答案,详细解释了 YAML 中的多行字符串,它......很复杂:
因为我看到你的机器上安装了 python,这可能对你有帮助
---
- name: Cisco NXOS
hosts: all
connection: network_cli
gather_facts: false
vars:
- cmdlist1: sh clock
- ansible_python_interpreter: /usr/bin/python3
- ansible_network_os: nxos
- cmdlist2: sh logging | include 2/3
tasks:
- name: Execute command
nxos_command:
commands: "{{ cmdlist1 }}"
register: output
- set_fact:
arr: "{{ output.stdout_lines[0][1] }}"
- name: print the debug using python
vars:
t1: "{{ (arr|to_datetime('%H:%M:%S.%f %Z %a %b %d %Y')).strftime('%s') }}"
t2: "{{ t1|int - 15 * 60 }}"
t3: "{{ '%H:%M:%S %Z %a %b %d %Y'|strftime(t2) }}"
t4: "{{ '%Y %b %d %H:%M:%S' | strftime(t2) }}"
command: /usr/bin/python
args:
stdin: |
print(" ")
print("""
{{ arr | trim }}
{{ t1 }}
{{ t2 }}
{{ t3 }}
{{ t4 }}
""")
print(" ")