ansible --version
ansible 2.5.2
molecule --version
molecule, version 2.14.0
Molecule installation method (one of):
Ansible installation method (one of):
In Ansible role I have task like:
- name: restart sshd service
systemd:
name: sshd
state: restarted
I expect that idempotence step will end without fail, but get an error, because of ansible task always returns "changed" state when restarts service.
--> Scenario: 'default'
--> Action: 'idempotence'
ERROR: Idempotence test failed because of the following tasks:
* [ansible-server-common] => server-common : restart sshd service
An error occured during the test sequence. Cleaning up
Is there some hack to get idempotence work?
Maybe: changed_when: False
Should the service always be restarted with every execution of the role? Seems like the role isn't idempotent, simply b/c it will always restart the service. Instead why not make the task a handler, and only restart when triggered?
It seems, that I've not completely understood handlers behaviour. Reading ansible docs clarified some things and @retr0h is right, my role isn't idempotent.
Thanks for help and such great instrument like molecule!
would you guys contribute on how this can be achieved?
@raolivei This document has you covered: https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#handlers-running-operations-on-change
Basically, it lets you define "actions" (=handlers) which you can run when a particular file has changed. This seems to be very useful to me; haven't tested it myself but it definitely seems like the way to go with these kind of things.
We also have molecule-notest-idempotence tag to get out jail now :+1:
@decentral1se
We also have
molecule-notest-idempotencetag to get out jail now +1
Good point. For reference: https://molecule.readthedocs.io/en/stable/configuration.html#id22
(but in general it's of course better to try and design your Ansible roles to be idempotent, as far as is possible.)
"get out of jail" = when Ansible is fighting you (bugs, issue tracker disagreements, etc.)
:)
"get out of jail" = when Ansible is fighting you (bugs, issue tracker disagreements, etc.)
True that. :+1: :laughing:
@decentral1se But it's actually named molecule-idempotence-notest. I tried the other name but it didn't work. :smirk:
Basically use service/systemd task as a handler and run it using notify only when (for example) configuration file changes.
Most helpful comment
@decentral1se But it's actually named
molecule-idempotence-notest. I tried the other name but it didn't work. :smirk: