Day: March 4, 2022

Learning – Ansible Quick Start

Learning - Ansible - Quick Start

Install (CentOS)

yum update -y
yum install epel-release -y
yum install ansible -y

Add managed hosts

Add following line in /etc/ansible/hosts

[linux]

45.56.72.153
45.79.56.223

[linux:vars]

ansible_user=root
ansible_password=P@ssword123

Disable host key checking

Edit ansible.cfg file as below

host_key_checking = false

Run module

ping

ansible linux -m ping

Run ad hoc command

ansible linux -a "cat /etc/os-release"
ansible linux -a "reboot"

Playbook

Playbook => Plays => Tasks

Servers

For example, following yaml file iluvnano.yml

---
  - name: iluvnano
    host: linux
    tasks:
      - name: ensure nano is there
        yum:
          name: nano
          state: latest

The ilvunano is Play, the ensure nano is there is Task.

Run playbook

ansible-playbook iluvnano.yml

If change the state to absent in iluvnano.ymland rerun above command, the nano will be removed.

Routers

In /etc/ansible/hosts file

[routers]

ios-xe-mgmt-latest.cisco.com
ios-xe-mgmt.cisco.com

[routes:vars]

ansible_user=developer
ansible_password=C1sco12345
ansible_connection=network_cli
ansible_network_os=ios
ansible_port=8181

In /etc/ansible/ansible.cfg

host_key_checking = false

Run commands

ansible routers -m ping
ansible routers -m ios_command -a "commands='show ip int brief'"

Playbook devnet.yml

---
  - name: General Config
    hosts: routers
    tasks:
    - name: Add Banner
      ios_banner:
        banner: login
        text: |
          Nicolas Cage is the
          Tiger King
        state: present
      - name: Add loopback
        ios_interface:
          name: Loopback21
          state: present

Run following command

ansible-playbook devnet.yml

To remove the changes, update the state in yaml file

state: absent

Run following command again

ansible-playbook devnet.yml

Then the routers will be modified.

show ip int brief
show run | beg banner

References

you need to learn Ansible RIGHT NOW!! (Linux Automation)
get started with Ansible Network Automation (FREE cisco router lab)