1

I'm pretty new in ansible and by now can't figure out how to make an idempotent task. E.g. file. It simply doesn't have creates or removes. In command module it does have creates, but whenever I run the provisioner and the file (symbolic link) does exist, the ansible still marks the command as "ok", not "skipped".

Here's command module example

- name: Firefox | link
  command: ln -s /opt/firefox/firefox /usr/local/bin/firefox creates=/usr/local/bin/firefox

Always gets executed.

How do I get sure that the command won't be executed >=2 times given the node's state is already accomplished by 1st run?

Thank you!

nkamm
  • 113
  • 7

1 Answers1

3

That should work.

Actually i checked and it mostly works (see later), here's the complete playbook i used for testing it (not using firefox but the concept is the same):

---
- hosts: localhost
  tasks:
  - name: Test | link & idempotency
    command: ln -s /tmp/ans/from /tmp/ans/to creates=/tmp/ans/to

Details:

  • When the link is not present it get's created correctly
  • When the link is present nothing get's changed. Even when the link points to the wrong file though.

You get a warning though:

[WARNING]: Consider using file module with state=link rather than running ln

If you want to be sure the link is correct, and change it only when needed (thus idempotent) the "correct" way, as for ansible's warning is to use the file module, here's an example:

---
- hosts: localhost
  tasks:
  - name: Test | link & idempotency
    file: src=/tmp/ans/from dest=/tmp/ans/to state=link
Fredi
  • 2,257
  • 10
  • 13