当文件包含新行时,如何将文件内容存储到ansible中的变量

当文件包含新行时,如何将文件内容存储到ansible中的变量,ansible,Ansible,我正在尝试使用以下任务将certificate.pem文件的内容存储到变量中: - name: Get the contents of the root certificate shell: cat {{ ca_certificate_file }} - name: Decode data and store as fact set_fact: root_certificate_content: "{{ ca_certificate_data.stdout }}"

我正在尝试使用以下任务将certificate.pem文件的内容存储到变量中:

 - name: Get the contents of the root certificate
   shell: cat {{ ca_certificate_file }}

 - name: Decode data and store as fact
   set_fact:
     root_certificate_content: "{{ ca_certificate_data.stdout }}"
变量root_certificate_content包含文件的全部内容,但它不是一个新行,而是用一个空格替换它。我有一种方法可以获取变量中的证书内容。

试试看

例如“
变量“root\u certificate\u content”应该具有文件的内容。如果文件有新行,那么它应该作为新行出现。”。下面的剧本

- hosts: localhost
  tasks:
    - set_fact:
        root_certificate_content: "{{ lookup('file', 'cert1') }}"
    - debug:
        msg: "{{ root_certificate_content.split('\n') }}"
使用该文件(3行,每行换行)

给出变量root_certificate_content的内容(3行,每行换行)

如果只显示根证书内容的值,而不在调试消息中使用.split('\n')

然后可以在字符串中看到换行符

"root_certificate_content": "line 1\nline 2\nline 3"
试一试

例如“
变量“root\u certificate\u content”应该具有文件的内容。如果文件有新行,那么它应该作为新行出现。”。下面的剧本

- hosts: localhost
  tasks:
    - set_fact:
        root_certificate_content: "{{ lookup('file', 'cert1') }}"
    - debug:
        msg: "{{ root_certificate_content.split('\n') }}"
使用该文件(3行,每行换行)

给出变量root_certificate_content的内容(3行,每行换行)

如果只显示根证书内容的值,而不在调试消息中使用.split('\n')

然后可以在字符串中看到换行符

"root_certificate_content": "line 1\nline 2\nline 3"

查找也没有解决新行和空格的问题。我还是得到了空间,而不是新的线路。这很奇怪。它对我有用。我已经更新了答案。我希望变量“root\u certificate\u content”应该包含文件的内容,因为这意味着如果文件有新行,那么它应该是新行,而不是空格。这就是我在答案中看到的。我已更新了它的更多详细信息。如果您只回显根证书内容的值,而不在调试msglookup中使用.split('\n'),又无法解决新行和空格的问题,该怎么办。我还是得到了空间,而不是新的线路。这很奇怪。它对我有用。我已经更新了答案。我希望变量“root\u certificate\u content”应该包含文件的内容,因为这意味着如果文件有新行,那么它应该是新行,而不是空格。这就是我在答案中看到的。我已更新了它的更多详细信息。如果您只是回显根证书内容的值,而不在调试消息中使用.split('\n'),该怎么办
- debug:
    var: root_certificate_content
"root_certificate_content": "line 1\nline 2\nline 3"