ansible win_shell module to run 2 seperate commands

89 views Asked by At

I am trying to have custom print out based on exit status message stored registered like below in ansible role called from main playbook.

- name: Check exit status
  win_shell: findstr /B failed_update_count {{ansible_workdir}}\\super_install.log | tail -1 | gawk '{print $2}'
  register: exitStatus
  
- debug:
    msg: "{{ exitStatus.stdout }}"

- name: Update ExitStOut info
  win_shell: |
    if ({{ exitStatus.stdout }}=="0") {
    echo "Install Succesful"
    }
    if ({{ exitStatus.stdout }}!="0") {
    echo "Install FAILED"
    }
  register: ExitStOut

- debug:
    msg: "{{ ExitStOut.stdout }}"

The first part worked ok & prints 0, but having 2 if statement is leading to below error. Kindly let me know if there is better way to group if and check registered variable value?.

TASK [rl_wininstall : Check exit status] ******************************************************************************
changed: [<MachineName>]

TASK [rl_wininstall : debug] ***************************************************************************************************
ok: [<MachineName>] => {
    "msg": "**0**\n"
}

TASK [rl_wininstall : Update ExitStOut info] ******************************************************************************
fatal: [<MachineName>]: FAILED! => {"changed": true, "cmd": "if (0\n==\"0\") {\necho \"Install Succesful\"\n}\nif (0\n!=\"0\") {\necho \"Install FAILED\"\n}", "delta": "0:00:00.865954", "end": "2023-05-16 07:49:46.941573", "msg": "non-zero return code", "rc": 1, "start": "2023-05-16 07:49:46.075619", "stderr": "At line:2 char:1\r\n+ ==\"0\") {\r\n+ ~~~~~\r\nMissing closing ')' after expression in 'if' statement.\r\nAt line:2 char:6\r\n+ ==\"0\") {\r\n+      ~\r\nUnexpected token ')' in expression or statement.\r\nAt line:6 char:1\r\n+ !=\"0\") {\r\n+ ~~~~~\r\nMissing closing ')' after expression in 'if' statement.\r\nAt line:6 char:2\r\n+ !=\"0\") {\r\n+  ~\r\nMissing expression after unary operator '!'.\r\nAt line:6 char:6\r\n+ !=\"0\") {\r\n+      ~\r\nUnexpected token ')' in expression or statement.\r\nAt line:6 char:1\r\n+ !=\"0\") {\r\n+ ~\r\nThe assignment expression is not valid. The input to an assignment operator must be an object that is able to accept \r\nassignments, such as a variable or a property.\r\n    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException\r\n    + FullyQualifiedErrorId : MissingEndParenthesisAfterStatement", "stderr_lines": ["At line:2 char:1", "+ ==\"0\") {", "+ ~~~~~", "Missing closing ')' after expression in 'if' statement.", "At line:2 char:6", "+ ==\"0\") {", "+      ~", "Unexpected token ')' in expression or statement.", "At line:6 char:1", "+ !=\"0\") {", "+ ~~~~~", "Missing closing ')' after expression in 'if' statement.", "At line:6 char:2", "+ !=\"0\") {", "+  ~", "Missing expression after unary operator '!'.", "At line:6 char:6", "+ !=\"0\") {", "+      ~", "Unexpected token ')' in expression or statement.", "At line:6 char:1", "+ !=\"0\") {", "+ ~", "The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept ", "assignments, such as a variable or a property.", "    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException", "    + FullyQualifiedErrorId : MissingEndParenthesisAfterStatement"], "stdout": "", "stdout_lines": []}
0

There are 0 answers