I have an Ansible task where I set the version of Ruby used globally on my system.
- name: Set Ruby global version
shell: /usr/local/bin/rbenv global 2.2.1
The problem is that this task always has the status of changed
. I want it to show changed
only if it actually has changed, otherwise ok
. This is the best solution I could come up with:
- name: Get current global version Ruby
shell: /usr/local/bin/rbenv global"
register: current_ruby_version
changed_when: False
- name: Set Ruby global version
shell: /usr/local/bin/rbenv global 2.2.1
changed_when: current_ruby_version.stdout != 2.2.1
This is going to be a pattern that I'm going to use a lot (check existing value, set value, see if it changed) so this is a bit onerous.
Is there a better way to do this?