Help Using an .stdout as a VAR Used in a WHEN Condition

41 views
Skip to first unread message

Symian Army

unread,
Jan 4, 2024, 10:05:56 AM1/4/24
to Ansible Project

Hello. New to this wonderful Ansible thing. I am a database guy. I have an Oracle process that spits out a file that looks like this (but the value could be different at each run):

[oracle@lnx0016 scripts]$ more /tmp/impact.txt -12268.06

Then, within the same playbook, Ansible playbook parses it, and if the value is a negative (less than 0), it is supposed to kick off a whole another block.

- name: opatch_direct | Export the Post-change Impact Score for for {{ db_name }} shell: 'cat /tmp/impact.txt' register: impact_score_literal - name: opatch_direct | GO / NOGO decision for {{ oracle_home }} and {{ db_name }} block: ... when: "'-' not in impact_score_literal.stdout"

I also tried a less than zero condition, but ignores it.
I think there are two problems:

  1. The number " -12268.06" has a heading empty space
  2. The data type may be wrong? May be I should use something other than “cat”?

Here is the error

TASK [opatch_direct | Stop database ORCLCDB before rolling back 35740258 for /opt/oracle/product/21c/dbhome_1] ************************************************************************************************************************ task path: /home/oracle/ansible/playbooks/opatch_direct.yml:185 fatal: [lnx006]: FAILED! => msg: |- The conditional check ''-' not in impact_score_literal.stdout' failed. The error was: error while evaluating conditional ('-' not in impact_score_literal.stdout): 'dict object' has no attribute 'stdout' The error appears to be in '/home/oracle/ansible/playbooks/opatch_direct.yml': line 185, column 11, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - name: opatch_direct | Stop database {{db_name}} before rolling back {{ patch_id }} for {{ oracle_home }} ^ here We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance: with_items: - {{ foo }} Should be written as: with_items: - "{{ foo }}" PLAY RECAP **************************************************************************************************************************************************************************************************************************** lnx006 : ok=17 changed=12 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0

Here is the value of the .stdout for the condition check:

changed: [lnx006] => changed=true cmd: cat /tmp/impact.txt delta: '0:00:00.004104' end: '2023-12-26 15:38:15.595984' invocation: module_args: _raw_params: cat /tmp/impact.txt _uses_shell: true argv: null chdir: null creates: null executable: null removes: null stdin: null stdin_add_newline: true strip_empty_ends: true warn: true rc: 0 start: '2023-12-26 15:38:15.591880' stderr: '' stderr_lines: <omitted> stdout: ' -12268.06' stdout_lines: <omitted>

What I find interesting is that the condition works ONLY with an == sign, not <>. That value , if it matches the /tmp/impact.txt contents, works, meaning Ansible keeps rolling, follows the condition as it should. But that is not what I need. I need it to kick off the block only if the number is negative.

Please help
Thanks
Symian

Todd Lewis

unread,
Jan 9, 2024, 10:05:09 PM1/9/24
to ansible...@googlegroups.com, uto...@gmail.com
$ cat symianarmy01.yml
---
# symianarmy01.yml
- name: Reading number from a file
  hosts: localhost
  gather_facts: false
  vars:
    fname: /tmp/impact.txt
    content: '  -12268.06'
  tasks:
    - name: Create our temporary file
      ansible.builtin.copy:
        content: '{{ content }}'
        dest: '{{ fname }}'

    - name: Read integer from /tmp/impact.txt
      ansible.builtin.shell: |
        read impact < "{{ fname }}"
        echo "$impact"
      args:
        executable: /bin/bash
      register: impact

    - name: Is impact less than zero
      ansible.builtin.debug:
        msg: 'Test: {{ impact.stdout_lines | first | float < 0 }}'

$ ansible-playbook -v symianarmy01.yml
Using /etc/ansible/ansible.cfg as config file

PLAY [Reading number from a file] ***************************

TASK [Create our temporary file] *****************************
ok: [localhost] => changed=false 
  checksum: c5aea891bcfc6021ef0c26a053039b72aef67c30
  dest: /tmp/impact.txt
  gid: 12428
  group: utoddl
  mode: '0664'
  owner: utoddl
  path: /tmp/impact.txt
  secontext: unconfined_u:object_r:user_tmp_t:s0
  size: 11
  state: file
  uid: 12428

TASK [Read integer from /tmp/impact.txt] *********************
changed: [localhost] => changed=true 
  cmd: |-
    read impact < "/tmp/impact.txt"
    echo "$impact"
  delta: '0:00:00.002843'
  end: '2024-01-09 21:59:56.534851'
  msg: ''
  rc: 0
  start: '2024-01-09 21:59:56.532008'
  stderr: ''
  stderr_lines: <omitted>
  stdout: '-12268.06'
  stdout_lines: <omitted>

TASK [Is impact less than zero] *****************************
ok: [localhost] => 
  msg: 'Test: True'

PLAY RECAP **************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e4eb868d-54da-4c86-92c8-352f6c023086n%40googlegroups.com.

-- 
Todd
Reply all
Reply to author
Forward
0 new messages