Strony

Friday, 20 April 2018

Accessing Arista via Ansible

This blog entry is regarding accessing Arista device via Ansible.

Topics which will be covered:
- preparation of Ansible files which will allow for accessing Arista device (playbook, inventory, configuration)
- preparation of Docker container with Ansible installed (needed files for accessing Arista device will be placed in container)

Prerequisites:
- Docker container environment
- Arista switch with management IP set (IP of Arista need to be accessible from Docker conatiner)
Arista vEOS with IP 192.168.42.110 was used in example.

Command which will be send to Arista via Ansible - "show version" (eos_command module will be used, access to Arista will be done via SSH).

1. User creation on Arista

Command for user creation in Arista (user used by Ansible for acessing device):
username ansibleuser privilege 15 secret ansiblepassword

2. Files and folders

File and folder structure which need to be prepared on system where Docker image with Ansible will be build.

Required file and folder structure:
[test@localhost for_docker]$ tree -f -i
.
./CentOS_Ansible
./CentOS_Ansible/Dockerfile
./CentOS_Ansible/src
./CentOS_Ansible/src/ansible.cfg
./CentOS_Ansible/src/arista-playbook.yaml
./CentOS_Ansible/src/inventory

2 directories, 4 files

Content of Dockerfile:
[test@localhost Centos_Ansible]$ cat Dockerfile
FROM centos:centos7

RUN yum -y update; yum clean all
RUN yum -y install epel-release; yum clean all
RUN yum -y install gcc python python-devel python-pip openssh-clients ; yum clean all

RUN pip install --upgrade pip
RUN pip install ansible

RUN mkdir /arista_ansible
COPY ./src/* /arista_ansible/
WORKDIR /arista_ansible

CMD ["sleep","10000000"]

Content of ansible.cfg:

[test@localhost src]$ cat ansible.cfg
[defaults]
host_key_checking = False

Content of arista-playbook.yaml:

[test@localhost src]$ cat arista-playbook.yaml
- name: Playbook for Arista
  hosts: veos
  gather_facts: no
  connection: local

  tasks:
  - name: show version
    eos_command:
      provider:
        username: ansibleuser
        password: ansiblepassword
      commands: show version
    register: output
  - debug: var=output

Content of inventory:
[test@localhost src]$ cat inventory
[veos]
192.168.42.110

3. Preparing Docker container

Next docker image with Ansible will be build. Command need to be executed from within prepared folder "Centos_Ansible".
sudo docker build -f Dockerfile -t "centos_ansible" .

When docker image is ready, docker container can be launched:
sudo docker run -d -t centos_ansible

Command which can be used for retrieving id of launched docker container:
sudo docker ps

Command for accessing shell of prepared container (value for container_id, need to be retrieved using "sudo docker ps"):
sudo docker exec -i -t container_id /bin/bash

Command for running ansible playbook from within prepared container (command need to be run from folder /arista_ansible)
ansible-playbook arista-playbook.yaml -i inventory

Example output:

No comments:

Post a Comment