需要语法将Ansible元模块添加到现有的Playbook

时间:2019-08-14 02:32:43

标签: ansible ansible-2.x

我希望在/ home / examples目录下所有扩展名为* .db的文件中搜索字符串(“ AC245”)。下面是我尝试过的。

---
- name: "Find the details here "


  hosts: localhost
  any_errors_fatal: true
  serial: 1
  tasks:
  - name: Ansible find files multiple patterns examples
    find:
      paths: /home/examples
      patterns: "*.db"
      recurse: yes
    register: files_matched


  - name: Search for String in the matched files
    command: grep -i {{ myString }} {{ item.path }}
    register: command_result
    failed_when: command_result.rc == 0
    with_items:
       - "{{ files_matched.files }}"  

使用以下命令运行上述find.yml:

ansible-playbook find.yml -e "myString=AC245"

我的要求是,如果找到了字符串,我希望立即使用“ meta:end_play”将剧本标记为FAILED来中止播放。

您能帮我建议一下,一旦在任何* .db文件中找到字符串,我如何更新当前代码以添加end_play功能?

2 个答案:

答案 0 :(得分:0)

一种可能的解决方案是将任务移到单独的任务文件中并在其上循环。这将允许对任务的每次迭代进行离散控制。例如:

playbook.yml

---
- name: "Find the details here "
  hosts: localhost
  any_errors_fatal: true
  serial: 1
  tasks:
  - name: Ansible find files multiple patterns examples
    find:
      paths: /home/mparkins/bin/playbooks_sandpit/outputs/dir
      patterns: "*.db"
      recurse: yes
    register: files_matched

  - name: Search for String in the matched files
    include_tasks:
      tasks.yml
    with_items:
       - "{{ files_matched.files }}"  

tasks.yml

- command: grep -i {{ myString }} {{ item.path }}
  register: command_result
  failed_when: command_result.rc == 0

如果希望以其他方式失败,则可以在任务文件中添加其他任务,例如,使用fail模块或meta: end_play

答案 1 :(得分:0)

  

Q:“要求是,如果找到了字符串,我希望立即使用“ meta:end_play”将剧本标记为FAILED来中止播放。”

(可使用2.7.9)

A:不可能。

1)无法根据条件(找到字符串)在循环中执行meta: end_play

2)无法一次执行meta: end_playfail/assert


1)可以编写filter_plugin(Python2)

$ cat filter_plugins/file_filters.py
import mmap

def file_list_search(list, string):
    for file in list:
        if file_search(file, string):
            break
    return file_search(file, string)

def file_search(file, string):
    f = open(file)
    s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
    if s.find(string) != -1:
        return True
    else:
        return False

class FilterModule(object):
    ''' Ansible filters for operating on files '''

    def filters(self):
        return {
            'file_search' : file_search,
            'file_list_search' : file_list_search
        }

并将其用于戏剧中。例如

- hosts: localhost
  vars:
    myString: "test string"
  tasks:
    - find:
        paths: /home/examples
        patterns: "*.db"
      register: files_matched
    - name: End of play when myString found
      meta: end_play
      when: "files_matched.files|
             json_query('[*].path')|
             file_list_search(myString)"
    - debug:
        msg: continue

2)可以set_stats。下面的游戏

- name: End of play when myString found
  block:
    - set_stats:
        data:
          FAILED: 1
    - meta: end_play
  when: "files_matched.files|
         json_query('[*].path')|
         file_list_search(myString)"

- debug:
    msg: continue
- set_stats:
    data:
      FAILED: 0

如果找到该字符串,则给出以下输出

PLAY RECAP **********************************************************************************
127.0.0.1                  : ok=4    changed=0    unreachable=0    failed=0   


CUSTOM STATS: *******************************************************************************

    RUN: { "FAILED": 1}

允许show_custom_stats

$ grep stats ansible.cfg
show_custom_stats = True