Ansible:在循环中向变量添加值
Ansible: Adding values to variable in a loop
我正在编写一个执行以下操作的剧本:
- 进入每个 Windows 服务器上的指定路径
- 从文件中提取文本并将其添加到变量
- 检查变量以查看文本字符串是否存在
- 根据结果将结果写入文件。
这是我的代码:
---
- 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
以下是剧本 运行 之后的文件内容:
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 镜头来看待这个问题;例如,"FOR EACH server, get the text from the file, perform a conditional check, write the output to the outfile, then move on to the next server." 在 Ansible 剧本中是否可能出现类似的情况?我已经研究过字典来解决这个问题,但我能找到的唯一好的例子是使用预先存在的字典或在 运行 时间填充基本服务器信息的字典。
在我看来,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
使用 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
我正在编写一个执行以下操作的剧本:
- 进入每个 Windows 服务器上的指定路径
- 从文件中提取文本并将其添加到变量
- 检查变量以查看文本字符串是否存在
- 根据结果将结果写入文件。
这是我的代码:
---
- 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
以下是剧本 运行 之后的文件内容:
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 镜头来看待这个问题;例如,"FOR EACH server, get the text from the file, perform a conditional check, write the output to the outfile, then move on to the next server." 在 Ansible 剧本中是否可能出现类似的情况?我已经研究过字典来解决这个问题,但我能找到的唯一好的例子是使用预先存在的字典或在 运行 时间填充基本服务器信息的字典。
在我看来,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
使用 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