0

I am quite new to YAML and cannot find the solution to this error. My main.yml is setting the ansible_host and actions variables and calling the mail.yml.

Mail.yml
- name: set variables
  set_fact:
    currentDateTime: "{{lookup('pipe','date \"+%Y-%m-%d %H:%M\"')}}

- name: send email
  mail:
    host: smtp.domain.com
    port: 25
    to: John Doe <john.doe@gmail.com>
    from: {{ ansible_host }}@domain.com
    subject: Please perform {{ actions }} on {{ currentDateTime }}
    body: Run {{ ansible_host }}

There are no tabs (only spaces) and I have tried adding quotes "" to the values under mail but still getting the:

"Syntax error while loading YAML.\n found character that cannot start any token\n\nThe error appears to be in 
'mail.yml': line 11, column 33. The offending line appears to be:\n\n  to: John Doe <john.doe@gmail.com>."

Any assistance would be greatly appreciated.

Thanks.

Mark-cet
  • 1
  • 1
  • 1
  • 1
    Hi, have you checked e.g. [this question](https://stackoverflow.com/questions/19109912/do-i-need-quotes-for-strings-in-yaml) ? I would suggest that characters < and > are the problem :-) – Maurice Klimek Jan 21 '20 at 13:48
  • 1
    The first line of your file is not valid (but I guess it is a copy/paste error). Your error is focusing on line 11 but the content returned does not match your file. So no one can reliably analyze your current problem to see where is the error and being sure it is not caused by something we don't see. But hint: you have a missing double quote in your first set_fact task. Please double check all this, edit your question, and paste the exact file and corresponding output. – Zeitounator Jan 21 '20 at 14:52
  • Hi Mark-cet, welcome to SO. In addition to all the other great comments, don't forget that you cannot have unquoted leading braces like you have with your `from:` line, because YAML thinks a `{` is part of an object literal, but unfortunately jinja2 uses that character in its interpolation syntax. But I think Zeitounator is right about the missing `"` being the majority of your problem. Good luck! – mdaniel Jan 21 '20 at 14:59
  • @mdaniel Jinja processes the file before the YAML parser sees it, the Jinja-syntax (`{{` / `}}`) will be gone by then. – flyx Jan 21 '20 at 15:26
  • @flyx that is **demonstrably** untrue; `printf '\n- hosts: all\n tasks:\n - debug:\n msg: {{ ansible_version }}\n' > pb.yml; ansible-playbook pb.yml` – mdaniel Jan 22 '20 at 04:20

1 Answers1

0

You're missing a closing " at

currentDateTime: "{{lookup('pipe','date \"+%Y-%m-%d %H:%M\"')}}

Another problem may be this line:

from: {{ ansible_host }}@domain.com

If {{ ansible_host }} evaluates to the empty string, this will be illegal since @ is a reserved character in YAML that must not start a scalar. You should quote it:

from: "{{ ansible_host }}@domain.com"
flyx
  • 35,506
  • 7
  • 89
  • 126