I dette eksempelet så skal vi sette opp standard oppsett for switchene vår med Ansible.
Noen av switchene er konfigurert med å kun tillate telnet.
Oppgaven vår blir å konfigurere switchene med å slå av telnet og sette opp SSH med en aksessliste.
Det er switch IOS_XE_switch_4 og IOS_switch_1 som har kun telnet aktivert.
Ved kjøre anible -m ping så kan vi bekrefte at vi har kontakt med switchene.
ansible -m ping IOS_XE_switch_3 --ask-vault-password Vault password: IOS_XE_switch_3 | SUCCESS => { "changed": false, "ping": "pong" }
ansible -m ios_command -a "commands='show version'" IOS_XE_switch_3 --ask-vault-password Vault password: IOS_XE_switch_3 | FAILED! => { "changed": false, "msg": "ssh connection failed: ssh connect failed: Connection refused"
--- - name: Aktivere ssh hosts: "{{ telnet_hosts }}" gather_facts: no tasks: - name: Konfigurere ssh ansible.netcommon.telnet: user: autom8 password: "{{ ansible_password }}" login_prompt: 'Username: ' prompts: - '[>#]' command: - configure terminal - ip ssh version 2 - ip domain-name autom8.no - crypto key generate rsa modulus 4096 - line vty 0 15 - transport input ssh
ansible-playbook playbook_telnet_ssh_basis_oppsett.yaml --ask-vault-password -e "telnet_hosts=IOS_XE_switch_3" Vault password: PLAY [Aktivere ssh] ******************************************************************************************************************************************************************************************************************** TASK [Konfigurere ssh] ***************************************************************************************************************************************************************************************************************** changed: [IOS_XE_switch_3] PLAY RECAP ***************************************************************************************************************************************************************************************************************************** IOS_XE_switch_3 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-playbook playbook_telnet_ssh_basis_oppsett.yaml --ask-vault-password -e "{"telnet_hosts": ["IOS_XE_switch_3", "IOS_switch_1"]}" Vault password: PLAY [Aktivere ssh] ******************************************************************************************************************************************************************************************************************** TASK [Konfigurere ssh] ***************************************************************************************************************************************************************************************************************** An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ConnectionRefusedError: [Errno 111] Connection refused fatal: [IOS_XE_switch_3]: FAILED! => {"msg": "Unexpected failure during module execution: [Errno 111] Connection refused", "stdout": ""} An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ConnectionRefusedError: [Errno 111] Connection refused fatal: [IOS_switch_1]: FAILED! => {"msg": "Unexpected failure during module execution: [Errno 111] Connection refused", "stdout": ""} PLAY RECAP ***************************************************************************************************************************************************************************************************************************** IOS_XE_switch_3 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0 IOS_switch_1 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
ansible -m ios_command -a "commands='show run | inc transport'" IOS_XE_switch_3 --ask-vault-password { "changed": false, "stdout": [ "transport input ssh transport input ssh transport input ssh" ], "stdout_lines": [ [ "transport input ssh", " transport input ssh", " transport input ssh" ] ] } ansible -m ios_command -a "commands='show run | inc transport'" IOS_switch_1 --ask-vault-password { "changed": false, "stdout": [ "transport input ssh transport input ssh transport input ssh" ], "stdout_lines": [ [ "transport input ssh", " transport input ssh", " transport input ssh" ] ] }
--- - name: Basis oppsett hosts: "{{ basis_oppsett_hosts }}" gather_facts: no vars: access_list: mgmt_tilgang tasks: - name: Oppretter standard akksess liste for ssh tilgang ios_config: lines: - "ip access-list standard {{ access_list }}" - name: Legger til IP adresser på aksessliste ios_config: lines: - permit 10.0.0.0 0.0.0.255 parents: "ip access-list standard {{ access_list }}"
ansible-playbook playbook_basis_oppsett.yaml --ask-vault-password -e "basis_oppsett_hosts=IOS_switch_1" Vault password: PLAY [Basis oppsett] ******************************************************************************************************************************************************************************************************************* TASK [Oppretter standard akksess liste for ssh tilgang] ******************************************************************************************************************************************************************************** changed: [IOS_switch_1] TASK [Legger til IP adresser på aksessliste] ******************************************************************************************************************************************************************************************* changed: [IOS_switch_1] PLAY RECAP ***************************************************************************************************************************************************************************************************************************** IOS_switch_1 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-playbook playbook_basis_oppsett.yaml --ask-vault-password -e "basis_oppsett_hosts=switcher" Vault password: PLAY [Basis oppsett] ******************************************************************************************************************************************************************************************************************* TASK [Oppretter standard akksess liste for ssh tilgang] ******************************************************************************************************************************************************************************** [WARNING]: To ensure idempotency and correct diff the input configuration lines should be similar to how they appear if present in the running configuration on device changed: [IOS_XE_switch_3] changed: [IOS_switch_2] changed: [IOS_XE_switch_4] ok: [IOS_switch_1] TASK [Legger til IP adresser på aksessliste] ******************************************************************************************************************************************************************************************* ok: [IOS_switch_1] changed: [IOS_XE_switch_3] changed: [IOS_XE_switch_4] changed: [IOS_switch_2] PLAY RECAP ***************************************************************************************************************************************************************************************************************************** IOS_XE_switch_3 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 IOS_XE_switch_4 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 IOS_switch_1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 IOS_switch_2 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
--- - name: Basis oppsett hosts: "{{ basis_oppsett_hosts }}" gather_facts: no vars: access_list: mgmt_tilgang tasks: - name: Oppretter standard akksess liste for ssh tilgang ios_config: lines: - "ip access-list standard {{ access_list }}" - name: Legger til IP adresser på aksessliste ios_config: lines: - permit 10.0.0.0 0.0.0.255 parents: "ip access-list standard {{ access_list }}" when: "'IOS_switcher' in group_names" - name: Legger til IP adresser på aksessliste ios_config: lines: - 10 permit 10.0.0.0 0.0.0.255 parents: "ip access-list standard {{ access_list }}" when: "'IOS_XE_switcher' in group_names"
ansible-playbook playbook_basis_oppsett.yaml --ask-vault-password -e "basis_oppsett_hosts=switcher" Vault password: PLAY [Basis oppsett] ******************************************************************************************************************************************************************************************************************* TASK [Oppretter standard akksess liste for ssh tilgang] ******************************************************************************************************************************************************************************** ok: [IOS_XE_switch_3] ok: [IOS_switch_2] ok: [IOS_switch_1] ok: [IOS_XE_switch_4] TASK [Legger til IP adresser på aksessliste] ******************************************************************************************************************************************************************************************* skipping: [IOS_XE_switch_3] skipping: [IOS_XE_switch_4] ok: [IOS_switch_2] ok: [IOS_switch_1] TASK [Legger til IP adresser på aksessliste] ******************************************************************************************************************************************************************************************* skipping: [IOS_switch_1] skipping: [IOS_switch_2] ok: [IOS_XE_switch_3] ok: [IOS_XE_switch_4] PLAY RECAP ***************************************************************************************************************************************************************************************************************************** IOS_XE_switch_3 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 IOS_XE_switch_4 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 IOS_switch_1 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 IOS_switch_2 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
vars: access_list: mgmt_tilgang access_list_ios_acls: ios_acls_mgmt_tilgang_acl - name: aksessliste med ios_acls tags: acl ios_acls: config: - afi: ipv4 acls: - name: "{{ access_list_ios_acls }}" acl_type: standard aces: - grant: permit source: address: 10.100.100.1 - grant: permit source: address: 10.0.0.0 wildcard_bits: 0.0.0.255 state: merged
vars: access_list_jinja2: jinja2_mgmt_tilgang_acl acl_list: - 10.0.0.0 0.0.0.255 - 10.0.1.0 0.0.0.255 - name: opprette aksessliste med jinja2 tags: acl_jinja2 ios_config: src: "acl.j2" match: line
ip access-list standard {{ access_list_jinja2 }}" {% for acl in acl_list %} permit {{ acl }} {% endfor %}
ip access-list standard {{ access_list_jinja2 }}" {% for acl in acl_list %} {{ 10 + loop.index0 * 10 }} permit {{ acl }} {% endfor %}
ip access-list standard {{ access_list_jinja2 }}" {% if 'IOS_XE_switcher' in group_names %} {% for acl in acl_list %} {{ 10 + loop.index0 * 10 }} permit {{ acl }} {% endfor %} {% endif %} {% if 'IOS_switcher' in group_names %} {% for acl in acl_list %} permit {{ acl }} {% endfor %} {% endif %}
ansible-playbook playbook_basis_oppsett.yaml --ask-vault-password -t acl_jinja2 -e "basis_oppsett_hosts=switcher" Vault password: PLAY [Basis oppsett] ************************************************************************************************************************************************************************************************* TASK [opprette aksessliste med jinja2] ******************************************************************************************************************************************************************************* ok: [IOS_XE_switch_3] ok: [IOS_switch_1] ok: [IOS_switch_2] ok: [IOS_XE_switch_4] PLAY RECAP *********************************************************************************************************************************************************************************************************** IOS_XE_switch_3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 IOS_XE_switch_4 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 IOS_switch_1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 IOS_switch_2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ip access-list standard {{ access_list_jinja2 }}" {% if 'IOS_XE_switcher' in group_names %} {% for acl in acl_list %} {{ 10 + loop.index0 * 10 }} permit {{ acl }} {% endfor %} {% endif %} {% if 'IOS_switcher' in group_names %} {% for acl in acl_list %} permit {{ acl }} {% endfor %} {% endif %} {% if 'IOS_XE_switcher' in group_names %} line vty 0 3 transport input ssh access-class {{ access_list_jinja2 }} in line vty 4 transport input ssh access-class {{ access_list_jinja2 }} in line vty 5 15 transport input ssh access-class {{ access_list_jinja2 }} in {% endif %} {% if 'IOS_switcher' in group_names %} line vty 0 2 transport input ssh access-class {{ access_list_jinja2 }} in line vty 3 4 transport input ssh access-class {{ access_list_jinja2 }} in line vty 5 15 transport input ssh access-class {{ access_list_jinja2 }} in {% endif %}
ansible-playbook playbook_basis_oppsett.yaml --ask-vault-password -t acl_jinja2 -e "basis_oppsett_hosts=switcher" Vault password: PLAY [Basis oppsett] ************************************************************************************************************************************************************************************************* TASK [opprette aksessliste med jinja2] ******************************************************************************************************************************************************************************* ok: [IOS_XE_switch_3] ok: [IOS_switch_1] ok: [IOS_switch_2] ok: [IOS_XE_switch_4] PLAY RECAP *********************************************************************************************************************************************************************************************************** IOS_XE_switch_3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 IOS_XE_switch_4 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 IOS_switch_1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 IOS_switch_2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0