Ansible register result of multiple commands

后端 未结 4 1740
醉话见心
醉话见心 2021-02-01 19:33

I was given a task to verify some routing entries for all Linux server and here is how I did it using an Ansible playbook

---
  - hosts: Linux
    serial: 1

            


        
4条回答
  •  春和景丽
    2021-02-01 19:54

    Starting in Ansible 1.6.1, the results registered with multiple items are stored in result.results as an array. So you can use result.results[0].stdout and so on.

    Testing playbook:

    ---
    - hosts: localhost
      gather_facts: no
      tasks:
        - command: "echo {{item}}"
          register: result
          with_items: [1, 2]
        - debug:
            var: result
    

    Result:

    $ ansible-playbook -i localhost, test.yml
    
    PLAY [localhost] ************************************************************** 
    
    TASK: [command echo {{item}}] ************************************************* 
    changed: [localhost] => (item=1)
    changed: [localhost] => (item=2)
    
    TASK: [debug ] **************************************************************** 
    ok: [localhost] => {
        "var": {
            "result": {
                "changed": true, 
                "msg": "All items completed", 
                "results": [
                    {
                        "changed": true, 
                        "cmd": [
                            "echo", 
                            "1"
                        ], 
                        "delta": "0:00:00.002502", 
                        "end": "2015-08-07 16:44:08.901313", 
                        "invocation": {
                            "module_args": "echo 1", 
                            "module_name": "command"
                        }, 
                        "item": 1, 
                        "rc": 0, 
                        "start": "2015-08-07 16:44:08.898811", 
                        "stderr": "", 
                        "stdout": "1", 
                        "stdout_lines": [
                            "1"
                        ], 
                        "warnings": []
                    }, 
                    {
                        "changed": true, 
                        "cmd": [
                            "echo", 
                            "2"
                        ], 
                        "delta": "0:00:00.002516", 
                        "end": "2015-08-07 16:44:09.038458", 
                        "invocation": {
                            "module_args": "echo 2", 
                            "module_name": "command"
                        }, 
                        "item": 2, 
                        "rc": 0, 
                        "start": "2015-08-07 16:44:09.035942", 
                        "stderr": "", 
                        "stdout": "2", 
                        "stdout_lines": [
                            "2"
                        ], 
                        "warnings": []
                    }
                ]
            }
        }
    }
    
    PLAY RECAP ******************************************************************** 
    localhost                  : ok=2    changed=1    unreachable=0    failed=0   
    

提交回复
热议问题