Home > Enterprise >  Ansible: Need help n printing msgs one by one without /n ..I have given the code and the current out
Ansible: Need help n printing msgs one by one without /n ..I have given the code and the current out

Time:07-09

Need help from the output: Below line from the output need to be printed line by line without \n..plz help..code and actual output pasted with this post

"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 


CodePudding user response:


- debug:
         msg: 
          - "currenttime: {{arr}}"
          - " timetostring: {{ t1 }}"
          - " currentimeless15min: {{t2}}" 
          - " back15min: {{t3}}"
          -  "needmin: {{t4}}"

I have changed the above portion n my code and getting output as expected. TASK [debug] ***********************************************************************************************************************************************************************************************

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"
    ]
}

CodePudding user response:

Ah, I see you posted a solution in your own answer but I would just like to point out the reason for the original problem.

In YAML, using | to specify a multi-line string results in newlines (\n) at each line break. In Ansible debug output, the string is parsed literally so it includes the newline characters, and not actual newlines.

Your workaround by doing each message as a single item in a list fed to msg works for your case. I don't think there is a way to get the result you're looking for by using the other multi-line YAML specifiers. Here's an answer with a detailed explanation of multi-line strings in YAML, it's... complicated:

How do I break a string in YAML over multiple lines?

  • Related