Beispiel Playbook exec_command.yaml:
--- - hosts: switch1 connection: local gather_facts: False vars: cli: host: "{{ inventory_hostname }}" username: "{{ ansible_user }}" password: "{{ ansible_ssh_pass }}" tasks: - name: Call show inventory ios_command: commands: show inventory provider: "{{ cli }}" register: output - name: Store output to file copy: content="{{ output.stdout[0] }}" dest="out/inventory_{{ inventory_hostname }}.txt"
Mit „ios_command“ lassen sich Kommandos auf der CLI ausführen und die Ausgabe weiterverwenden. In dem Beispiel wird die Ausgabe in einer Datei gespeichert.
ansible-playbook -i hosts.yaml exec_command.yaml -v PLAY [switch1] ********************************************************************************* TASK [Call show inventory] ********************************************************************* ok: [switch1] => {"changed": false, "stdout": ["NAME: \"1\", DESCR: \"WS-C2960-8TC-S\"\nPID: WS-C2960-8TC-S , VID: V01 , SN: FOCXXXXXXXX"], "stdout_lines": [["NAME: \"1\", DESCR: \"WS-C2960-8TC-S\"", "PID: WS-C2960-8TC-S , VID: V01 , SN: FOCXXXXXXXX"]]} TASK [Store output to file] ******************************************************************** changed: [switch1] => {"changed": true, "checksum": "bb873c38a92c5fcf4f337c4730bf626ba36ac558", "dest": "out/inventory_switch1.txt", "gid": 0, "group": "root", "md5sum": "f7a2aca1b08cdaeb5564d1e368b6ff19", "mode": "0644", "owner": "root", "size": 87, "src": "/root/.ansible/tmp/ansible-tmp-1516308679.35-272245399379253/source", "state": "file", "uid": 0} PLAY RECAP ************************************************************************************* switch1 : ok=2 changed=1 unreachable=0 failed=0
cat out/inventory_switch1.txt
NAME: "1", DESCR: "WS-C2960-8TC-S" PID: WS-C2960-8TC-S , VID: V01 , SN: FOCXXXXXXXX