Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store the contents of the file to a variable in ansible when the file contains new lines as well

Tags:

ansible

I'm trying to store content of the certificate.pem file to a variable using the following task:

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

The variable root_certificate_content has the entire content of the file but instead of a new line it is replacing it with a space. I there a way I can get the certificate content as it is in the variable.

like image 269
Abc Avatar asked Dec 15 '25 14:12

Abc


1 Answers

update

Q: "A new line is replacing with a space."

A: What you see depends on the callback. Given the file

shell> cat /tmp/certificate.pem 
line 1
line 2
line 3

Get the content of the file either by lookup (if the file is on the controller)

  ca_certificate_file: /tmp/certificate.pem
  cert: "{{ lookup('file', ca_certificate_file) }}"

, or declare the variables

  ca_certificate_file: /tmp/certificate.pem
  cert: "{{ out.stdout }}"

and read the file on the remote host (works on the controller too)

    - command: "cat {{ ca_certificate_file }}"
      register: out

Display the variable cert

    - debug:
        var: cert

The default callback format is JSON (the newlines \n are visible)

shell> ANSIBLE_STDOUT_CALLBACK=default ansible-playbook pb.yml -l localhost
 ...
ok: [localhost] => {
    "cert": "line 1\nline 2\nline 3"
}

The YAML format would be

shell> ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook pb.yml -l localhost
  ...
ok: [localhost] => 
  cert: |-
    line 1
    line 2
    line 3

Example of a complete playbook for testing

shell> cat pb.yml 
- hosts: all

  vars:

    ca_certificate_file: /tmp/certificate.pem
    # cert: "{{ lookup('file', ca_certificate_file }}"
    cert: "{{ out.stdout }}"

  tasks:

    - command: "cat {{ ca_certificate_file }}"
      register: out

    - debug:
        var: cert

origin

Try lookup plugins

    - set_fact:
        root_certificate_content: "{{ lookup('file', ca_certificate_file) }}"

For example, " the variable "root_certificate_content" should have the contents of the file as it is. If the file has a new line then it should come as the new line". The play below

    - hosts: localhost
      tasks:
        - set_fact:
            root_certificate_content: "{{ lookup('file', 'cert1') }}"
        - debug:
            msg: "{{ root_certificate_content.split('\n') }}"

with the file (3 lines with newline each)

shell> cat cert1 
line 1
line 2
line 3

gives the content of the variable root_certificate_content (3 lines with newline each)

  "msg": [
      "line 1", 
      "line 2", 
      "line 3"
  ]

"if you just show the value of root_certificate_content without using .split('\n') in the debug msg"

    - debug:
        var: root_certificate_content

then the newlines can be seen in the string

  "root_certificate_content": "line 1\nline 2\nline 3"
like image 72
Vladimir Botka Avatar answered Dec 17 '25 08:12

Vladimir Botka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!