From 107e1f297a7d86a57aba4962df7284838a11e5e1 Mon Sep 17 00:00:00 2001 From: Ilgar_Naghiyev Date: Wed, 4 Mar 2020 17:03:29 +0100 Subject: [PATCH] demo with templates --- README.md | 33 ++++++++++++++++++++++++++++++++- playbooks/network_playbook.yml | 7 +++++++ templates/network.j2 | 3 +++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 playbooks/network_playbook.yml create mode 100644 templates/network.j2 diff --git a/README.md b/README.md index e518ad2..e54db1d 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Red Hat Certified Specialist in Ansible Automation (EX407) Preparation Course - [LAB: Ansible Playbooks - Error Handling](#lab-ansible-playbooks---error-handling) - [Create and Use Templates to Create Customized Configuration Files](#create-and-use-templates-to-create-customized-configuration-files) - [Using Ansible Templates Lecture](#using-ansible-templates-lecture) - + - [Demo: Using Ansible Templates](#demo-using-ansible-templates) ## Understanding Core Components of Ansible @@ -939,4 +939,35 @@ Template Module Template File ![img](https://github.com/Bes0n/EX407-Ansible-Automation/blob/master/images/img18.png) + + +### Demo: Using Ansible Templates + +Our template `network.j2` file will look like this: +``` +My IP address is {{ ansible_default_ipv4.address }}. + +{{ ansible_distribution }} is my OS version. +``` + +- `ansible_default_ipv4.address` and `ansible_distribution` - are gathered facts during playbook run + +Cookbook for calling our template will look like this: +``` +--- +- hosts: labservers + tasks: + - name: deploy local net file + template: + src: /home/cloud_user/template/network.j2 + dest: /home/cloud_user/template/network.txt +``` + +Created file `/home/cloud_user/template/network.txt` after playbook run will be: +``` +My IP address is 142.21.46.232. + +RedHat is my OS version. +``` + diff --git a/playbooks/network_playbook.yml b/playbooks/network_playbook.yml new file mode 100644 index 0000000..9f2d60e --- /dev/null +++ b/playbooks/network_playbook.yml @@ -0,0 +1,7 @@ +--- +- hosts: labservers + tasks: + - name: deploy local net file + template: + src: /home/cloud_user/template/network.j2 + dest: /home/cloud_user/template/network.txt \ No newline at end of file diff --git a/templates/network.j2 b/templates/network.j2 new file mode 100644 index 0000000..55c3a6f --- /dev/null +++ b/templates/network.j2 @@ -0,0 +1,3 @@ +My IP address is {{ ansible_default_ipv4.address }}. + +{{ ansible_distribution }} is my OS version. \ No newline at end of file