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?


You could combine it into one task:

- name: Set global version Ruby
  shell: /usr/local/bin/rbenv global; /usr/local/bin/rbenv global 2.2.1
  register: old_ruby_version
  changed_when: old_ruby_version.stdout != 2.2.1

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.