32

I try to print the previously registered mosh_version variable using the ansible debug msg command like this:

- name: Print mosh version
  debug: msg="Mosh Version: {{ mosh_version.stdout }}"

It doesn't work and prints the following error:

Note: The error may actually appear before this position: line 55, column 27

- name: Print mosh version
  debug: msg="Mosh Version: {{ mosh_version.stdout }}"
                          ^
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 }}"

I tried

- name: Print mosh version
  debug: msg=Mosh Version: "{{ mosh_version.stdout }}"

but this will just print "Mosh".

What's the best way to get this running?

6 Answers 6

45

Try this:

- name: Print mosh version
  debug: "msg=Mosh Version: '{{ mosh_version.stdout }}'"

More info in http://docs.ansible.com/YAMLSyntax.html#gotchas

Edited: Something like this works perfect for me:

- name: Check Ansible version
  command: ansible --version
  register: ansibleVersion

- name: Print version
  debug:
    msg: "Ansible Version: {{ ansibleVersion.stdout }}"

http://pastie.org/private/cgeqjucn3l5kxhkkyhtpta

5
  • There are no more syntax errors, but it doesn't work either: TASK: [ Print mosh version] ************************************** ok: [127.0.0.1] => { "msg": "Mosh" }
    – Zulakis
    Jun 1, 2015 at 12:23
  • Try first just print variable and see output without custom message, like this: - name: Print mosh version debug: var=mosh_version.stdout_lines
    – Tom Aac
    Jun 1, 2015 at 12:30
  • ok: [127.0.0.1] => { "var": { "mosh_version.stdout_lines": [ "mosh 1.2.4a [build mosh-1.2.4-57-g9eeb2fb]" ] } } this works, I'd really prefer the custom message though ;-)
    – Zulakis
    Jun 1, 2015 at 12:37
  • 1
    Check my updated answer
    – Tom Aac
    Jun 1, 2015 at 12:55
  • 1
    Please note: debug: "msg=Mosh Version: '{{ mosh_version.stdout }}'" will display only "Mosh". The msg="..." must be in quotes, instead of the whole message. But answer from @xddsg works better, as it is more detailed var dump. Nov 26, 2015 at 14:42
12

Simplest answer

- debug: var=mosh_version.stdout
3

I have displayed variable and message in the same debug play.

Ansible Task

- name: Report the /boot/initramfs file status for latest installed kernel
  debug:
    msg: "{{ ansible_hostname }} =  {{INITRAMFS_LAST_KERNEL.stdout}}"
    

Output

TASK [os-upgrade.linux : Report the /boot/initramfs file status for latest installed kernel] *******************************************
ok: [ANSIBLENODE] => {
    "msg": "ANSIBLENODE =  /boot/initramfs-3.10.0-1062.12.1.el7.x86_64.img"
}
2

Just remove the colon

debug: msg="Mosh Version {{ mosh_version.stdout }}"
1
  • Honestly, this is not a full solution but still it worked and solved the next issue I had. Can't use the other syntax because I use it with when so removing the colon actually is the simplest way to solve this. so I upvote ;)
    – TecHunter
    Oct 27, 2016 at 9:48
2

Anytime I have problems with special characters in Ansible strings/cmds I do this:

  1. Wrap with single quotes
  2. Wrap with double curly brackets

So your standard colon becomes {{':'}}

And your task becomes:

- debug: msg="Ansible Version{{':'}} {{ ansibleVersion.stdout }}"

Again this works for most special characters, even strings. Consider the following:

docker ps --format '{{.Names}}'

In order to run this in Ansible, just apply the same logic, the following task executes as expected:

- name: Get the docker container names
  become: yes
  shell: "docker ps --format '{{'{{'}}.Names{{'}}'}}'"
  register: docker_containers
1

I use this, notice the location of double quotes(") and single quotes(')

- name: Print mosh version
  debug: "msg='Mosh Version: {{ mosh_version.stdout }}'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.