Ansible - How register some line in variable

Hey Community ansible !! :slight_smile:

I have some line texte with this content :

my first line
my second line

my 3ème line
my 4ème line

I want register all this line with one variable ? i don’t know if possible or not please ???

Can you help me please Community Ansible ? :slight_smile:

Best regards,

Karther

Hey Community ansible !! :slight_smile:

I have some line texte with this content :

my first line
my second line

my 3ème line
my 4ème line

I want register all this line with one variable ? i don't know if possible or not please ???

Can you help me please Community Ansible ? :slight_smile:

Best regards,

Karther

Sorry I have no idea what you want to achieve.

Regards
         Racke

Hey,

I want register some line in one variable …

I have some line :

my first line
my second line

my 3ème line
my 4ème line

I want that my variable =

my first line
my second line

my 3ème line
my 4ème line

Thanks for your help !! :slight_smile:

Regards,

Hi,

maybe you try this:

include_newlines: |
            exactly as you see
            will appear these three
            lines of poetry

fold_newlines: >
            this is really a
            single line of text
            despite appearances

see: https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

Hey,

no I have this result with this task :

“msg”: “exactly as you see\nwill appear these three\nlines of poetry\n”

Can you help me please ?? :slight_smile:

Thanks very much,

Best Regards,

Karther

The playbook below is self-explaining

- hosts: localhost
  vars:
    msg: |
      exactly as you see
      will appear these three
      lines of poetry
  tasks:
    - debug:
        msg: "{{ msg }}"
    - debug:
        msg: "{{ msg.split('\n') }}"
    - debug:
        msg: "{{ msg.split('\n')[:-1] }}"

gives

ok: [localhost] => {
    "msg": "exactly as you see\nwill appear these three\nlines of poetry\n"
}

ok: [localhost] => { "msg": [
        "exactly as you see",
        "will appear these three",
        "lines of poetry",
        ""
    ]
}

ok: [localhost] => { "msg": [
        "exactly as you see",
        "will appear these three",
        "lines of poetry"
    ]
}

HTH,

  -vlado

Here is another way:

`

  • hosts: localhost
    gather_facts: no

tasks:

  • name: register file contents
    shell: cat /path/to/file/that/contains/text
    register: file_contents

  • name: show file contents
    debug:
    msg: ‘{{ file_contents.stdout }}’
    `

or you can use a lookup

`

  • hosts: localhost
    gather_facts: no
    tasks:

  • name: register file contents
    set_fact:
    file_contents: “{{ lookup(‘file’, ‘/path/to/file/that/contains/text’ )}}”

  • name: show file contents
    debug:
    msg: “{{ file_contents }}”

`

Hope this helps

Jon