Just getting my head around Ansible. Just trying to see what I am doing wrong or what is a better way to reach my goal.
Task :
My goal here is to update a file /proc/sys/vm/overcommit_memory. I want to put the value 0 into it. I also want the value it was before I changed it to be logged and displayed to me in output in case I need to rollback.
The below works fine but wanted to see if there are better ways to do it.
---
- hosts: "{{ target }}"
gather_facts: yes
become: yes
tasks:
- name: "update overcommit value on {{ target }} ..."
shell: |
echo "the value was "
cat /proc/sys/vm/overcommit_memory
echo 0 > /proc/sys/vm/overcommit_memory
echo "the value now is "
cat /proc/sys/vm/overcommit_memory
register: rc
become: true
become_user: root
- debug:
var: rc.stdout_lines
Thanks in advance
Regarding your example it is recommend to use an Ansible module,
sysctl.Since Ansible is a Configuration Management it makes sure that the above task is idempotent and the value is available and set after. An "reporting" could than just be like
Because of
you will get already the information if the key was there and the value not
0if.For a simple reporting of the current value set you could use a reporting task like
You may need to adjust Defining failure to catch cases if the key and therefore the file does not exists or if access rights are missing.
Further Documentation
Since it might be that Ansible facts are already contain the value