需要输出的帮助:输出的下一行需要逐行打印,不需要\n..plz help..code,实际输出需要粘贴在这篇文章中
"msg":"19:19:11.445 UTC清华7月07 2022\n 1657235951\n 1657235051\n 19:04:11美国东部时间清华7月07 2022\n 2022 7月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
发布于 2022-07-08 17:21:10
啊,我看到你在自己的答案里贴出了一个解决方案,但是我只想指出原来问题的原因。
在YAML中,使用|
指定多行字符串会在每一个行中断处生成换行符(\n
)。在Ansible调试输出中,字符串是按字面解析的,因此它包含换行符,而不是实际的换行符。
您的解决方法是将每条消息作为列表中的单个项进行处理,输入到msg
中,这对您的情况是有效的。我不认为通过使用其他多行YAML说明符就可以得到您想要的结果。这是一个关于YAML中多行字符串的详细解释的答案,是.复杂的:
https://stackoverflow.com/questions/72903164
复制相似问题