从Ansible调试中提取标准输出行的精确值

从Ansible调试中提取标准输出行的精确值,ansible,Ansible,我得到了我的剧本,从中我通过调试方法得到了我需要的值,但我无法得到调试变量的子级的确切值 下面是我的剧本 - hosts: db tasks: - name: Checking if For Page Life Expectancy. win_command: sqlcmd -q "SELECT [object_name],[counter_name],[cntr_value] FROM sys.dm_os_performance_counters WHERE [object_n

我得到了我的剧本,从中我通过调试方法得到了我需要的值,但我无法得到调试变量的子级的确切值

下面是我的剧本

- hosts: db
  tasks:
  - name: Checking if For Page Life Expectancy.
    win_command: sqlcmd -q "SELECT [object_name],[counter_name],[cntr_value] FROM sys.dm_os_performance_counters WHERE [object_name] LIKE '%Manager%'AND [counter_name] = 'Page life expectancy'"
    register: win_command_result


  - debug:
      var: win_command_result.stdout_lines.object_name
我们得到这样的输出

TASK [debug] 

我们只需要cntr_值的值,即238579

如果我将cntr_值设置为stdout_行的子级

  - debug:
      var: win_command_result.stdout_lines.cntr_value
上面说

ok: [db1.local] => {
    "win_command_result.stdout_lines.cntr_value": "VARIABLE IS NOT DEFINED!"
}

如何提取cntr\U值的精确值?您的输出最终变量是:

myvar.win_command_result.stdout
因此,如果要提取数字,请执行正则表达式:

- name: Debug
  debug:
    msg: "{{ myvar.win_command_result.stdout | regex_search('\\d{6}')}}"
另一种选择:

- name: Fact
  set_fact:
    mylist: "{{ myvar.win_command_result.stdout_lines | list }}"

- name: Debug
  debug:
    msg: "{{ item | regex_search('\\d+') }}"
  with_items: "{{ mylist[2] }}"

您的输出最终变量是:

myvar.win_command_result.stdout
因此,如果要提取数字,请执行正则表达式:

- name: Debug
  debug:
    msg: "{{ myvar.win_command_result.stdout | regex_search('\\d{6}')}}"
另一种选择:

- name: Fact
  set_fact:
    mylist: "{{ myvar.win_command_result.stdout_lines | list }}"

- name: Debug
  debug:
    msg: "{{ item | regex_search('\\d+') }}"
  with_items: "{{ mylist[2] }}"

有了regex yes,它就可以工作了,要把那个值作为条件使用,我需要注册那个msg。然而,它只在值为6位或更多数字时工作。该值可能或多或少,这就是我们试图获取子值stdout的原因。更好!我加了六个,只是为了跟你学。在答案中修改;)有了regex yes,它就可以工作了,要把那个值作为条件使用,我需要注册那个msg。然而,它只在值为6位或更多数字时工作。该值可能或多或少,这就是我们试图获取子值stdout的原因。更好!我加了六个,只是为了跟你学。在答案中修改;)