Michal Krasny
Michal Krasny

Reputation: 5916

How tu run task in ansible only when a certain service is running?

I'm trying to backup Postgres DB by a pg_dump, but this can be done only if the service is running.

Example:

    - name: Backup DB to Amazon S3
      when: ansible_facts.services["postgresql.service"] is defined
      become: no
      shell: pg_dump -U postgres -d my_db -Z 9  | aws s3 cp --storage-class STANDARD_IA --sse aws:kms - s3://my_bucket/{{inventory_hostname}}/dump-`date +%Y-%m-%d-%H-%M-%S`.sql.gz

This checks that service if defined, how to check if the service is running?

Upvotes: 0

Views: 167

Answers (1)

OMS
OMS

Reputation: 31

The ansible_facts.services module you are calling has a status variable. Have you tried this?

when: ansible_facts.services["postgresql.service"].state == started

Upvotes: 2

Related Questions