Re: [ansible-project] concatenate a value with another variable in default filter

2017-01-05 Thread Nicolas G
Got it Matt , that makes sense.

Thanks .

On Thursday, January 5, 2017 at 10:46:00 AM UTC-5, Matt Martz wrote:
>
> Your general approach is pretty much correct in your last example:
>
> - hosts: "{{ hosts | default('bid-'+ dc) }}"
>
> Although jinja2 recommends using a `~` instead of `+` for string 
> concatenation, although both will work.
>
> However, the real problem is with your variable.  You have created the 
> `dc` variable in your inventory, which means that it is scoped to the hosts 
> within that group.  Because you are using the `dc` variable in your `hosts` 
> declaration, no hosts have been targeted, and additionally, the `dc` 
> variable is per host, not global.
>
> As such, you cannot use the `dc` variable in a hosts declaration due to 
> variable scoping, and the `dc` variable not being defined at that point.
>
> The only real place to define a variable that can be used in the `hosts` 
> declaration is via the command line via -e/--extra-vars
>
> On Thu, Jan 5, 2017 at 8:10 AM, Nicolas G  > wrote:
>
>> I have the bellow Ansible inventory :
>>
>> [web:children]
>> web-eu-west
>> web-us-east
>> [eu:vars]
>> dc=eu-west
>> [eu:children]
>> web-eu-west
>> [us:vars]
>> dc=us-east
>> [us:children]
>> web-us-east
>>
>>
>> The bellow is working fine in my playbook  :
>> -- hosts: "{{  hosts | default('web') }}"
>>
>>
>> Unfortunately we have a case now where we need to combine the *dc *variable 
>> in the default filter , I'm trying the bellow two combinations with errors :
>>
>> - hosts: "{{ hosts | default('web-'dc) }}"
>>
>>> ERROR! template error while templating string: expected token ',', got 
>>> 'dc'. String: {{ hosts | default('web-'dc) }}
>>
>>
>> - hosts: "{{ hosts | default('bid-'+ dc) }}"
>>
>>> ERROR! the field 'hosts' has an invalid value, which appears to include 
>>> a variable that is undefined. The error was: 'dc' is undefined
>>
>>
>> What is the right way to concatenate a value and a var using the default 
>> filter ?
>>
>> -- 
>> 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 .
>> To post to this group, send email to ansible...@googlegroups.com 
>> .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/1a96a93f-11a3-485a-be73-58beeb046c6f%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Matt Martz
> @sivel
> sivel.net
>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/f7b300ba-f154-44a1-80c9-a459e4ea4876%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] concatenate a value with another variable in default filter

2017-01-05 Thread Matt Martz
Your general approach is pretty much correct in your last example:

- hosts: "{{ hosts | default('bid-'+ dc) }}"

Although jinja2 recommends using a `~` instead of `+` for string
concatenation, although both will work.

However, the real problem is with your variable.  You have created the `dc`
variable in your inventory, which means that it is scoped to the hosts
within that group.  Because you are using the `dc` variable in your `hosts`
declaration, no hosts have been targeted, and additionally, the `dc`
variable is per host, not global.

As such, you cannot use the `dc` variable in a hosts declaration due to
variable scoping, and the `dc` variable not being defined at that point.

The only real place to define a variable that can be used in the `hosts`
declaration is via the command line via -e/--extra-vars

On Thu, Jan 5, 2017 at 8:10 AM, Nicolas G  wrote:

> I have the bellow Ansible inventory :
>
> [web:children]
> web-eu-west
> web-us-east
> [eu:vars]
> dc=eu-west
> [eu:children]
> web-eu-west
> [us:vars]
> dc=us-east
> [us:children]
> web-us-east
>
>
> The bellow is working fine in my playbook  :
> -- hosts: "{{  hosts | default('web') }}"
>
>
> Unfortunately we have a case now where we need to combine the *dc *variable
> in the default filter , I'm trying the bellow two combinations with errors :
>
> - hosts: "{{ hosts | default('web-'dc) }}"
>
>> ERROR! template error while templating string: expected token ',', got
>> 'dc'. String: {{ hosts | default('web-'dc) }}
>
>
> - hosts: "{{ hosts | default('bid-'+ dc) }}"
>
>> ERROR! the field 'hosts' has an invalid value, which appears to include a
>> variable that is undefined. The error was: 'dc' is undefined
>
>
> What is the right way to concatenate a value and a var using the default
> filter ?
>
> --
> 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-project+unsubscr...@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/ansible-project/1a96a93f-11a3-485a-be73-58beeb046c6f%40googlegroups.
> com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Matt Martz
@sivel
sivel.net

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAD8N0v_4H%3DPPtyCypjg2YVy1DxQwLAjh6Zs23xvRj87mOSq4zg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] concatenate a value with another variable in default filter

2017-01-05 Thread Johannes Kastl
On 05.01.17 15:10 Nicolas G wrote:
> - hosts: "{{ hosts | default('web-'dc) }}"

Try this:

- hosts: "{{ hosts | default([ [ 'web-', dc ] | join ] ) }}"
or
- hosts: "{{ hosts | default([ 'web-', dc ] | join ) }}"

Not sure if you need two sets of square brackets.

Johannes

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/914a07d6-492f-bab0-86f9-a1a254a883ef%40ojkastl.de.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: OpenPGP digital signature