Discussion:
[ansible-project] variable set to a default value in play vars unexpectedly becomes not the default value
Kathy Allen
2015-07-22 00:04:07 UTC
Permalink
Hi there,

I'm using ansible 1.9.2. We don't use ansible for configuration management,
but instead for deployment automation.

I'm trying to set some conditions early on in a play so that the right
tasks get executed under the right conditions later in the play. This play
is called multiple times from a playbook. I thought I had a good thing
going here, until I realized upon the 2nd include of the play, the default
I intended for a var was set to a value which got set within the first
include of the play. I had thought that upon the 2nd include of the play,
all vars would get set again to my intended default value. But that didn't
happen.

The details of what's happening, including the playbook and play are at the
bottom...

And when you get there .. Is this expected? Is this a bug? Shouldn't it
work the way I expect?

The reason why I'm even going down this road is because ansible doesn't
support conditionally including a play within a playbook. "Conditions on
include don't apply to the include itself, it is applied to the tasks
included", from a previous post.

Thank you in advance!
kallen


ansible_1.9.2 $ cat sushi_playbook.yml
---
- hosts: 127.0.0.1
connection: local
gather_facts: False
tasks:

# in group_vars/all:
# by default, verbose is False
# ... yes, the inventory has a group called 'all'

- include: "{{ playbook_dir }}/dev/test_sushi.yml fish=hamachi
sushi_tool_prepare=true"
- include: "{{ playbook_dir }}/dev/test_sushi.yml fish=hamachi
sushi_tool_phase_pickles=true"

#- include: "{{ playbook_dir }}/dev/test_sushi.yml fish=sake
sushi_tool_prepare=true"
#- include: "{{ playbook_dir }}/dev/test_sushi.yml fish=sake
sushi_tool_phase_pickles=true"


ansible_1.9.2 $ cat -b dev/test_sushi.yml
1 ---
2 - hosts: 127.0.0.1
3 connection: local
4 gather_facts: False
5 vars:
6 # set the defaults:
7 testify: False
8 sushi_tool_prepare: False
9 sushi_tool_phase_pickles: False
10 sushi_tool_phase_ginger: False
11 sushi_tool_phase_daikon: False
12 local_run_sushi_prepare_hamachi: False
13 local_run_sushi_prepare_sake: False
14 local_fish_is_hamachi: False
15 local_fish_is_sake: False

16 tasks:
17 - name: start of play
18 debug: msg=
"========================================================================================"

19 - debug: var=fish
20 # For these next 2 vars, i expect them to always be False here,
having taken
21 # on such value from vars default above ^^. But my expectation is
thwarted
22 - debug: var=local_run_sushi_prepare_sake
23 - debug: var=local_run_sushi_prepare_hamachi

24 - set_fact: testify=True
25 when: verbose|bool

26 - set_fact: sushi_phase="prepare"
27 when: sushi_tool_prepare|bool
28 - set_fact: sushi_phase="pickles"
29 when: sushi_tool_phase_pickles|bool
30 - set_fact: sushi_phase="ginger"
31 when: sushi_tool_phase_ginger|bool
32 - set_fact: sushi_phase="daikon"
33 when: sushi_tool_phase_daikon|bool

34 - set_fact: local_fish_is_hamachi=True
35 when: fish == "hamachi"
36 - set_fact: local_fish_is_sake=True
37 when: fish == "sake"

38 # TODO is this a bug? from the playbook, when this play is called
a second
39 # time, the default of False at the top isn't honored?
40 - set_fact: local_run_sushi_prepare_hamachi=True
41 when: local_fish_is_hamachi|bool and sushi_tool_prepare|bool
42 - set_fact: local_run_sushi_prepare_sake=True
43 when: local_fish_is_sake|bool and sushi_tool_prepare|bool

44 - debug: var=sushi_tool_prepare
45 - debug: var=sushi_phase
46 - debug: var=local_run_sushi_prepare_sake
47 - debug: var=local_run_sushi_prepare_hamachi
48 - debug: var=local_run_upgradesushi_pm
49 - debug: var=local_run_upgradedb_mt
50 - debug: var=verbose
51 - debug: var=testify



In the run output below, I expect variable local_run_sushi_prepare_hamachi
to be False at line 23 in the play each and every time the play gets
included from the playbook. But, it doesn't. sadface.


$ ansible-playbook -i inventory/on1.ini -e verbose=false sushi_playbook.yml

PLAY [127.0.0.1]
**************************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.034) 0:00:00.034
**********
===============================================================================

PLAY [127.0.0.1]
**************************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.000) 0:00:00.035
**********
===============================================================================

TASK: [start of play]
*********************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.000) 0:00:00.035
**********
ok: [127.0.0.1] => {
"msg":
"========================================================================================"
}

msg:
========================================================================================

TASK: [debug var=fish]
********************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.011) 0:00:00.047
**********
ok: [127.0.0.1] => {
"var": {
"fish": "hamachi"
}
}

TASK: [debug var=local_run_sushi_prepare_sake]
********************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.003) 0:00:00.051
**********
ok: [127.0.0.1] => {
"var": {
"local_run_sushi_prepare_sake": "False"
}
}

TASK: [debug var=local_run_sushi_prepare_hamachi]
*****************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.003) 0:00:00.055
**********
ok: [127.0.0.1] => {
"var": {
"local_run_sushi_prepare_hamachi": "False"
}
}

TASK: [set_fact testify=True]
*************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.003) 0:00:00.058
**********
skipping: [127.0.0.1]

TASK: [set_fact sushi_phase="prepare"]
****************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.063
**********
ok: [127.0.0.1]

TASK: [set_fact sushi_phase="pickles"]
****************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.067
**********
skipping: [127.0.0.1]

TASK: [set_fact sushi_phase="ginger"]
*****************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.005) 0:00:00.072
**********
skipping: [127.0.0.1]

TASK: [set_fact sushi_phase="daikon"]
*****************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.006) 0:00:00.078
**********
skipping: [127.0.0.1]

TASK: [set_fact local_fish_is_hamachi=True]
***********************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.005) 0:00:00.084
**********
ok: [127.0.0.1]

TASK: [set_fact local_fish_is_sake=True]
**************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.005) 0:00:00.089
**********
skipping: [127.0.0.1]

TASK: [set_fact local_run_sushi_prepare_hamachi=True]
*************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.005) 0:00:00.095
**********
ok: [127.0.0.1]

TASK: [set_fact local_run_sushi_prepare_sake=True]
****************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.100
**********
skipping: [127.0.0.1]

TASK: [debug var=sushi_tool_prepare]
******************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.005) 0:00:00.106
**********
ok: [127.0.0.1] => {
"var": {
"sushi_tool_prepare": "true"
}
}

TASK: [debug var=sushi_phase]
*************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.003) 0:00:00.109
**********
ok: [127.0.0.1] => {
"var": {
"sushi_phase": "prepare"
}
}

TASK: [debug var=local_run_sushi_prepare_sake]
********************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.003) 0:00:00.113
**********
ok: [127.0.0.1] => {
"var": {
"local_run_sushi_prepare_sake": "False"
}
}

TASK: [debug var=local_run_sushi_prepare_hamachi]
*****************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.003) 0:00:00.116
**********
ok: [127.0.0.1] => {
"var": {
"local_run_sushi_prepare_hamachi": "True"
}
}

TASK: [debug var=local_run_upgradesushi_pm]
***********************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.003) 0:00:00.119
**********
ok: [127.0.0.1] => {
"var": {
"local_run_upgradesushi_pm": "local_run_upgradesushi_pm"
}
}

TASK: [debug var=local_run_upgradedb_mt]
**************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.002) 0:00:00.122
**********
ok: [127.0.0.1] => {
"var": {
"local_run_upgradedb_mt": "local_run_upgradedb_mt"
}
}

TASK: [debug var=verbose]
*****************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.002) 0:00:00.124
**********
ok: [127.0.0.1] => {
"var": {
"verbose": "false"
}
}

TASK: [debug var=testify]
*****************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.129
**********
ok: [127.0.0.1] => {
"var": {
"testify": "False"
}
}

PLAY [127.0.0.1]
**************************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.006) 0:00:00.136
**********
===============================================================================

TASK: [start of play]
*********************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.000) 0:00:00.136
**********
ok: [127.0.0.1] => {
"msg":
"========================================================================================"
}

msg:
========================================================================================

TASK: [debug var=fish]
********************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.002) 0:00:00.138
**********
ok: [127.0.0.1] => {
"var": {
"fish": "hamachi"
}
}

TASK: [debug var=local_run_sushi_prepare_sake]
********************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.143
**********
ok: [127.0.0.1] => {
"var": {
"local_run_sushi_prepare_sake": "False"
}
}

TASK: [debug var=local_run_sushi_prepare_hamachi]
*****************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.011) 0:00:00.154
**********
ok: [127.0.0.1] => {
"var": {
"local_run_sushi_prepare_hamachi": "True"
}
}

TASK: [set_fact testify=True]
*************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.158
**********
skipping: [127.0.0.1]

TASK: [set_fact sushi_phase="prepare"]
****************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.163
**********
skipping: [127.0.0.1]

TASK: [set_fact sushi_phase="pickles"]
****************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.167
**********
ok: [127.0.0.1]

TASK: [set_fact sushi_phase="ginger"]
*****************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.005) 0:00:00.172
**********
skipping: [127.0.0.1]

TASK: [set_fact sushi_phase="daikon"]
*****************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.177
**********
skipping: [127.0.0.1]

TASK: [set_fact local_fish_is_hamachi=True]
***********************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.181
**********
ok: [127.0.0.1]

TASK: [set_fact local_fish_is_sake=True]
**************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.186
**********
skipping: [127.0.0.1]

TASK: [set_fact local_run_sushi_prepare_hamachi=True]
*************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.006) 0:00:00.192
**********
skipping: [127.0.0.1]


TASK: [set_fact local_run_sushi_prepare_sake=True]
****************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.197
**********
skipping: [127.0.0.1]

TASK: [debug var=sushi_tool_prepare]
******************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.201
**********
ok: [127.0.0.1] => {
"var": {
"sushi_tool_prepare": "False"
}
}

TASK: [debug var=sushi_phase]
*************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.004) 0:00:00.205
**********
ok: [127.0.0.1] => {
"var": {
"sushi_phase": "pickles"
}
}

TASK: [debug var=local_run_sushi_prepare_sake]
********************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.003) 0:00:00.209
**********
ok: [127.0.0.1] => {
"var": {
"local_run_sushi_prepare_sake": "False"
}
}

TASK: [debug var=local_run_sushi_prepare_hamachi]
*****************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.002) 0:00:00.212
**********
ok: [127.0.0.1] => {
"var": {
"local_run_sushi_prepare_hamachi": "True"
}
}

TASK: [debug var=local_run_upgradesushi_pm]
***********************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.002) 0:00:00.215
**********
ok: [127.0.0.1] => {
"var": {
"local_run_upgradesushi_pm": "local_run_upgradesushi_pm"
}
}

TASK: [debug var=local_run_upgradedb_mt]
**************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.002) 0:00:00.218
**********
ok: [127.0.0.1] => {
"var": {
"local_run_upgradedb_mt": "local_run_upgradedb_mt"
}
}

TASK: [debug var=verbose]
*****************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.002) 0:00:00.220
**********
ok: [127.0.0.1] => {
"var": {
"verbose": "false"
}
}

