Ansible regex_replace或regex_search

时间:2018-04-02 09:19:28

标签: regex ansible

我正在尝试匹配" OK"从以下输出与regex并将其存储在varible:

System 'server.mylabserver.com'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.00] [0.01] [0.05]
  cpu                          0.1%us 0.1%sy 0.0%wa
  memory usage                 367.9 MB [20.0%]
  swap usage                   0 B [0.0%]
  uptime                       2h 10m
  boot time                    Mon, 02 Apr 2018 06:51:01
  data collected               Mon, 02 Apr 2018 09:01:02

使用" regex_replace"的可接受代码我试过了:

  - name: Fetch the monit status
    shell: "monit status | tail -n +3"
    register: monit_status_raw
    tags: basic_monitoring
  - name: Extract monit variables
    set_fact:
        vmstatus: "{{ monit_status_raw | regex_replace('^\s\s([a-z]*)\s+', '\\1:')}}"

错误:

The offending line appears to be:

    set_fact:
       vmstatus: "{{ monit_status_raw | regex_replace('^\s\s([a-z]*)\s+', '\\1')}}"
                                                        ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

Ansible code with" regex_search"我试过了:

- name: Fetch the monit status
  shell: "monit status | tail -n +3"
  register: monit_status_raw
- name: Extract monit variables
  set_fact:
     vmstatus: "{{ monit_status_raw | regex_search('^\s\sstatus\s+(.*)$') }}"

错误:

The offending line appears to be:

    set_fact:
      vmstatus: "{{ monit_status_raw | regex_search('^\s\sstatus\s+(.*)$') }}"
                                                      ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

知道正则数据集中的错误吗?

谢谢你, 丹

1 个答案:

答案 0 :(得分:0)

我想如果您想使用regexp_search - 您需要提供一个字符串并考虑转义字符,然后您需要使用一些构造:

php7.1 -f file.php

但我认为这会更简单:

{{1}}

如果您使用样本,您将获得vmstatus ='Ok'。