Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store grep command result in ansible variable?

Tags:

ansible-2.x

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

like image 247
user13708337 Avatar asked Oct 16 '25 06:10

user13708337


1 Answers

With regards to the Fail condition, it is because a grep -c PATTERN FILE will return a non-zero exit code if the PATTERN is not found in a file.

Quick example:

# grep -c root /etc/passwd; echo $?
2
0

# grep -c rootNO /etc/passwd; echo $?
0
1

So, I believe there are two options:

  1. Just base upon the exit code
  2. Set to ignore the error, and deal with the value

Since the count should be sufficient, Option 2 is shown here:

- hosts: localhost
  tasks:
    - name: get value
      shell:
        cmd: grep -c root /etc/passwd
      register: grep_output
      ignore_errors: yes
      changed_when: false

    - name: Print
      debug:
        msg: "{{ inventory_hostname }}: '{{ grep_output.stdout }}'"

    - name : Set the variable
      set_fact:
        oracle_version_12_1_0: "{% if grep_output.stdout == '0' %}false{% else %}true{% endif %}"

    - name: Print
      debug:
        msg: "val: {{ oracle_version_12_1_0 }}

Adjust root to rootNo (for example) to see the difference in true/false.

Also, depending upon exactly what you are wishing to do, it is also possible to register the variable as indicated in the get value, and then use it in a when check (i.e., avoid setting the variable):

- name : task name
  ...
  when: grep_output.stdout != '0'

Whether this latter approach is helpful or not depends on the overall use case.

like image 185
KevinO Avatar answered Oct 18 '25 22:10

KevinO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!