# Skipped tasks and register variables

**URL:** <https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232>\
**Category:** Ansible Project\
**Created:** [January 22, 2013, 1:34pm UTC](https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232 "2013-01-22T13:34:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tin\_Tvrtkovic](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@Tin\_Tvrtkovic](https://forum.ansible.com/u/Tin_Tvrtkovic)\
**Post date:** [January 22, 2013, 1:34pm UTC](https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232/1 "2013-01-22T13:34:25Z")

</div>

Hi everyone,

I’m trying to create a playbook to set up my Postgres instances. At a certain point, I’d like to make Postgres do an initdb. This is what I have so far:

- name: ensure postgresql 9.2 is initialized  
shell: service postgresql-9.2 initdb creates=/var/lib/pgsql/9.2/data  
register: postgresql\_initialized

If I’ve just run this, and Postgres just happens to be up (maybe not likely since it can’t start without it, but I’d still like my playbooks to be nice and idempotent), I’d like to stop it now and let a subsequent task make sure it’s up. Now I do:

- name: stop postgresql because of fresh initialization  
service: name=postgresql-9.2 state=stopped  
only\_if: ${postgresql\_initialized}

which works if I’ve just initialized it, but doesn’t if the first task skips. I’ve worked around it by setting a global var of postgresql\_initialized to False, but I’d rather not pollute my global vars like this. I’ve tried using is\_set, but I can’t get it to work for both cases.

Any tips on doing something like this? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [January 22, 2013, 2:09pm UTC](https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232/2 "2013-01-22T14:09:23Z")

</div>

Why not just unconditionally set the service to the stopped state?

&nbsp;&nbsp;- name: ensure postgresql is stopped in case initialization started it  
&nbsp;&nbsp;&nbsp;&nbsp;service: name=postgresql-9.2 state=stopped

There's no harm in doing this if the service isn't running.

Take care,

Lorin

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 22, 2013, 2:13pm UTC](https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232/3 "2013-01-22T14:13:38Z")

</div>

+1.

---

<div class="post-metadata">

**Author:** ![Tin\_Tvrtkovic](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@Tin\_Tvrtkovic](https://forum.ansible.com/u/Tin_Tvrtkovic)\
**Post date:** [January 22, 2013, 2:51pm UTC](https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232/4 "2013-01-22T14:51:05Z")

</div>

Thanks for the reply.

Ideally, if the database has already been initialized I’d just skip over all the steps involved in that and continue with other playbook items without restarting PG for no reason. I mean, I can see your point; I’m not likely to run the playbook on production machines in the middle of the day so just stopping it and starting it back up wouldn’t be a problem, but somehow it feels better to just skip unnecessary work 🙂

In any case, I find the only\_if: is\_set() condition behaviour surprising: I get an exception if I try it with a register variable? Maybe I’ll try ‘when\_unset’ to work around.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 22, 2013, 2:59pm UTC](https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232/5 "2013-01-22T14:59:45Z")

</div>

> In any case, I find the only\_if: is\_set() condition behaviour surprising: I  
> get an exception if I try it with a register variable? Maybe I'll try  
> 'when\_unset' to work around.

Really? Please paste the TB and the cause of it.

---

<div class="post-metadata">

**Author:** ![Tin\_Tvrtkovic](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@Tin\_Tvrtkovic](https://forum.ansible.com/u/Tin_Tvrtkovic)\
**Post date:** [January 22, 2013, 3:14pm UTC](https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232/6 "2013-01-22T15:14:09Z")

</div>

In my playbook:

- name: ensure postgresql 9.2 is initialized  
shell: service postgresql-9.2 initdb creates=$postgresql\_conf\_dir  
register: postgresql\_just\_initialized

- name: stop postgresql because of re-initialization  
service: name=postgresql-9.2 state=stopped  
only\_if: is\_set(‘${postgresql\_just\_initialized}’)

Stdout:

TASK: [stop postgresql because of fresh initialization] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [X.X.X.X] =\> Traceback (most recent call last):  
File “/home/tin/ansible/local/lib/python2.7/site-packages/ansible/runner/ **init**.py”, line 236, in \_executor  
exec\_rc = self.\_executor\_internal(host)  
File “/home/tin/ansible/local/lib/python2.7/site-packages/ansible/runner/ **init**.py”, line 292, in \_executor\_internal  
return self.\_executor\_internal\_inner(host, self.module\_name, self.module\_args, inject, port)  
File “/home/tin/ansible/local/lib/python2.7/site-packages/ansible/runner/ **init**.py”, line 352, in \_executor\_internal\_inner  
if not getattr(handler, ‘BYPASS\_HOST\_LOOP’, False) and not utils.check\_conditional(conditional):  
File “/home/tin/ansible/local/lib/python2.7/site-packages/ansible/utils/ **init**.py”, line 146, in check\_conditional  
return eval(conditional.replace(“\n”, “\n”))  
File “”, line 1  
is\_set(‘{u’changed’: True, u’end’: u’2013-01-22 16:10:17.354809’, u’stdout’: u’Initializing database: \x1b[60G[\x1b[0;32m OK \x1b[0;39m]‘, u’cmd’: u’service postgresql-9.2 initdb ‘, u’rc’: 0, u’start’: u’2013-01-22 16:10:14.408764’, u’stderr’: u’‘, u’delta’: u’0:00:02.946045’, ‘invocation’: {‘module\_name’: u’shell’, ‘module\_args’: u’service postgresql-9.2 initdb creates=/var/lib/pgsql/9.2/data’}, ‘stdout\_lines’: [u’Initializing database: \x1b[60G[\x1b[0;32m OK \x1b[0;39m]‘]}’)  
^  
SyntaxError: invalid syntax

FATAL: all hosts have already failed – aborting

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 22, 2013, 3:19pm UTC](https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232/7 "2013-01-22T15:19:57Z")

</div>

Tin Tvrtković wrote:

> In my playbook:
> 
> &nbsp;&nbsp;- name: ensure postgresql 9.2 is initialized  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: service postgresql-9.2 initdb creates=$postgresql\_conf\_dir  
> &nbsp;&nbsp;&nbsp;&nbsp;register: postgresql\_just\_initialized
> 
> &nbsp;&nbsp;- name: stop postgresql because of re-initialization  
> &nbsp;&nbsp;&nbsp;&nbsp;service: name=postgresql-9.2 state=stopped  
> &nbsp;&nbsp;&nbsp;&nbsp;only\_if: is\_set('${postgresql\_just\_initialized}')

One of the reasons to use  
&nbsp;&nbsp;when\_set: ${postgresql\_just\_initialized}  
instead.

Daniel

---

<div class="post-metadata">

**Author:** ![Tin\_Tvrtkovic](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@Tin\_Tvrtkovic](https://forum.ansible.com/u/Tin_Tvrtkovic)\
**Post date:** [January 22, 2013, 3:31pm UTC](https://forum.ansible.com/t/skipped-tasks-and-register-variables/12232/8 "2013-01-22T15:31:04Z")

</div>

Right, that works. Thanks 🙂
