I am trying to set an Ansible variable based on the count returned by the grep command.
The code is as follows.
tasks:
- name: Check for 12.1 databases
shell: "grep -c /u1/app/oracle/product/12.1.0/dbhome_1 /etc/oratab"
register: grep_output
- name: Print
debug:
msg="grep_output.stdout"
- name: Set the variable
set_fact:
oracle_version_12_1_0: "true"
when: "grep_output.stdout > 0"
- name: Print variable
command: echo "{{ oracle_version_12_1_0 }}"
The code errors out as follows.
PLAY [oradbpoc1] ***********************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************
ok: [oradbpoc1]
TASK [Check for 12.1 databases] *********************************************************************************************************
fatal: [oradbpoc1]: FAILED! => {"changed": true, "cmd": "grep -c /u1/app/oracle/product/12.1.0/dbhome_1 /etc/oratab", "delta": "0:00:00.003425", "end": "2020-08-06 18:33:04.476299", "msg": "non-zero return code", "rc": 1, "start": "2020-08-06 18:33:04.472874", "stderr": "", "stderr_lines": [], "stdout": "0", "stdout_lines": ["0"]}
PLAY RECAP *********************************************************************************************************
oradbpoc1 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
PS: Also, is there a way to assign a default value ("false") to the oracle_version_12_1_0 variable?
Thanks
FR