Ansible 有没有办法在每次循环的时候把值存入数组?
Is There a Way to Have a Value Stored in an Array Every Time the Loop Occurs in Ansible?
我有一个 ansible-playbook
,旨在显示来自域控制器内 DNS 服务器的特定主机的 A Record
。这是我在 Ansible-Playbook 上所做的:
- 使用powershell获取DNS服务器A记录相关信息。
- 将其保存为名为
test_var
. 的变量
- 将变量
test_var
的内容逐行划分
- 检索包含我要查找的字符串
host
的重要行。
- 把那些重要行的重要属性取下来,显示为
msg
。
代码如下:
# hostname and domain are necessary
---
- hosts: all
gather_facts: no
vars:
search_name: "{{hostname}}"
tasks:
- name: powershell query
win_shell: "Get-DnsServerResourceRecord -Name '{{hostname}}' -ZoneName '{{domain}}' -RRType A"
register: result1
when: (hostname is defined) and (domain is defined)
- set_fact:
test_var: "{{ result1.stdout_lines }}"
- name: pickup lines
set_fact:
important_lines: "{{ important_lines |default([]) + [item] }}"
with_items:
- "{{ test_var }}"
- name: find the line
set_fact:
target_line: "{{item}}"
when: item|trim is search(search_name)
loop: "{{ important_lines | flatten(1) }}"
- name: get all attributes
set_fact:
attribute_record: "{{ target_line.split()[1]|trim}}"
attribute_type: "{{ target_line.split()[2]|trim}}"
attribute_timestamp: "{{ target_line.split()[3]|trim}}"
attribute_timetolive: "{{ target_line.split()[4]|trim}}"
attribute_ipaddress: "{{ target_line.split()[5]|trim}}"
- name: print results
debug:
msg: "name: {{search_name}}, Ip Address: {{attribute_ipaddress}}"
这是我的 DNS 服务器配置:
结果如下(host=test1):
但是,我有一个问题。在运行循环的 Find the line
任务中,target_line
变量仅存储任务末尾的最后一行。所以,当执行 print results
任务时,只会显示最后的主机和 IP 地址。问题是,有没有办法在每次循环发生时将每一行存储在一个数组中?这样,我就可以调用数组的内容来一一显示了。谢谢。
这是我得到的解决方案:
# hostname and domain are necessary
---
- hosts: all
gather_facts: no
vars:
correct_line: []
search_name: "{{hostname}}"
tasks:
- name: powershell query
win_shell: "Get-DnsServerResourceRecord -Name '{{hostname}}' -ZoneName '{{domain}}' -RRType A"
register: result1
when: (hostname is defined) and (domain is defined)
- set_fact:
test_var: "{{ result1.stdout_lines }}"
- name: pickup lines
set_fact:
important_lines: "{{ important_lines |default([]) + [item] }}"
with_items:
- "{{ test_var }}"
- name: find the line
set_fact:
correct_line: "{{correct_line + [item]}}"
when: item|trim is search(search_name)
loop: "{{ important_lines | flatten(1) }}"
- name: print results
debug:
msg: "name: {{item.split()[0]|trim}}, Ip Address: {{item.split()[5]|trim}}"
loop: "{{ correct_line | flatten(1) }}"
结果如下:
我有一个 ansible-playbook
,旨在显示来自域控制器内 DNS 服务器的特定主机的 A Record
。这是我在 Ansible-Playbook 上所做的:
- 使用powershell获取DNS服务器A记录相关信息。
- 将其保存为名为
test_var
. 的变量
- 将变量
test_var
的内容逐行划分 - 检索包含我要查找的字符串
host
的重要行。 - 把那些重要行的重要属性取下来,显示为
msg
。
代码如下:
# hostname and domain are necessary
---
- hosts: all
gather_facts: no
vars:
search_name: "{{hostname}}"
tasks:
- name: powershell query
win_shell: "Get-DnsServerResourceRecord -Name '{{hostname}}' -ZoneName '{{domain}}' -RRType A"
register: result1
when: (hostname is defined) and (domain is defined)
- set_fact:
test_var: "{{ result1.stdout_lines }}"
- name: pickup lines
set_fact:
important_lines: "{{ important_lines |default([]) + [item] }}"
with_items:
- "{{ test_var }}"
- name: find the line
set_fact:
target_line: "{{item}}"
when: item|trim is search(search_name)
loop: "{{ important_lines | flatten(1) }}"
- name: get all attributes
set_fact:
attribute_record: "{{ target_line.split()[1]|trim}}"
attribute_type: "{{ target_line.split()[2]|trim}}"
attribute_timestamp: "{{ target_line.split()[3]|trim}}"
attribute_timetolive: "{{ target_line.split()[4]|trim}}"
attribute_ipaddress: "{{ target_line.split()[5]|trim}}"
- name: print results
debug:
msg: "name: {{search_name}}, Ip Address: {{attribute_ipaddress}}"
这是我的 DNS 服务器配置:
结果如下(host=test1):
但是,我有一个问题。在运行循环的 Find the line
任务中,target_line
变量仅存储任务末尾的最后一行。所以,当执行 print results
任务时,只会显示最后的主机和 IP 地址。问题是,有没有办法在每次循环发生时将每一行存储在一个数组中?这样,我就可以调用数组的内容来一一显示了。谢谢。
这是我得到的解决方案:
# hostname and domain are necessary
---
- hosts: all
gather_facts: no
vars:
correct_line: []
search_name: "{{hostname}}"
tasks:
- name: powershell query
win_shell: "Get-DnsServerResourceRecord -Name '{{hostname}}' -ZoneName '{{domain}}' -RRType A"
register: result1
when: (hostname is defined) and (domain is defined)
- set_fact:
test_var: "{{ result1.stdout_lines }}"
- name: pickup lines
set_fact:
important_lines: "{{ important_lines |default([]) + [item] }}"
with_items:
- "{{ test_var }}"
- name: find the line
set_fact:
correct_line: "{{correct_line + [item]}}"
when: item|trim is search(search_name)
loop: "{{ important_lines | flatten(1) }}"
- name: print results
debug:
msg: "name: {{item.split()[0]|trim}}, Ip Address: {{item.split()[5]|trim}}"
loop: "{{ correct_line | flatten(1) }}"
结果如下: