-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
  • 8,601
  • 6
  • 24
  • 40
NewbChewbCS
  • 67
  • 2
  • 8
  • 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

1 Answers1

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.

NewbChewbCS
  • 67
  • 2
  • 8