Ansible - How register some line in variable

44 views
Skip to first unread message

Karther

unread,
Dec 10, 2019, 9:20:54 AM12/10/19
to Ansible Project
Hey Community ansible !! :)

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 ? :)

Best regards,

Karther

Stefan Hornburg (Racke)

unread,
Dec 10, 2019, 10:15:38 AM12/10/19
to ansible...@googlegroups.com
Sorry I have no idea what you want to achieve.

Regards
Racke

>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/a3b343ad-216c-45f5-b1c5-9f0c1bb97cb9%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/a3b343ad-216c-45f5-b1c5-9f0c1bb97cb9%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Karther

unread,
Dec 10, 2019, 10:32:56 AM12/10/19
to Ansible Project
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 !! :)

Regards,

Le mardi 10 décembre 2019 16:15:38 UTC+1, Stefan Hornburg (Racke) a écrit :
On 12/10/19 3:20 PM, Karther wrote:
> Hey Community ansible !! :)
>
> 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 ? :)
>
> Best regards,
>
> Karther

Sorry I have no idea what you want to achieve.

Regards
         Racke

>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to

j1f0x

unread,
Dec 12, 2019, 4:48:49 PM12/12/19
to Ansible Project
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

Karther

unread,
Dec 31, 2019, 3:34:58 AM12/31/19
to Ansible Project
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 ?? :)

Thanks very much,

Best Regards,

Karther

Vladimir Botka

unread,
Dec 31, 2019, 4:26:57 AM12/31/19
to Karther, ansible...@googlegroups.com
On Tue, 31 Dec 2019 00:34:57 -0800 (PST)
Karther <ryadmo...@gmail.com> wrote:

> No I have this result with this task :
> "msg": "exactly as you see\nwill appear these three\nlines of poetry\n"
>
> Le jeudi 12 décembre 2019 22:48:49 UTC+1, j1f0x a écrit :
> > maybe you try this:
> > include_newlines: |
> > exactly as you see
> > will appear these three
> > lines of poetry

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

J Hawkesworth

unread,
Dec 31, 2019, 4:13:33 PM12/31/19
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages