Diffculty with registering the script output

23 views
Skip to first unread message

itssi...@gmail.com

unread,
Oct 23, 2018, 10:23:27 PM10/23/18
to Ansible Project
I am trying to execute a shut down script and Shut down remote server upon successful execution of the script . I do not want to shutdown the server when the script fails.
I did write the play book below, but it is throwing errors. can some one help me on this?

- hosts: all
user: test
become: yes
connection: ssh
gather_facts: false
ignore_errors: yes

tasks:
- name: execute stop.sh script
shell: /home/test/stop.sh
register: script_output

- debug:
var: script_output

- name: shutdown the server
command: shutdown -h now
when: "script_output.rc !=0"

======================================================

here is the output for script_output
"script_output": {
"changed": true,
"cmd": "/home/test/stop.sh",
"delta": "0:00:00.004297",
"end": "2018-10-23 18:15:13.564368",
"failed": true,
"msg": "non-zero return code",
"rc": 1,
"start": "2018-10-23 18:15:13.560071",
"stderr": "",
"stderr_lines": [],
"stdout": "hello world",
"stdout_lines": [
"hello world"
]
}
}

========================================
below is the error
fatal: [10.x.x.x]: FAILED! => {"msg": "The conditional check 'script_output.find('rc'') != 0' failed. The error was: template error while templating string: unexpected char u\"'\" at 29. String: {% if script_output.find('rc'') != 0 %} True {% else %} False {% endif %}\n\nThe error appears to have been in '/home/vadsit01/playbooks/include/errorhandling/error.yml': line 17, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: shutdown\n ^ here\n"}

can some one help me with it please?

Vladimir Botka

unread,
Oct 24, 2018, 1:21:46 AM10/24/18
to Ansible Project

Remove the quotation marks. The conditionals are expanded automatically. 
 

        when: "scrtipt_output.rc !=0"

 ...

The error was: template error while templating string: unexpected char u\"'\" at 29


Alternatively you might want to use:

    when: not scrtipt_output.failed

itssi...@gmail.com

unread,
Oct 24, 2018, 9:30:13 AM10/24/18
to Ansible Project
Thanks alot!
when: not scrtipt_output.failed  

This is worked for me!!!
Reply all
Reply to author
Forward
0 new messages