Ubuntu Ansible-如何捕获命令输出并将其另存为多个变量

Ubuntu Ansible-如何捕获命令输出并将其另存为多个变量,ubuntu,ansible,Ubuntu,Ansible,我正在尝试使用Ansible编写一些自动化程序,我遇到了一种情况,即我需要在以后的任务中使用命令输出的多个部分 我有一个任务要运行命令“lspci | grep Ethernet | grep Gigabit” 输出应如下所示(应为2行输出): 在这个输出中,我需要提取4条信息作为变量 我要第一条线的巴士。应该是“02” var2-我想要第一行的函数。应为“0” var3-我要乘第二条线的公共汽车。应该是“02” var4-我想要第二行的函数。应该是“1” 如何将输出中的信息提取到这4个变量中?

我正在尝试使用Ansible编写一些自动化程序,我遇到了一种情况,即我需要在以后的任务中使用命令输出的多个部分

我有一个任务要运行命令“lspci | grep Ethernet | grep Gigabit”

输出应如下所示(应为2行输出):

在这个输出中,我需要提取4条信息作为变量

我要第一条线的巴士。应该是“02”

var2-我想要第一行的函数。应为“0”

var3-我要乘第二条线的公共汽车。应该是“02”

var4-我想要第二行的函数。应该是“1”

如何将输出中的信息提取到这4个变量中?不一定非得是一个包含所有4个变量的单一游戏


谢谢您的帮助。

按照建议,您应该注册该命令的输出。之后,您可以循环查看结果并获取您感兴趣的部分

工作示例:

- name: Fun with lspci output 
  hosts: localhost
  connection: local
  tasks:
   - name: get Gigabit Ethernet adapters
     shell: lspci | grep Ethernet | grep Gigabit | awk '{print $1}'
     register: eth_adapters

   - name: use extracted info
     debug: msg="Adapter found, bus {{item.split(':')[0]}}, function {{item.split('.')[-1]}}"
     with_items: "{{eth_adapters.stdout_lines}}"
注意使用
eth_adapters.stdout_行
获取命令输出作为项目列表,使用
awk'{print$1}'
仅获取设备的总线信息

此示例生成以下输出:

PLAY [Fun with lspci output] ***************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [get Gigabit Ethernet adapters] *******************************************
changed: [localhost]

TASK [use extracted info] ******************************************************
ok: [localhost] => (item=00:00.0) => {
    "item": "00:00.0", 
    "msg": "Adapter found, bus 00, function 0"
}
ok: [localhost] => (item=00:01.0) => {
    "item": "00:01.0", 
    "msg": "Adapter found, bus 00, function 0"
}
ok: [localhost] => (item=00:01.1) => {
    "item": "00:01.1", 
    "msg": "Adapter found, bus 00, function 1"
}
ok: [localhost] => (item=00:01.3) => {
    "item": "00:01.3", 
    "msg": "Adapter found, bus 00, function 3"
}


PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0 

您是否尝试过使用
寄存器
?显示您尝试过的内容,我们可以提供帮助。如果我使用register,当我只需要字符串的一小部分时,它将获取整个输出字符串。它提供“行”,因此您可以开始使用Jinja提取片段。使用
register
保存所有输出,然后使用bash
grep
获取每个变量<代码>-命令:cat{{all_output.stdout}}grep#您喜欢什么
PLAY [Fun with lspci output] ***************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [get Gigabit Ethernet adapters] *******************************************
changed: [localhost]

TASK [use extracted info] ******************************************************
ok: [localhost] => (item=00:00.0) => {
    "item": "00:00.0", 
    "msg": "Adapter found, bus 00, function 0"
}
ok: [localhost] => (item=00:01.0) => {
    "item": "00:01.0", 
    "msg": "Adapter found, bus 00, function 0"
}
ok: [localhost] => (item=00:01.1) => {
    "item": "00:01.1", 
    "msg": "Adapter found, bus 00, function 1"
}
ok: [localhost] => (item=00:01.3) => {
    "item": "00:01.3", 
    "msg": "Adapter found, bus 00, function 3"
}


PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0