0

我正在编写执行以下操作的剧本:

  1. 进入每个 Windows 服务器上的指定路径
  2. 从文件中提取文本并将其添加到变量中
  3. 对变量执行检查以查看是否存在文本字符串
  4. 根据结果​​将结果写入文件。

这是我的代码:

---
- name: Slurps text from file on Windows server
  hosts: win
  gather_facts: false

  tasks:    
    - name: Get text
      slurp:
        src: D:\testsearch.ini
      register: norequest

    - name: Check for norequest=false in variable
      lineinfile:
        dest: ./norequest.csv
        line: "{{ inventory_hostname }} There is a false value"
        state: present
        create: true
        insertafter: EOF
      when: '"''NoRequest = False'' in norequest.content|b64decode"|lower'
      delegate_to: localhost

    - name: Check for norequest=true in variable
      lineinfile:
        dest: ./norequest.csv
        line: "{{ inventory_hostname }} There is a true value."
        state: present
        create: true
        insertafter: EOF
      when: '"''NoRequest = True'' in norequest.content|b64decode"|lower'
      delegate_to: localhost

根据我的结果,看起来剧本从两台测试服务器上的文件中提取文本并将其全部添加到变量中,然后对其中一台服务器执行条件检查(因为任务本身被委派给本地主机)和将结果输出到文件中,就好像它们都来自 SERVER1 一样(最后一部分似乎是由于委托)。

PLAY [Slurps text from file on Windows server] *******************************

TASK [Delete previous norequest file] *******************************
changed: [SERVER1 -> localhost]

TASK [Get text] ***************************************
ok: [SERVER2]
ok: [SERVER1]

TASK [Check for norequest=false in variable] ********************************
changed: [SERVER1 -> localhost]

TASK [Check for norequest=true in variable] *******************************
changed: [SERVER1 -> localhost]

PLAY RECAP *******************************
SERVER1  : ok=4    changed=3    unreachable=0    failed=0
SERVER2  : ok=1    changed=0    unreachable=0    failed=0

以下是 playbook 运行后文件的内容:

SERVER1 There is a false value
SERVER1 There is a true value.

如果剧本按我的意愿工作,结果应该是这样的:

SERVER1 There is a false value
SERVER2 There is a true value.

我觉得我的部分(或全部)问题可能是我正在通过 PowerShell 镜头看待这个问题;如,“对于每个服务器,从文件中获取文本,执行条件检查,将输出写入输出文件,然后转到下一个服务器。” 在 Ansible 剧本中是否可能出现类似的情况?我已经研究了字典作为解决此问题的一种方法,但我能找到的唯一很好的例子是使用预先存在的字典或在运行时填充基本服务器信息的字典。

4

1 回答 1

1

在我看来,when:条件是错误的。匹配现在通过正则表达式完成。用这个剧本测试它:

---
- name: Slurps text from file on Windows server
  hosts:
    - SERVER1
    - SERVER2
  gather_facts: false

  tasks:
    - name: Get text
      slurp:
        src: D:\testsearch.ini
      register: norequest

    - name: Check for norequest=false in variable
      lineinfile:
        dest: ./norequest.csv
        line: "{{ inventory_hostname }} There is a false value"
        state: present
        create: true
        insertafter: EOF
      when: 'norequest["content"] | b64decode | lower | regex_search("norequest *= *false")'
      delegate_to: localhost

    - name: Check for norequest=true in variable
      lineinfile:
        dest: ./norequest.csv
        line: "{{ inventory_hostname }} There is a true  value"
        state: present
        create: true
        insertafter: EOF
      when: 'norequest["content"] | b64decode | lower | regex_search("norequest *= *true")'
      delegate_to: localhost

该文件testsearch.ini在系统上具有以下内容:

服务器1

NoRequest = False

服务器2

NoRequest = True

执行 playbookansible-playbook -i hosts play.yml会给出以下输出:

PLAY [SERVER1,SERVER2] *********************************************************

TASK [Get text] ****************************************************************
ok: [SERVER1]
ok: [SERVER2]

TASK [Check for norequest=false in variable] ***********************************
skipping: [SERVER2]
ok: [SERVER1 -> localhost]

TASK [Check for norequest=true in variable] ************************************
skipping: [SERVER1]
ok: [SERVER2 -> localhost]

PLAY RECAP *********************************************************************
SERVER1                  : ok=2    changed=0    unreachable=0    failed=0
SERVER2                  : ok=2    changed=0    unreachable=0    failed=0

norequest.csv运行后的内容是

SERVER1 There is a false value
SERVER2 There is a true  value
于 2018-10-26T12:57:58.373 回答