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!

0

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.