0

I wrote this task

- name: Enabling the repoistory                          
  shell:  subscription-manager repos --enable={{ item }} 
  register: _repostatus                                  
  loop: "{{ repo_id }}"    

- debug:                          
    msg: "{{ item.stdout }}"      
  loop: "{{ _repostatus.stdout_lines }}"

And it print all the variables which i dont need to see

 _repostatus:
   changed: true
   msg: All items completed
   results:
   - ansible_loop_var: item
     changed: true
     cmd: subscription-manager repos --enable=rhel-7-server-extras-rpms
     delta: '0:00:04.035395'
     end: '2024-02-19 10:43:14.323637'
     failed: false
     invocation:
       module_args:
         _raw_params: subscription-manager repos --enable=rhel-7-server-extras-rpms
         _uses_shell: true
         argv: null
         chdir: null
         creates: null
         executable: null
         removes: null
         stdin: null
         stdin_add_newline: true
         strip_empty_ends: true
         warn: false
     item: rhel-7-server-extras-rpms
     rc: 0
     start: '2024-02-19 10:43:10.288242'
     stderr: ''
     stderr_lines: []
     stdout: Repository 'rhel-7-server-extras-rpms' is enabled for this system.
     stdout_lines:
     - Repository 'rhel-7-server-extras-rpms' is enabled for this system.

I only want to see "stdout_lines:" and its content. Normally if it is not a loop then everything is ok but there is results key in out and i dont know how to printout _repostatus.stdout_lines.

It gives this error

  msg: '''dict object'' has no attribute ''stdout_lines'''

Tried to search for solution but didnt find exactly what i am looking for

1 Answer 1

0

I only want to see stdout_lines: and its content. Normally if it is not a loop then everything is OK but there is results key in the output and I don't know how to print it out

A minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Create STDOUT output (single)
    command: 'echo "1"'
    register: result

  - name: Show '.stdout'
    debug:
      msg: "The result in '.stdout': {{ result.stdout }} "

  - name: Show full result (single)
    debug:
      var: result

  - name: Create STDOUT output (loop)
    command: 'echo "{{ item }}"'
    register: result
    loop: [1, 2, 3]
    loop_control:
      label: "{{ item }}"

  - name: Show '.stdout'
    debug:
      msg: "The result in '.stdout': {{ item.stdout }} "
    loop: "{{ result.results }}"
    loop_control:
      label: "{{ item.item }}"

  - name: Show full result (loop)
    debug:
      var: result

will result into the requested output

TASK [Show '.stdout'] ****************
ok: [localhost] => (item=1) =>
  msg: 'The result in ''.stdout'': 1 '
ok: [localhost] => (item=2) =>
  msg: 'The result in ''.stdout'': 2 '
ok: [localhost] => (item=3) =>
  msg: 'The result in ''.stdout'': 3 '

To summarize, one will need to loop over the results, _repostatus.results in order to get the item.stdout later. So just use loop: "{{ _repostatus.results }}"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .