检查列表是否仅由Ansible中的值等于0组成

时间:2017-05-08 22:31:16

标签: ansible

我有一个来自PowerShell脚本的返回值数组,我想只有当数组中的所有项都是0时才执行命令。我有这个:

- name: check if computer is running DC services
  raw: Get-Service -name {{item}}
  with_items:
    - "adws"
    - "kdc"
    - "netlogon"
  register: services
  # ignore_errors: yes
  failed_when: services.stderr and 'Cannot find any service with service name ' not in services.stderr
  changed_when: False

- name: debug services
  debug:
    var=services

- name: copy check_ad.exe to DCs
  win_copy: src=check_ad.exe dest='{{nagios_client_install_path}}\\check_ad.exe'
  tags: ['win-nagios-client']
  when: item.rc == 0

但如果任何服务正在运行,则会运行copy check_ad.exe。如何在所有服务正在运行时将其更改为仅运行?

调试信息在这里:

ok: [server_name] => {
    "changed": false, 
    "services": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "changed": false, 
                "failed": false, 
                "failed_when_result": false, 
                "item": "adws", 
                "rc": 1, 
                "stderr": "Get-Service : Cannot find any service with service name 'adws'.\r\nAt line:1 char:1\r\n+ Get-Service -name adws\r\n+ ~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : ObjectNotFound: (adws:String) [Get-Service], Ser \r\n   viceCommandException\r\n    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell. \r\n   Commands.GetServiceCommand\r\n \r\n\r\n", 
                "stdout": "", 
                "stdout_lines": []
            }, 
            {
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "changed": false, 
                "failed": false, 
                "failed_when_result": false, 
                "item": "kdc", 
                "rc": 1, 
                "stderr": "Get-Service : Cannot find any service with service name 'kdc'.\r\nAt line:1 char:1\r\n+ Get-Service -name kdc\r\n+ ~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : ObjectNotFound: (kdc:String) [Get-Service], Serv \r\n   iceCommandException\r\n    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell. \r\n   Commands.GetServiceCommand\r\n \r\n\r\n", 
                "stdout": "", 
                "stdout_lines": []
            }, 
            {
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "changed": false, 
                "failed": false, 
                "failed_when_result": false, 
                "item": "netlogon", 
                "rc": 0, 
                "stderr": "", 
                "stdout": "\r\nStatus   Name               DisplayName                           \r\n------   ----               -----------                           \r\nRunning  Netlogon           netlogon                              \r\n\r\n\r\n", 
                "stdout_lines": [
                    "", 
                    "Status   Name               DisplayName                           ", 
                    "------   ----               -----------                           ", 
                    "Running  Netlogon           netlogon                              ", 
                    "", 
                    ""
                ]
            }
        ]
    }
}

1 个答案:

答案 0 :(得分:1)

由于rc值是整数,您可以将它们相加并将结果与​​0进行比较:

- name: copy check_ad.exe to DCs
  win_copy: src=check_ad.exe dest='{{nagios_client_install_path}}\\check_ad.exe'
  tags: ['win-nagios-client']
  when: services.results | map(attribute='rc') | sum == 0

更通用的方法(不仅仅是整数)是使用difference filter删除所有出现的0(从上面的映射rc的结果列表中删除)并检查其余清单的大小。

如果原始列表包含其他元素,则结果值为正。