Template error while templating string: unexpected char u - Ansible - ansible

When executing a playbook to run a command in a remote host and pass the output using shell, getting below error.
fatal: [master1]: FAILED! => {} MSG: template error while templating
string: unexpected char u'a' at 4. String:
{{54aa7fda16833bff8358b6bd1157df2d9caa26b2}}
Below is my playbook content
- name: 'Play1'
hosts: master
tasks:
- name: 'Execute command'
shell: ''sh generate_ticket.sh" #command to generate ticket
register: shell_output
- name: 'debug shell_output'
debug:
var="{{ shell_output.stdout | from_yaml }}"
When I try the same with msg and don't try to filter then the output is printed without any error. However I prefer to use var as it suits best for my further requirements. If the ticket number is a different string I do not face any issues. Please see below:
Output:
ok: [master1] => {}
MSG:
54aa7fda16833bff8358b6bd1157df2d9caa26b2
Playbook :
- name: 'Play1'
hosts: master
tasks:
- name: 'Execute command'
shell: ''sh generate_ticket.sh" #command to generate ticket
register: shell_output
- name: 'debug shell_output'
debug: msg="{{ shell_output.stdout | from_yaml }}"

It seems to work when I put single quotes around shell_output.stdout
var="{{ 'shell_output.stdout' | from_yaml}}"
Let me know if anybody has a better fix than this.

use !unsafe
enter link description here

Related

Registering a result when calling a role/script inside a playbook

I am working with ansible in Linux and have this playbook, which calls a role which passes the script name to run with an argument:
- hosts: localhost
gather_facts: no
ignore_errors: True
roles:
- { role: execute-my-script,
myscript_script_name: script.py,
myscript_args: "{{ my_var }}" }
- hosts: localhost
tasks:
- name: Status
ansible.builtin.debug:
msg: "WARNING: THERE IS AN ERROR!!!"
when:
- result is defined
- result.stdout is search("This resulted in error")
This is working when I run this playbook, and it will execute the script.py with arguments my_var.
This is my main.yml (execute-my-script):
---
- name: Execute script
ansible.builtin.command: "script.sh
{{ argument }}"
register: result
tags: execute-my-script
failed_when: "'This resulted in error' in result.stderr"
- set_fact: {"result": "{{ result }}"}
Note: This resulted in error comes from the script.sh.
I want to register the result of this task and then add another task which will do something if the result.stdout has this : "This resulted in error".
Although, I cannot register the result using register: result.
So I registered result inside main.yml task. But when I try to display an output in the playbook its not working.
Any ideas how to register the result of the given task in the playbook?

Is it possible to display stdout in dialog-box or better human readable format in ansible?

I am new to Ansible and my play looks like this:
- name: "Spark Submit Command"
shell: "{{ sparkCommand }}"
register: spark_output
- debug: msg="{{ spark_output.stdout }}"
I have around 60 lines in my spark_output.stdout and getting the output as below:
ok: [DHADLX110] => {
"msg": "Line1\nLine2\nLine3...........Line.."
Is it possible to print these line by line or in a proper dialog box? Something similar to below format:
Line1
Line2
.
.
.
Line60
When you register command output, Ansible will give you stdout and stdout_lines.
If you change your debug task to:
- debug:
var: spark_output
You will see that it also returns stdout_lines. So instead of spark_output.stdout use:
- debug:
msg: "{{ spark_output.stdout_lines }}"

How to extract the exact output from stdout.lines in ansible

My Ansible Playbook:
#Tag --> B.6 -->
- name: Change the Security Realm to CustomRealm from ManagementRealm
command: /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect--command="/core-service=management/management-interface=http-interface:read-attribute(name=security-realm)"
register: Realm
- debug:
msg: "{{ Realm.stdout_lines }}"
The output for the above command in the message is as follows:
ok: [342f2f7bed8e] => {
"msg": [
"{",
" \"outcome\" => \"success\","
" \"result\" => \"ManagementRealm\"",
"}"
]
}
is there a way to just exact \"result\" => \"ManagementRealm\"".
I tried using the
Realm.stdout_lines.find('result')
but that fails, AWk & grep commands doesn't seem to be working here.
Any thoughts is greatly appreciated.
Thak you
I think there are a few ways you could handle this.
1) Grep the output before it gets to Ansible:
# Note the change of 'command' to 'shell'
- name: Change the Security Realm to CustomRealm from ManagementRealm
shell: /jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect--command="/core-service=management/management-interface=http-interface:read-attribute(name=security-realm)" | grep -o 'result.*'
register: Realm
2) If the output from the source script is always 4 lines long, you can just grab the 3rd line:
# List indexes start at 0
- debug:
msg: "{{ Realm.stdout_lines[2] | regex_replace('^ *(.*$)', '\\1') }}"
3) The nicest way if you have an option to modify jboss-cli.sh, would be to get the jboss-cli.sh to output valid JSON which can then be parsed by Ansible:
# Assuming jboss-cli.sh produces {"outcome": "success", "result": "ManagementRealm"}
- set_fact:
jboss_data: "{{ Realm.stdout | from_json }}"
- debug:
var: jboss_data.result

Ansible Print IOS output on string match

I'm trying to use Ansible to hit a bunch (100+) Cisco Catalyst switches and check if they have a certain line card installed. Via SSH, this can be done with the "sh mod" command. I want to parse the output of that command in a playbook and then show the output of the command if a certain string matches. Right now with the playbook below I get the following error:
fatal: [redacted-hostname]: FAILED! => {"failed": true, "msg": "The
conditional check 'showmod | search(\"4548\")' failed. The error was:
Unexpected templating type error occurred on ({% if showmod |
search(\"4548\") %} True {% else %} False {% endif %}): expected
string or buffer\n\nThe error appears to have been in
'/etc/ansible/playbooks/linecard-4548.yaml': line 22, column 5, but
may\nbe elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n\n - debug:
\"msg='4548 Card Found'\"\n ^ here\n"}
Current playbook code:
---
- hosts: redacted-hostname
gather_facts: yes
connection: local
tasks:
- name: SYS | Define provider
set_fact:
provider:
host: "{{ inventory_hostname }}"
username: redacted-user
password: redacted-password
- name: IOS | Get Module List
ios_command:
provider: "{{ provider }}"
commands:
- sh mod | inc 4548
register: showmod
- debug: "msg='4548 Card Found'"
when: showmod.stdout | search("/4548/")
I've tried the when in the debug with and without the .stdout to no avail. I've done some research and the error I'm getting usually occurs when, in my case, showmod is undefined but it definitely is. If I replace the debug with the following snippet, the playbook runs fine but of course it'll print the output for every switch which isn't what I want.
- name: IOS | Show Output
debug:
var: showmod
Any suggestions?
ios_command returns stdout as list and stdout_lines as list of lists (whereas command module return stdout as string and stdout_lines as list).
So in your case, you may want to try:
- debug: "msg='4548 Card Found'"
when: showmod.stdout | join(" ") | search("/4548/")

Ansible: how to register variable in uri module?

This is my code or checking wheter the website is valid and running:
- hosts: master
become: true
tasks:
- name: "Checking server availibility"
uri:
url: http://www.google.pl
timeout: 5
register: command_result
ignore_errors: yes
- debug: msg= "{{ command_result }}"
So command_result variable always returns this output regardless of the website availability:
ok: [Centos.v1] => {
"changed": false,
"msg": "Hello world!"
}
I would like to know if the first task was succesful or not but i can't do that when the output of registered variable is always the same.
How to setup my variable properly to read the return code?
and also
Is there some other way to check if an error occured in the previous task?
This has nothing to do with registering.
Remove the space character after msg=, otherwise you pass an empty string to the debug module and it prints Hello World! as a result (its default behaviour).
Better yet, use var parameter and YAML syntax:
- debug:
var: command_result

Resources