Ansible:将值循环添加到变量

时间:2018-10-25 16:53:50

标签: loops dictionary ansible slurp

我正在研究执行以下任务的剧本:

  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

根据我的结果,看似剧本从两个测试服务器上的文件中吸取文本并将其全部添加到变量中,然后对其中一台服务器执行条件检查(因为任务本身被委派给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

运行剧本后,这里是文件的内容:

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剧本中是否有可能做到这一点?我已经研究了字典作为解决此问题的一种方法,但是我能找到的唯一好例子是使用预先存在的字典或在运行时使用基本服务器信息填充的字典。

1 个答案:

答案 0 :(得分: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在系统上具有以下内容:

SERVER1

NoRequest = False

SERVER2

NoRequest = True

使用ansible-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