ansible将多个变量输出到同一行

时间:2019-10-14 11:43:27

标签: ansible

我正在收集使用win_reg_stat在注册表中找到的SQL版本和版本,将它们注册到变量中,并将它们写入文件中。
是否可以将值写入同一行而不是单独的行?

- name: get sql version
  win_reg_stat:
    path: HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\{{ item }}\Setup
    name: Version
  with_items: "{{ sql_versions }}"
  register: sql_version

- name: get sql edition
  win_reg_stat:
    path: HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\{{ item }}\Setup
    name: Edition
  with_items: "{{ sql_versions }}"
  register: sql_edition

- lineinfile:
    dest: /tmp/sql
    line: "{{ inventory_hostname }};{{ item.value }}"
  when: item.exists
  with_items:
    - "{{ sql_version.results }}"
  delegate_to: localhost

- lineinfile:
    dest: /tmp/sql
    line: "{{ inventory_hostname }};{{ item.value }}"
  when: item.exists
  with_items:
    - "{{ sql_edition.results }}"
  delegate_to: localhost

sql_versions变量包含多个项目:

sql_versions:
  - MSSQL15.MSSQLSERVER
  - MSSQL14.MSSQLSERVER
  - MSSQL13.MSSQLSERVER
  - MSSQL12.MSSQLSERVER
  - MSSQL11.MSSQLSERVER
  - MSSQL10_50.MSSQLSERVER
  - MSSQL10.MSSQLSERVER
  - MSSQL.1

输出示例:

TASK [licensing : register sql version] *****************************************************************************************************************************************************************************************************
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL15.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL14.MSSQLSERVER', 'ansible_loop_var': 'item'})
ok: [win2019] => (item={'changed': False, 'type': 'REG_SZ', 'raw_value': '13.2.5026.0', 'value': '13.2.5026.0', 'exists': True, 'failed': False, 'item': 'MSSQL13.MSSQLSERVER', 'ansible_loop_var': 'item'}) => {
    "msg": "13.2.5026.0"
}
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL12.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL11.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL10_50.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL10.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL.1', 'ansible_loop_var': 'item'})

TASK [licensing : register sql edition] *****************************************************************************************************************************************************************************************************
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL15.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL14.MSSQLSERVER', 'ansible_loop_var': 'item'})
ok: [win2019] => (item={'changed': False, 'type': 'REG_SZ', 'raw_value': 'Web Edition', 'value': 'Web Edition', 'exists': True, 'failed': False, 'item': 'MSSQL13.MSSQLSERVER', 'ansible_loop_var': 'item'}) => {
    "msg": "Web Edition"
}
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL12.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL11.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL10_50.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL10.MSSQLSERVER', 'ansible_loop_var': 'item'})
skipping: [win2019] => (item={'changed': False, 'exists': False, 'failed': False, 'item': 'MSSQL.1', 'ansible_loop_var': 'item'})

2 个答案:

答案 0 :(得分:0)

您尝试过此吗。

- lineinfile:
    dest: /tmp/sql
    line: "{{ inventory_hostname }};{{ item.value }}"
  when: item.exists
  with_items:
    - "{{ sql_version.results }}"
    - "{{ sql_edition.results }}"
  delegate_to: localhost

答案 1 :(得分:0)

请尝试以下操作(未经测试)

- lineinfile:
    dest: /tmp/sql
    line: "{{ inventory_hostname }};{{ item.version.value }};{{ item.edition.value }}"
  when: item.exists
  with_items:
    - "{ version: {{sql_version.results}}, edition: {{sql_edition.results}} }"
  delegate_to: localhost