如何打印上一个任务的结果变量的标准输出

如何打印上一个任务的结果变量的标准输出

我写了这个任务

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

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

它会打印出我不需要看到的所有变量

 _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.

我只想查看“stdout_lines:”及其内容。通常,如果不是循环,则一切正常,但在 out 中键入结果,但我不知道如何打印出 _repostatus.stdout_lines。

它给出了这个错误

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

尝试寻找解决方案,但没有找到我想要的

答案1

我只想看看stdout_lines:它的内容。通常,如果它不是循环,那么一切都正常,但results输出中有密钥,我不知道如何将其打印出来

最小示例剧本

---
- 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

将导致请求的输出

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 '

总而言之,需要循环遍历results_repostatus.results才能得到item.stdout后者。因此只需使用loop: "{{ _repostatus.results }}"

相关内容