簡體   English   中英

ansible 中的迭代和計數變量

[英]iteration and count variable in ansible

我正在嘗試簡化代碼

    - name: Report status
      ansible.builtin.debug:
        msg:
          - "{{ agents.results.0.services.0.display_name }} is {{ agents.results.0.services.0.state }}"
          - "{{ agents.results.1.services.0.display_name }} is {{ agents.results.1.services.0.state }}"
          - "{{ agents.results.2.services.0.display_name }} is {{ agents.results.2.services.0.state }}"
          - "{{ agents.results.3.services.0.display_name }} is {{ agents.results.3.services.0.state }}"
          - "{{ agents.results.4.services.0.display_name }} is {{ agents.results.4.services.0.state }}"
          - "{{ agents.results.5.services.0.display_name }} is {{ agents.results.5.services.0.state }}"
          - "{{ agents.results.6.services.0.display_name }} is {{ agents.results.6.services.0.state }}"

我需要獲取每個項目的名稱和服務 state。

我試過 with_sequence 但這會導致錯誤:

{"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'item'
    - ansible.builtin.debug:
        msg: 
          - "{{ agents.results.item.services.0.display_name }} is {{ agents.results.item.services.0.state }}"
      with_sequence: end=6

我嘗試過的每個建議

    - ansible.builtin.debug:
        msg:
          - "{{ agents.results[item].services.0.display_name }} is {{ agents.results[item].services.0.state }}"
      with_sequence: end=6

這會導致錯誤:“msg”:“任務包含一個帶有未定義變量的選項。錯誤是:‘list object’沒有屬性‘1’

另外,嘗試過:

    - ansible.builtin.debug:
        msg: "{{ item.services.0.display_name }} is {{ item.services.0.state }}"
      loop: "{{ agents.results }}"

它提供了比要求更多的信息,例如

ok: [100.71.76.4] => (item={'changed': False, 'invocation': {'module_args': {'name': 'CSFalconService'}}, 'exists': True, 'services': [{'checkpoint': 0, 'controls_accepted': ['stop', 'pre_shutdown'], 'dependencies': [], 'dependency_of': [], 'description': 'Helps protect the system from malicious activities', 'desktop_interact': False, 'display_name': 'CrowdStrike Falcon Sensor Service', 'error_control': 'normal', 'failure_actions': [{'type': 'restart', 'delay_ms': 60000}, {'type': 'restart', 'delay_ms': 60000}, {'type': 'restart', 'delay_ms': 60000}], 'failure_actions_on_non_crash_failure': False, 'failure_command': None, 'failure_reboot_msg': None, 'failure_reset_period_sec': 86400, 'launch_protection': 'antimalware_light', 'load_order_group': '', 'name': 'CSFalconService', 'path': '"C:\\Program Files\\CrowdStrike\\CSFalconService.exe"', 'pre_shutdown_timeout_ms': 180000, 'preferred_node': None, 'process_id': 7164, 'required_privileges': [], 'service_exit_code': 0, 'service_flags': [], 'service_type': 'win32_own_process', 'sid_info': 'none', 'start_mode': 'auto', 'state': 'started', 'triggers': [], 'username': 'NT AUTHORITY\\SYSTEM', 'wait_hint_ms': 0, 'win32_exit_code': 0}], 'failed': False, 'item': 'CSFalconService', 'ansible_loop_var': 'item'}) => {
    "msg": "CrowdStrike Falcon Sensor Service is started"
}


ok: [100.71.76.4] => (item={'changed': False, 'invocation': {'module_args': {'name': 'Tanium Client'}}, 'exists': True, 'services': [{'checkpoint': 0, 'controls_accepted': ['stop'], 'dependencies': [], 'dependency_of': [], 'description': 'Tanium Client', 'desktop_interact': False, 'display_name': 'Tanium Client', 'error_control': 'normal', 'failure_actions': [{'type': 'restart', 'delay_ms': 60000}, {'type': 'restart', 'delay_ms': 60000}, {'type': 'none', 'delay_ms': 0}], 'failure_actions_on_non_crash_failure': False, 'failure_command': None, 'failure_reboot_msg': None, 'failure_reset_period_sec': 86400, 'launch_protection': 'none', 'load_order_group': '', 'name': 'Tanium Client', 'path': '"C:\\Program Files (x86)\\Tanium\\Tanium Client\\TaniumClient.exe" --service', 'pre_shutdown_timeout_ms': 10000, 'preferred_node': None, 'process_id': 5324, 'required_privileges': [], 'service_exit_code': 0, 'service_flags': [], 'service_type': 'win32_own_process', 'sid_info': 'none', 'start_mode': 'auto', 'state': 'started', 'triggers': [], 'username': 'NT AUTHORITY\\SYSTEM', 'wait_hint_ms': 0, 'win32_exit_code': 0}], 'failed': False, 'item': 'Tanium Client', 'ansible_loop_var': 'item'}) => {
    "msg": "Tanium Client is started"
}

實際上,我只需要 msg 部分。

首先,您應該閱讀ansible FAQ 中的專題文章 因此,您上述問題的直接答案是:

"{{ agents.results[item | int].services.0.display_name }} is {{ agents.results[item].services.0.state }}"

請注意int過濾器將變量轉換為with_sequence實際上會生成字符串。


但接下來,你為什么要把事情搞得這么復雜? 只需循環結果:

- ansible.builtin.debug:
    msg: "{{ item.services.0.display_name }} is {{ item.services.0.state }}"
  loop: "{{ agents.results }}"

如果每個單獨的項目都很大,並且您想減少每次循環迭代時 Ansible 顯示的信息量,請參閱循環控制 - 限制循環 output

關於

...提供了比要求更多的信息...實際上,我只需要msg部分。

你可以看看Limiting loop output 和label

當遍歷復雜的數據結構時,任務的控制台 output 可能會非常龐大。 要限制顯示的 output,請將label指令與loop_control一起使用。

並使用類似的東西

  loop_control:
    label: "{{ item.name }}" # or item.item

對於你的循環,為了減少 header。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM