variable set to a default value in play vars unexpectedly becomes not the default value

86 views
Skip to first unread message

Kathy Allen

unread,
Jul 21, 2015, 8:04:07 PM7/21/15
to Ansible Project
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



Kathy Allen

unread,
Jul 23, 2015, 3:21:41 PM7/23/15
to Ansible Project, kal...@gmail.com
bump?


Timothy Appnel

unread,
Jul 24, 2015, 10:16:29 AM7/24/15
to ansible...@googlegroups.com, kal...@gmail.com
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/>


On Thu, Jul 23, 2015 at 3:21 PM, Kathy Allen <kal...@gmail.com> wrote:
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-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/a18a929b-e1bc-4540-8314-2abaee50ed65%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kathy Allen

unread,
Jul 27, 2015, 2:17:24 PM7/27/15
to Ansible Project, ti...@ansible.com
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?

Timothy Appnel

unread,
Jul 27, 2015, 3:37:54 PM7/27/15
to Kathy Allen, Ansible Project
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/>

Brian Coca

unread,
Jul 27, 2015, 4:23:02 PM7/27/15
to Ansible Project, Kathy Allen
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

Kathy Allen

unread,
Jul 27, 2015, 7:58:43 PM7/27/15
to Ansible Project, bc...@ansible.com
Thank you Brian. That's the Clue I needed. I'll rethink this...

kallen
Reply all
Reply to author
Forward
0 new messages