加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 在可循环播放器中注册循环中的变量

发布时间:2020-12-13 19:48:43 所属栏目:Linux 来源:网络整理
导读:我有两个可以完成的任务如下 tasks: - shell: ifconfig -a | sed 's/[ t].*//;/^(lo|)$/d' register: var1 - debug: var=var1 - shell: ethtool -i {{ item }} | grep bus-info | cut -b 16-22 with_items: var1.stdout_lines register: var2 - debug: v
我有两个可以完成的任务如下
tasks:
 - shell: ifconfig -a | sed 's/[ t].*//;/^(lo|)$/d'
   register: var1
 - debug: var=var1

 - shell: ethtool -i {{ item }} | grep bus-info | cut -b 16-22
   with_items: var1.stdout_lines
   register: var2
 - debug: var=var2

用于获取机器(linux)中的接口列表,并获取每个接口的总线地址.在同一本书中,我还有一个以上的任务

- name: Binding the interfaces
   shell: echo {{ item.item }}
   with_flattened: var2.results
   register: var3

我期望从var2迭代值,然后打印总线编号.

var2.results如下

"var2": {
    "changed": true,"msg": "All items completed","results": [
        {
            "changed": true,"cmd": "ethtool -i br0: | grep bus-info | cut -b 16-22","delta": "0:00:00.005778","end": "2015-04-14 20:29:47.122203","invocation": {
                "module_args": "ethtool -i br0: | grep bus-info | cut -b 16-22","module_name": "shell"
            },"item": "br0:","rc": 0,"start": "2015-04-14 20:29:47.116425","stderr": "","stdout": "","warnings": []
        },{
            "changed": true,"cmd": "ethtool -i enp13s0: | grep bus-info | cut -b 16-22","delta": "0:00:00.005862","end": "2015-04-14 20:29:47.359749","invocation": {
                "module_args": "ethtool -i enp13s0: | grep bus-info | cut -b 16-22","item": "enp13s0:","start": "2015-04-14 20:29:47.353887","stdout": "0d:00.0","cmd": "ethtool -i enp14s0: | grep bus-info | cut -b 16-22","delta": "0:00:00.005805","end": "2015-04-14 20:29:47.576674","invocation": {
                "module_args": "ethtool -i enp14s0: | grep bus-info | cut -b 16-22","item": "enp14s0:","start": "2015-04-14 20:29:47.570869","stdout": "0e:00.0","cmd": "ethtool -i enp15s0: | grep bus-info | cut -b 16-22","delta": "0:00:00.005873","end": "2015-04-14 20:29:47.875058","invocation": {
                "module_args": "ethtool -i enp15s0: | grep bus-info | cut -b 16-22","item": "enp15s0:","start": "2015-04-14 20:29:47.869185","stdout": "0f:00.0","cmd": "ethtool -i enp5s0f1: | grep bus-info | cut -b 16-22","delta": "0:00:00.005870","end": "2015-04-14 20:29:48.112027","invocation": {
                "module_args": "ethtool -i enp5s0f1: | grep bus-info | cut -b 16-22","item": "enp5s0f1:","start": "2015-04-14 20:29:48.106157","stdout": "05:00.1","cmd": "ethtool -i enp5s0f2: | grep bus-info | cut -b 16-22","delta": "0:00:00.005863","end": "2015-04-14 20:29:48.355733","invocation": {
                "module_args": "ethtool -i enp5s0f2: | grep bus-info | cut -b 16-22","item": "enp5s0f2:","start": "2015-04-14 20:29:48.349870","stdout": "05:00.2","cmd": "ethtool -i enp5s0f3: | grep bus-info | cut -b 16-22","delta": "0:00:00.005829","end": "2015-04-14 20:29:48.591244","invocation": {
                "module_args": "ethtool -i enp5s0f3: | grep bus-info | cut -b 16-22","item": "enp5s0f3:","start": "2015-04-14 20:29:48.585415","stdout": "05:00.3","cmd": "ethtool -i enp9s0f0: | grep bus-info | cut -b 16-22","delta": "0:00:00.005943","end": "2015-04-14 20:29:48.910992","invocation": {
                "module_args": "ethtool -i enp9s0f0: | grep bus-info | cut -b 16-22","item": "enp9s0f0:","start": "2015-04-14 20:29:48.905049","stdout": "09:00.0","cmd": "ethtool -i enp9s0f1: | grep bus-info | cut -b 16-22","end": "2015-04-14 20:29:49.143706","invocation": {
                "module_args": "ethtool -i enp9s0f1: | grep bus-info | cut -b 16-22","item": "enp9s0f1:","start": "2015-04-14 20:29:49.137843","stdout": "09:00.1","cmd": "ethtool -i lo: | grep bus-info | cut -b 16-22","delta": "0:00:00.005856","end": "2015-04-14 20:29:49.386044","invocation": {
                "module_args": "ethtool -i lo: | grep bus-info | cut -b 16-22","item": "lo:","start": "2015-04-14 20:29:49.380188","stderr": "Cannot get driver information: Operation not supported","cmd": "ethtool -i virbr0: | grep bus-info | cut -b 16-22","delta": "0:00:00.005859","end": "2015-04-14 20:29:49.632356","invocation": {
                "module_args": "ethtool -i virbr0: | grep bus-info | cut -b 16-22","item": "virbr0:","start": "2015-04-14 20:29:49.626497","cmd": "ethtool -i virbr0-nic: | grep bus-info | cut -b 16-22","delta": "0:00:00.024850","end": "2015-04-14 20:29:49.901539","invocation": {
                "module_args": "ethtool -i virbr0-nic: | grep bus-info | cut -b 16-22","item": "virbr0-nic:","start": "2015-04-14 20:29:49.876689","warnings": []
        }
    ]

我的目标是获取上面每个项目中的stdout的值,例如(“stdout”:“09:00.0”).我试着给些东西

- name: Binding the interfaces
       shell: echo {{ item.item.stdout}}
       with_flattened: var2.results
#       with_indexed_items: var2.results
       register: var3

但这并不是正确地给出了stdout中的总线值.在第二个变量和索引列表中,给出下面给出的任务中列出变量值变量的帮助.我试图避免直接的索引编号,如项目[0],因为接口的数量是动态的,直接索引可能会导致意想不到的结果.

谢谢

解决方法

这是你要找的:
- hosts: localhost
  tags: s21
  gather_facts: no
  vars:
    images:
      - foo
      - bar
  tasks:
    - shell: "echo result-{{item}}"
      register: "r"
      with_items: "{{images}}"

    - debug: var=r

    - debug: msg="item.item={{item.item}},item.stdout={{item.stdout}},item.changed={{item.changed}}"
      with_items: "{{r.results}}"

    - debug: msg="Gets printed only if this item changed - {{item}}"
      when: "{{item.changed == true}}"
      with_items: "{{r.results}}"

资料来源:Register variables in with_items loop in Ansible playbook

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读