TASK: [debug var=testify]
*****************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.003) 0:00:00.223
**********
ok: [127.0.0.1] => {
"var": {
"testify": "False"
}
}

PLAY RECAP
********************************************************************
Tuesday 21 July 2015 16:55:12 -0700 (0:00:00.002) 0:00:00.226
**********
===============================================================================
127.0.0.1 : ok=29 changed=0 unreachable=0 failed=0
--
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+***@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/471e830a-fcba-41ad-a090-16c4d3724be5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Kathy Allen
2015-07-23 19:21:41 UTC
Permalink
bump?
--
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+***@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/a18a929b-e1bc-4540-8314-2abaee50ed65%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Timothy Appnel
2015-07-24 14:15:45 UTC
Permalink
If I am following what behavior you are looking for (what you describe is
how includes are intended to work) your playbook includes should be
implemented as a role and then called with parameters. See if the added
namespacing protections roles have, but includes does not kicks in.

<tim/>
Post by Kathy Allen
bump?
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/a18a929b-e1bc-4540-8314-2abaee50ed65%40googlegroups.com
<https://groups.google.com/d/msgid/ansible-project/a18a929b-e1bc-4540-8314-2abaee50ed65%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 ansible-project+***@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/CAOMCme7xtNdmrrqTf5NkyvuyxrNymA%2Bzov_izrSbLoW-FTfymw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Kathy Allen
2015-07-27 18:17:24 UTC
Permalink
Thank you. However, our playbooks don't use roles. For our deployment
orchestration, it doesn't seem necessary, or at least it works well without
them.

Can anyone speak to how that variable is getting set as the playbook
executes the second include? Will this absolutely not work without roles?
Post by Timothy Appnel
If I am following what behavior you are looking for (what you describe is
how includes are intended to work) your playbook includes should be
implemented as a role and then called with parameters. See if the added
namespacing protections roles have, but includes does not kicks in.
<tim/>
--
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+***@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/6d21c77a-7b76-4e09-9d62-dde41ea05dc7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Timothy Appnel
2015-07-27 19:37:10 UTC
Permalink
I realize your are using includes. I was pointing out that you want to use
roles for the benefit of the namespacing protection that roles have and
includes do not. From what I see this is correct behavior for what you've
implemented and not a bug. A role should give you the separate variable
namespace scope you seem to be expecting. <tim/>
Post by Kathy Allen
Thank you. However, our playbooks don't use roles. For our deployment
orchestration, it doesn't seem necessary, or at least it works well without
them.
Can anyone speak to how that variable is getting set as the playbook
executes the second include? Will this absolutely not work without roles?
Post by Timothy Appnel
If I am following what behavior you are looking for (what you describe is
how includes are intended to work) your playbook includes should be
implemented as a role and then called with parameters. See if the added
namespacing protections roles have, but includes does not kicks in.
<tim/>
--
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+***@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/CAOMCme7fOihJx948YkHWizGs5CRwdFPvLXg8imP1h2VRKrbCJQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-27 20:22:56 UTC
Permalink
So your expectation is incorrect, as any play level variable modified
during the play is still modified during any subsequent part of the
play, includes are not namespaced, in a play you only have 2 scopes:
play vars and host vars, both carry forward any changes made at any
point in the play. In the case of hostvars they'll carry over to
subsequent plays also.

What you want is 2 plays that set the same default vars and you can do
include #1 in the first play and include #2 in the 2nd, then the vars
will look like what you expect.
--
Brian Coca
--
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+***@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/CAJ5XC8nLtWYRxtj0Td5FWnQ5p1HYSi_i7NE8_rQWRa1EGy6QHA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Kathy Allen
2015-07-27 23:58:43 UTC
Permalink
Thank you Brian. That's the Clue I needed. I'll rethink this...

kallen
--
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+***@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/1c26cb57-e3e6-4a19-9d7f-0ca46d5d1e32%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...