Ansible - Print message - debug: msg="line1 \n {{ var2 }} \n line3 with var3 = {{ var3 }}"

AKS picture AKS · Dec 9, 2015 · Viewed 78.1k times · Source

In Ansible (1.9.4) or 2.0.0

I ran the following action:

- debug: msg="line1 \n {{ var2 }} \n line3 with var3 = {{ var3 }}"

$ cat roles/setup_jenkins_slave/tasks/main.yml

- debug: msg="Installing swarm slave = {{ slave_name }} at {{ slaves_dir }}/{{ slave_name }}"
  tags:
    - koba

- debug: msg="1 == Slave properties = fsroot[ {{ slave_fsroot }} ], master[ {{ slave_master }} ], connectingToMasterAs[ {{ slave_user }} ], description[ {{ slave_desc }} ], No.Of.Executors[ {{ slave_execs }} ], LABELs[ {{ slave_labels }} ], mode[ {{ slave_mode }} ]"
  tags:
    - koba


- debug: msg="print(2 == Slave properties = \n\nfsroot[ {{ slave_fsroot }} ],\n master[ {{ slave_master }} ],\n connectingToMasterAs[ {{ slave_user }} ],\n description[ {{ slave_desc }} ],\n No.Of.Executors[ {{ slave_execs }} ],\n LABELs[ {{ slave_labels }} ],\n mode[ {{ slave_mode }} ])"
  tags:
    - koba

But this is not printing the variable with new lines (for the 3rd debug action)?

Answer

diabloneo picture diabloneo · Dec 28, 2016

debug module support array, so you can do like this:

debug:
  msg:
    - "First line"
    - "Second line"

The output:

ok: [node1] => {
    "msg": [
        "First line",
        "Second line"
    ]
}

Or you can use the method from this answer:

In YAML, how do I break a string over multiple lines?