Search code examples
ansibleansible-2.xansible-awx

Print output of the items in a with_lines loop


In the below playbook, I need to print the output of all the stdout_lines executed from the text file.
I need to validate for some string existence in the stdout_lines.

---
- hosts: '{{ host }}'
  gather_facts: true
  become: true
  vars:
    command_txt: '{{ txt_file }}'
  tasks:
  - name: apply patch on remote nodes
    ansible.builtin.shell: some_command
    register: output
    with_lines: cat {{ command_txt }}
    ignore_errors: yes   

  - debug:
        msg: "The patch apply status is::: {{ output }}" 

Solution

  • For example, given the tree for testing

    shell> tree /tmp/test
    /tmp/test
    ├── commands.txt
    ├── file1.txt
    ├── patch1.txt
    ├── patch2.txt
    └── patch3.txt
    
    0 directories, 5 files
    
    shell> cat /tmp/test/commands.txt 
    patch file1.txt < patch1.txt
    patch file1.txt < patch2.txt
    patch file1.txt < patch3.txt
    
    shell> cat /tmp/test/file1.txt 
    
    shell> cat /tmp/test/patch1.txt 
    --- file1.txt.orig  2022-12-06 06:54:57.349543101 +0100
    +++ file1.txt   2022-12-06 06:52:13.493314972 +0100
    @@ -0,0 +1 @@
    +line 1
    
    shell> cat /tmp/test/patch2.txt 
    --- file1.txt.orig  2022-12-06 06:58:33.773844471 +0100
    +++ file1.txt   2022-12-06 06:58:47.569863681 +0100
    @@ -1 +1,2 @@
     line 1
    +line 2
    
    shell> cat /tmp/test/patch3.txt 
    --- file1.txt.orig  2022-12-06 06:59:36.633932008 +0100
    +++ file1.txt   2022-12-06 06:59:48.049947906 +0100
    @@ -1,2 +1,3 @@
     line 1
     line 2
    +line 3
    

    Declare the variables

      my_path: /tmp/test
      txt_file: /tmp/test/commands.txt
    

    and iterate the commands

        - shell:
            cmd: "{{ item }}"
            chdir: "{{ my_path }}"
          with_lines: "cat {{ txt_file }}"
          register: output
    

    The patches will be applied

    shell> cat /tmp/test/file1.txt 
    line 1
    line 2
    line 3
    

    and the output will be registered

      output:
        changed: true
        msg: All items completed
        results:
        - ansible_facts:
            discovered_interpreter_python: /usr/bin/python3
          ansible_loop_var: item
          changed: true
          cmd: patch file1.txt < patch1.txt
          delta: '0:00:00.003814'
          end: '2022-12-06 07:25:24.057143'
          failed: false
          invocation:
            module_args:
              _raw_params: patch file1.txt < patch1.txt
              _uses_shell: true
              argv: null
              chdir: /tmp/test
              creates: null
              executable: null
              removes: null
              stdin: null
              stdin_add_newline: true
              strip_empty_ends: true
              warn: false
          item: patch file1.txt < patch1.txt
          msg: ''
          rc: 0
          start: '2022-12-06 07:25:24.053329'
          stderr: ''
          stderr_lines: []
          stdout: patching file file1.txt
          stdout_lines:
          - patching file file1.txt
        - ansible_loop_var: item
          changed: true
          cmd: patch file1.txt < patch2.txt
          delta: '0:00:00.004013'
          end: '2022-12-06 07:25:24.317642'
          failed: false
          invocation:
            module_args:
              _raw_params: patch file1.txt < patch2.txt
              _uses_shell: true
              argv: null
              chdir: /tmp/test
              creates: null
              executable: null
              removes: null
              stdin: null
              stdin_add_newline: true
              strip_empty_ends: true
              warn: false
          item: patch file1.txt < patch2.txt
          msg: ''
          rc: 0
          start: '2022-12-06 07:25:24.313629'
          stderr: ''
          stderr_lines: []
          stdout: patching file file1.txt
          stdout_lines:
          - patching file file1.txt
        - ansible_loop_var: item
          changed: true
          cmd: patch file1.txt < patch3.txt
          delta: '0:00:00.003602'
          end: '2022-12-06 07:25:24.582663'
          failed: false
          invocation:
            module_args:
              _raw_params: patch file1.txt < patch3.txt
              _uses_shell: true
              argv: null
              chdir: /tmp/test
              creates: null
              executable: null
              removes: null
              stdin: null
              stdin_add_newline: true
              strip_empty_ends: true
              warn: false
          item: patch file1.txt < patch3.txt
          msg: ''
          rc: 0
          start: '2022-12-06 07:25:24.579061'
          stderr: ''
          stderr_lines: []
          stdout: patching file file1.txt
          stdout_lines:
          - patching file file1.txt
        skipped: false
    

    You can see that the list results keeps the iteration's items. Print any information you like. For example,

        - debug:
            msg: "{{ item.item }} rc: {{ item.rc }}"
          loop: "{{ output.results }}"
          loop_control:
            label: "{{ item.item }}"
    

    gives

    TASK [debug] *********************************************************************************
    ok: [localhost] => (item=patch file1.txt < patch1.txt) => 
      msg: 'patch file1.txt < patch1.txt rc: 0'
    ok: [localhost] => (item=patch file1.txt < patch2.txt) => 
      msg: 'patch file1.txt < patch2.txt rc: 0'
    ok: [localhost] => (item=patch file1.txt < patch3.txt) => 
      msg: 'patch file1.txt < patch3.txt rc: 0'
    

    You can put the information into a single block. For example,

        - debug:
            msg: |
              {% for item in output.results %}
              {{ item.item }} rc: {{ item.rc }}
              {% endfor %}
    

    gives

    TASK [debug] *********************************************************************************
    ok: [localhost] => 
      msg: |-
        patch file1.txt < patch1.txt rc: 0
        patch file1.txt < patch2.txt rc: 0
        patch file1.txt < patch3.txt rc: 0