2016-04-07 3 views
0

Ich habe ein Beispiel-Playbook In dem bin ich Matching Host von zwei ansible Variable mit with_nested. Diese beiden Variablen im JSON-Format. Datensätze stimmen korrekt überein. Aber aufgrund von Änderungen in ansible 2.x zeigt es sowohl übereinstimmende als auch nicht übereinstimmende Datensätze in der Registervariablen an.Erhalte die Anzahl der übereinstimmenden Bedingungen der Registervariablen in Ansible Playbook

--- 

- hosts: 127.0.0.1 
    connection: local 

    tasks: 

    - shell: "echo {{ item[0] }}" 
     with_nested: 
     - [{"host": "host1","description": "This is host1 server"}, {"host": "host2","description": "This is host2 server"}] 
     - [{"host_name": "host1"},{"host_name": "host2"},{"host_name": "host3"},{"host_name":"host4"}] 
     register: all_hosts 
     when: item[0].host == item[1].host_name 

    - debug: var=all_hosts.results 

Ausgabe von all_hosts

"all_hosts.results": [ 
     { 
      "_ansible_no_log": false, 
      "changed": true, 
      "cmd": "echo {u'host': u'host1', u'description': u'This is host1 server'}", 
      "delta": "0:00:00.005456", 
      "end": "2016-04-07 02:34:37.151824", 
      "invocation": { 
       "module_args": { 
        "_raw_params": "echo {u'host': u'host1', u'description': u'This is host1 server'}", 
        "_uses_shell": true, 
        "chdir": null, 
        "creates": null, 
        "executable": null, 
        "removes": null, 
        "warn": true 
       }, 
       "module_name": "command" 
      }, 
      "item": [ 
       { 
        "description": "This is host1 server", 
        "host": "host1" 
       }, 
       { 
        "host_name": "host1" 
       } 
      ], 
      "rc": 0, 
      "start": "2016-04-07 02:34:37.146368", 
      "stderr": "", 
      "stdout": "{uhost: uhost1, udescription: uThis is host1 server}", 
      "stdout_lines": [ 
       "{uhost: uhost1, udescription: uThis is host1 server}" 
      ], 
      "warnings": [] 
     }, 
     { 
      "_ansible_no_log": false, 
      "changed": false, 
      "item": [ 
       { 
        "description": "This is host1 server", 
        "host": "host1" 
       }, 
       { 
        "host_name": "host2" 
       } 
      ], 
      "skip_reason": "Conditional check failed", 
      "skipped": true 
     }, 
     { 
      "_ansible_no_log": false, 
      "changed": false, 
      "item": [ 
       { 
        "description": "This is host1 server", 
        "host": "host1" 
       }, 
       { 
        "host_name": "host3" 
       } 
      ], 
      "skip_reason": "Conditional check failed", 
      "skipped": true 
     }, 
     { 
      "_ansible_no_log": false, 
      "changed": false, 
      "item": [ 
       { 
        "description": "This is host1 server", 
        "host": "host1" 
       }, 
       { 
        "host_name": "host4" 
       } 
      ], 
      "skip_reason": "Conditional check failed", 
      "skipped": true 
     }, 
     { 
      "_ansible_no_log": false, 
      "changed": false, 
      "item": [ 
       { 
        "description": "This is host2 server", 
        "host": "host2" 
       }, 
       { 
        "host_name": "host1" 
       } 
      ], 
      "skip_reason": "Conditional check failed", 
      "skipped": true 
     }, 
     { 
      "_ansible_no_log": false, 
      "changed": true, 
      "cmd": "echo {u'host': u'host2', u'description': u'This is host2 server'}", 
      "delta": "0:00:00.005470", 
      "end": "2016-04-07 02:34:37.310095", 
      "invocation": { 
       "module_args": { 
        "_raw_params": "echo {u'host': u'host2', u'description': u'This is host2 server'}", 
        "_uses_shell": true, 
        "chdir": null, 
        "creates": null, 
        "executable": null, 
        "removes": null, 
        "warn": true 
       }, 
       "module_name": "command" 
      }, 
      "item": [ 
       { 
        "description": "This is host2 server", 
        "host": "host2" 
       }, 
       { 
        "host_name": "host2" 
       } 
      ], 
      "rc": 0, 
      "start": "2016-04-07 02:34:37.304625", 
      "stderr": "", 
      "stdout": "{uhost: uhost2, udescription: uThis is host2 server}", 
      "stdout_lines": [ 
       "{uhost: uhost2, udescription: uThis is host2 server}" 
      ], 
      "warnings": [] 
     }, 
     { 
      "_ansible_no_log": false, 
      "changed": false, 
      "item": [ 
       { 
        "description": "This is host2 server", 
        "host": "host2" 
       }, 
       { 
        "host_name": "host3" 
       } 
      ], 
      "skip_reason": "Conditional check failed", 
      "skipped": true 
     }, 
     { 
      "_ansible_no_log": false, 
      "changed": false, 
      "item": [ 
       { 
        "description": "This is host2 server", 
        "host": "host2" 
       }, 
       { 
        "host_name": "host4" 
       } 
      ], 
      "skip_reason": "Conditional check failed", 
      "skipped": true 
     } 
    ] 

So gibt es zwei passende Datensätze gibt. Gibt es Möglichkeiten, diese übereinstimmenden Datensätze zu zählen?

Etwas

 - debug: msg="Task if matching record count more than one" 
     when: all_hosts.changed | length > 0 

Antwort

1

Wollen Sie wirklich die Zählung oder will, muss es überprüfen, ob mindestens ein passender Datensatz ist? Wie wäre es mit:

- debug: msg="Task if matching record count more than one" 
    when: all_hosts.changed 
+0

Ich möchte nur in einer Bedingung überprüfen, ob passende Datensätze vorhanden sind oder nicht. Aber als Array-Typ funktioniert 'all_hosts.changed' nicht für mich. Um irgendeine Aufgabe auszuführen, die auf einer Übereinstimmung beruht, muss ich 'all_hosts' in' with_items' setzen. – Roopendra

+0

'all_hosts' ist ein Diktat und' all_hosts' hat einen Schlüssel mit dem Namen 'changed'. 'all_hosts.results' ist ein Array. Hast du versucht? Funktioniert gut für mich in Ansible 1.9.2 – helloV

+0

Ah .. ja du hast mich noch einmal gerettet :) Ich war sehr geschlossen. – Roopendra

Verwandte Themen