Network Automation Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. Inside the ch1_ansible folder, create a new folder called roles and create a new role called basic_config, as shown here:
$ mkdir roles
$ cd roles
$ ansible-galaxy init basic_config
  1. Update the basic_config/vars/main.yml file with the following variable:
$ cat roles/basic_config/vars/main.yml

---
config_dir: basic_config
  1. Update the basic_config/tasks/main.yml file with the following tasks:
$ cat roles/basic_config/tasks/main.yml

---
- name: Create Configs Directory
file:
path: "{{ config_dir }}"
state: directory
run_once: yes

- name: Generate Cisco Basic Config
template:
src: "{{os}}.j2"
dest: "{{config_dir}}/{{inventory_hostname}}.cfg"
  1. Inside the basic_config/templates folder, create the following structure:
$ tree roles/basic_config/templates/

roles/basic_config/templates/
├── ios.j2
└── junos.j2

$ cat roles/basic_config/templates/ios.j2
hostname {{ hostname }}
!
{% for server in ntp_servers %}
ntp {{ server }}
{% endfor %}
  1. Create a new playbook, pb_ansible_role.yml, with the following content to use our role:
$ cat pb_ansible_role.yml
---
- name: Build Basic Config Using Roles
hosts: all
connection: local
roles:
- basic_config