-1

In a YAML playbook, I need to run multiple commands through each element in an array. I am using with_items to iterate, but I am getting syntax errors when I try to blend debug into the tasks.

Everything works when I remove the debug module, but than I can't see whats happening. Example provided below.

Produces Syntax Error:

- name: Iterate through array and display results  
  shell: "run command 1 on {{ item }}"  
  register: command1  
  debug:  
    msg: "Command 1 || {{ command1.stdout_lines}}"  
  shell: "run command 2 on {{ item }}"  
  register: command2  
  debug:  
    msg: "Command 2 || {{ command2.stdout_lines}}"  
  with_items: "{{ array_name }}"

Everything seems to be working fine when I remove the debugs, but I need to see what each command produces because it has important info.

Is there some way to print/debug the results while keeping it inside the with_items iterations?

Edit:

I also tried a few more things but they also gave me a syntax error

Also Tried 1:

- name: Iterate through array and display results  
- shell: "run command 1 on {{ item }}"  
  register: command1  
- debug:  
    msg: "Command 1 || {{ command1.stdout_lines}}"  
- shell: "run command 2 on {{ item }}"  
  register: command2  
- debug:  
    msg: "Command 2 || {{ command2.stdout_lines}}"  
  with_items: "{{ array_name }}"

Also Tried 2:

- name: Iterate through array and display results  
  - shell: "run command 1 on {{ item }}"  
    register: command1  
  - debug:  
      msg: "Command 1 || {{ command1.stdout_lines}}"  
  - shell: "run command 2 on {{ item }}"  
    register: command2  
  - debug:  
      msg: "Command 2 || {{ command2.stdout_lines}}"  
  with_items: "{{ array_name }}"
U880D
  • 3,600
  • 5
  • 18
  • 30
  • 1
    Please have a read at [ask] as well as the [markdown editing help page](https://stackoverflow.com/editing-help), as your question is currently written, the error, which comes from a syntax in your code, cannot be pointed as you did not format your code in the question as it is in your Jenkins integration. – β.εηοιτ.βε Jan 03 '22 at 19:38
  • I fixed the format of the code – NewbChewbCS Jan 03 '22 at 19:56
  • 2
    So, in Ansible, you can only have one module per item in the list (`tasks` is a list of tasks, each being an Ansible module). To fix your issue, you have to have a new item in the list (a dash `-`), in front of all the `debug` and `shell` tasks. – β.εηοιτ.βε Jan 03 '22 at 20:58
  • Ok thank you @β.εηοιτ.βε , I tried a few things with the dashes and also produced errors. I edited the original post to provide examples of how I formatted the playbooks. I am not sure what I am doing wrong. – NewbChewbCS Jan 04 '22 at 18:47
  • 1
    You can fix all those syntax errors yourself by making your playbook valid after spending the necessary time to [read the documentation](https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#about-playbooks) – Zeitounator Jan 05 '22 at 08:32

2 Answers2

0

As the comments of β.εηοιτ.βε already stated

In Ansible, you can only have one module per item in the list (tasks is a list of tasks, each being an Ansible module) ... you have to have a new item in the list (a dash -), in front of all the debug and shell tasks.

This means,

integrate debug into iterations I try to blend debug into the tasks

you can't do debugging in that way since the debug_module is for

Print statements during execution

only. However, according your question

Is there some way to print/debug the results while keeping it inside the with_items iterations?

it seems for me that you are more interested in Debugging tasks, play, role or block itself. So for this

Ansible offers a task debugger so you can fix errors during execution instead of editing your playbook and running it again to see if your change worked.

For usage you have to Enabling the debugger with the debugger keyword and may have a look into the Examples, Resolving errors in the debugger and Available debug commands.

Further questions here on SO are

U880D
  • 3,600
  • 5
  • 18
  • 30
0

I was able to solve the issue, Ansible does not allow iterations through multiple tasks. I had to put the tasks in a separate yml playbook and use include_tasks: to call the playbook.