include_vars helps by making a chunk of work re-usable; thanks! However in
my use case, I would like to include text in one file into the variable
file to finally be included in a task. I've updated the git repository to
reflect this setup with use of include_vars. The problem is that the
output from dumping one multiline variable into another file in YAML format
doesn't indent each line to comply with the YAML multiline format.
The actual use case here is the use of the ec2 module to create new
instances, and I am setting the user_data field to some cloud-init YAML for
use in the initial provisioning of CoreOS (I would prefer to use Ansible,
but some of it can only be done as part of this cloudinit process). I want
to write files during this process that are included directly in the
cloud-config YAML, and so I would prefer to have those files as separate
files in my Ansible setup, such as "bashrc.yml" for example, and include
these inside a "cloudinit.yml" file that constructs the final text to use
for the user_data variable.
I found an awkward way to deal with this, though it does at least work.
One can use the Jinja2 indent filter as I have in the code:
multilinevar_from_included_varfile: |
var_tests:
- name: include some script in yaml format
script: |
{{ script_content*|indent(4)* }}"
If anyone knows of a cleaner way to do this I'd love to hear it.
Output from updated test without using the Jinja2 'indent' filter:
var_tests:
- name: var_from_playbook
v: var_from_playbook_VALUE
- name: var_from_role
v: var_from_role_VALUE
- name: include some script in yaml format
script: #!/bin/bash
echo this is my script!
echo notice that the lines in the final output are not indented currently
to be parsed correctly in the task file
On Thursday, 7 August 2014 14:23:09 UTC-7, Michael DeHaan wrote:
>
> The lookup plugin with "file" can't be used to interpolate variables and
> read them back in.
>
> Read the docs on the "include_vars" module and you may be a bit happier,
> and it eliminates the overcomplex lookup attempt.
>
>
> On Wed, Aug 6, 2014 at 7:24 PM, AJ Daws <[email protected] <javascript:>>
> wrote:
>
>> I created a simple example to demonstrate at
>> https://github.com/tdaws/ansible-templated-file-to-var-example. To try
>> it directly, you can git clone
>> https://github.com/tdaws/ansible-templated-file-to-var-example &&
>> run-me.sh
>> Or you can browse the files directly. The run-me.sh script just shows
>> what command I use to call the test.
>>
>> Output contains the {{ }} notation instead of the replacement values
>> desired:
>>
>> TASK: [testrole | Test multiline templated var]
>> *******************************
>> ok: [localhost] => {
>> "msg": "#testfile.yml\nvar_tests:\n - name: var_from_playbook\n v:
>> {{ var_from_playbook }}\n\n - name: var_from_role\n v: {{ var_from_role
>> }}"
>> }
>>
>>
>>
>>
>>
>> On Wednesday, 6 August 2014 08:06:22 UTC-7, Michael DeHaan wrote:
>>
>>> "Tried the lookup('file', 'my-user-data.yml') but it does not appear to
>>> be parsed by jinja2"
>>>
>>> Not sure why this would be the case but would need to see the playbook.
>>>
>>> Needs to be surrounded by "{{ }}" to evaluate of course, which may be
>>> part of the problem.
>>>
>>>
>>> On Tue, Aug 5, 2014 at 8:17 PM, Amr Ali <[email protected]> wrote:
>>>
>>>> Why not just use a variables file, along with include_vars.. you can
>>>> even use a group or hosts var file..
>>>>
>>>> What kind of "templating" options are you looking into using?
>>>>
>>>> --
>>>> 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 [email protected].
>>>> To post to this group, send email to [email protected].
>>>> To view this discussion on the web visit https://groups.google.com/d/
>>>> msgid/ansible-project/f7f83a30-61e6-4619-afe2-
>>>> d3487ae36b02%40googlegroups.com.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/ff6d36d1-103d-4528-8459-07ff43ac71b0%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/ansible-project/ff6d36d1-103d-4528-8459-07ff43ac71b0%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/f4ddc9f1-50e5-41ad-8ec5-50d3560ac09d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.