0

I have this script:

---
- hosts: localhost

  vars:

    new_line: "\n"
    text_msg1: "Hello"
    text_msg2: "Bye"

  tasks:

  - name: append
    set_fact:
      new_text: "{{ text_msg1 + new_line + text_msg2 }}"

  - name: Show Output
    debug:
      msg: "{{ new_text }}"

The output is:

TASK [Show Output] ****
ok: [localhost] => {
    "msg": "Hello\nBye"

But I would like to have something like this:

msg": "Hello
       Bye"

how can I create new STRING variable new_text via multiple variables + new_line between ? I don't wanna use \n after end of each defined text_msg like :

text_msg1: "Hello \n"
text_msg2: "Bye \n"

But I would like use approach I have in script : text_msg1 + new_line + text_msg2

andrew
  • 459
  • 5
  • 21
  • Does "[How to format Ansible debug output?](https://stackoverflow.com/a/71660574/6771046)" answer your question? In respect to your initial description and question you could probably take advantage from [What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – U880D Jun 27 '23 at 14:10

2 Answers2

1

The output is ... But I would like to have something like this

The reason for your observation is that the debug output is in standard JSON format as you may have noticed from the first line ok: [localhost] => {.

I understand that part of your question that you like to display the ansible-playbook output in an other way formatted.

Whereby with a default setting in ansible.cfg of

# stdout_callback         = yaml

a playbook like

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:
    text: |

      This is:
      Some longer example text

      with line breaks.

  tasks:

  - name: Show message
    debug:
      msg: "{{ text }}"

results into an output of

TASK [Show message] ****************************************************
ok: [localhost] => {
    "msg": "\nThis is:\nSome longer example text\n\nwith line breaks.\n"
}

Setting an other Callback Plugin

stdout_callback         = yaml

will result into an YAML-ized Ansible screen output output of

TASK [Show message] ********
ok: [localhost] =>
  msg: |2-

    This is:
    Some longer example text

    with line breaks.

Further Documentation

Further Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40
  • I updated my Q, basically I am looking for solution how can I create new STRING variable via multiple variables + new_line, I don't wanna use \n in each text_msg like text_msg1 "Hello \n" – andrew Jun 27 '23 at 14:09
  • Since your update doesn't reflect what you have commented here, at least not for me, can you update your question again with much more context and description? It is also recommended to provide a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – U880D Jun 27 '23 at 14:13
1

Example for an arbitrary number of lines

---
- hosts: localhost
  gather_facts: false

  vars:
    # You can add any number of lines
    my_lines:
      - hello
      - I can add many lines
      - and even more
      - bye

    my_lines_joined: "{{ my_lines | join('\n') }}"

  tasks:
    - name: Display the expected result
      ansible.builtin.debug:
        var: my_lines_joined

    - name: "To clear any misunderstanding, let's push the result
            to a file we can later inspect to see we effectively
            wrote new lines"
      ansible.builtin.copy:
        content: "{{ my_lines_joined }}"
        dest: /tmp/test-result.txt

Running this playbook results in:

PLAY [localhost] *************************************************************************************************************************************************************

TASK [Display the expected result] *******************************************************************************************************************************************
ok: [localhost] => {
    "my_lines_joined": "hello\nI can add many lines\nand even more\nbye"
}

TASK [To clear any misunderstanding, let's push the result to a file we can later inspect to see we effectively wrote new lines] *********************************************
changed: [localhost]

PLAY RECAP *******************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

And we can then inspect the file content:

$ cat /tmp/test-result.txt 
hello
I can add many lines
and even more
bye
Zeitounator
  • 38,476
  • 7
  • 53
  • 66