Skipped tasks and register variables

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.

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

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

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

Take care,

Lorin

+1.

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 :slight_smile:

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.

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.

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

Tin Tvrtković wrote:

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}')

One of the reasons to use
  when_set: ${postgresql_just_initialized}
instead.

Daniel

Right, that works. Thanks :slight_smile: