Ansible:如何使用换行字符追加多个变量?

huangapple go评论75阅读模式
英文:

Ansible: How to append multiple variables with new line character?

问题

Here is the translated code section:

我有这个脚本:

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

输出如下:

任务 [显示输出] ****
ok: [localhost] => {
    "msg": "Hello\nBye"

但我想要类似这样的输出:

msg: "Hello
      Bye"

我如何通过多个变量 + new_line 创建新的字符串变量 new_text?我不想在每个定义的 text_msg 后面使用 \n,像这样:

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

但我想使用脚本中的方法:text_msg1 + new_line + text_msg2

英文:

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

答案1

得分: 1

以下是您要翻译的内容:

输出是...但我想要类似这样的东西

您观察到的原因是调试输出以标准的JSON格式呈现,正如您可能从第一行ok: [localhost] => {中注意到的那样。

我理解您的问题的一部分,您想以另一种格式显示ansible-playbook的输出。

如果在ansible.cfg中使用默认设置:

# stdout_callback         = yaml

像这样的剧本:

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

会产生以下输出:

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

设置其他回调插件

stdout_callback         = yaml

将导致YAML格式化的Ansible屏幕输出输出:

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

    This is:
    Some longer example text

    with line breaks.

更多文档

更多问题和答案

英文:

> 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

答案2

得分: 1

以下是您提供的代码部分的翻译:

---
- hosts: localhost
  gather_facts: false

  vars:
    # You can add any number of lines
    my_lines:
      - 你好
      - 我可以添加很多行
      - 还有更多
      - 再见

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

  tasks:
    - name: 显示预期结果
      ansible.builtin.debug:
        var: my_lines_joined

    - name: "为了消除任何误解,让我们将结果推送到稍后可以检查的文件中,以确保我们有效地写入了新行"
      ansible.builtin.copy:
        content: "{{ my_lines_joined }}"
        dest: /tmp/test-result.txt

运行这个剧本会产生以下结果:

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

TASK [显示预期结果] *******************************************************************************************************************************************
ok: [localhost] => {
    "my_lines_joined": "你好\n我可以添加很多行\n还有更多\n再见"
}

TASK [为了消除任何误解,让我们将结果推送到稍后可以检查的文件中,以确保我们有效地写入了新行] *********************************************
changed: [localhost]

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

然后我们可以检查文件内容:

$ cat /tmp/test-result.txt 
你好
我可以添加很多行
还有更多
再见

请注意,这只是代码部分的翻译。如果您有其他需要,请告诉我。

英文:

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

huangapple
  • 本文由 发表于 2023年6月27日 21:52:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定