
上QQ阅读APP看书,第一时间看更新
How to do it...
- Create a new file inside the group_vars directory called network.yml:
$ cat group_vars/network.yml
---
ntp_servers:
- 172.20.1.1
- 172.20.2.1
- Create a new templates directory and create a new ios_basic.j2 file with the following content:
$ cat templates/ios_basic.j2
hostname {{ hostname }}
!
{% for server in ntp_servers %}
ntp {{ server }}
{% endfor %}
!
- Create a new junos_basic.j2 file within the templates directory with the following content:
$ cat templates/junos_basic.j2
set system host-name {{ hostname }}
{% for server in ntp_servers %}
set system ntp server {{ server }}
{% endfor %}
- Create a new playbook called ansible_jinja2.yml with the following content:
---
- name: Generate Cisco config from Jinja2
hosts: localhost
gather_facts: no
tasks:
- name: Create Configs Directory
file: path=configs state=directory
- name: Generate Cisco config from Jinja2
hosts: cisco
gather_facts: no
tasks:
- name: Generate Cisco Basic Config
template:
src: "templates/ios_basic.j2"
dest: "configs/{{inventory_hostname}}.cfg"
delegate_to: localhost
- name: Generate Juniper config from Jinja2
hosts: juniper
gather_facts: no
tasks:
- name: Generate Juniper Basic Config
template:
src: "templates/junos_basic.j2"
dest: "configs/{{inventory_hostname}}.cfg"
delegate_to: localhost
- Run the Ansible playbook as shown here:
$ ansible-playbook -i hosts ansible_jinja2.yml