从Ansible调试中提取stdout_lines的准确值

时间:2018-09-11 06:28:21

标签: 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_name] LIKE '%Manager%'AND [counter_name] = 'Page life expectancy'"
    register: win_command_result


  - debug:
      var: win_command_result.stdout_lines.object_name

我们得到这样的输出

TASK [debug] 

ok: [db1.local] => {
    "win_command_result": {
        "changed": true, 
        "cmd": "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'\"", 
        "delta": "0:00:01.099974", 
        "end": "2018-09-11 05:08:36.022907", 
        "failed": false, 
        "rc": 0, 
        "start": "2018-09-11 05:08:34.922933", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "object_name                                                                                                                      
                   counter_name                                                                                                                     
                   cntr_value          \r\n--------------------------- 
  ----------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- --------------------\r\nSQLServer:Buffer Manager                                                                                                         
                            Page life expectancy                                                                                                                           
                            238579\r\n\r\n(1 rows affected)\r\n", 
        "stdout_lines": [
            "object_name                                                                                                                      
             counter_name                                                                                                                     
             cntr_value          ", 
            "--------------------------------------------------------- 
            ----------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- --------------------", 
            "SQLServer:Buffer Manager                                                                                                         
             Page life expectancy                                                                                                                           
             238579", 
            "", 
            "(1 rows affected)"
        ] 
    }
}  

我们仅需要 cntr_value 的值,即 238579

如果我设为cntr_value是stdout_lines的子代,则设置为

  - debug:
      var: win_command_result.stdout_lines.cntr_value

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

如何提取 cntr_value

的准确值

1 个答案:

答案 0 :(得分:1)

您输出的最终变量是:

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] }}"