So I know I can do something like this:
copy:
dest: /etc/issue
content: |
Hello
World
But this doesn't work:
vars:
login_banner_text: !!str |-
"Hello\nWorld"
tasks:
- name: Set TTY login banner
copy:
dest: /etc/issue
content: "{{ login_banner_text }}"
The newline character is printed straight to the file, without parsing, i.e. it's a single line populated with \n
strings. I'd like to do this without copying a file into place, because I need to copy this text to two files.
For one file, the \n
strings need to remain unparsed, so it's written to file as a single line. For the other, I want the \n
to be interpreted, so that the text is expanded into multiple lines.
The first file is being modified using the ini_file
module. This task works as expected using the explicit \n
in the variable declaration.
- name: "Set message"
ini_file:
dest: /etc/dconf/db/gdm.d/00-security-settings
section: org/gnome/login-screen
option: banner-message-text
value: string '{{ login_banner_text }}'
create: yes
tags:
- always
However, other modules behave this way as well.
If I copy a file into place, the rather long text (not "Hello World") has to be maintained in two places.
Update
I found, what I think is, a better way of doing this, based on this post. It stores the banner in a separate file, and then uses that to modify both configuration files. So the value is only stored in one place. However, the answer given by @larsks does answer the question as it was originally posed.
- hosts: 127.0.0.1
connection: local
vars:
login_banner_text: "{{ lookup('file', 'login_banner.txt') }}"
tasks:
- name: "Set the GNOME3 Login Warning Banner Text"
ini_file:
dest: /etc/dconf/db/gdm.d/00-security-settings
section: org/gnome/login-screen
option: banner-message-text
value: '{{ login_banner_text | to_json }}'
create: yes
tags:
- always
- name: "Set the TTY Login Warning Banner Text"
copy:
dest: '/etc/issue'
content: "{{ '\n' + login_banner_text + '\n' }}"
tags:
- always