Skip to content

Latest commit

 

History

History
106 lines (74 loc) · 2.83 KB

Templates.md

File metadata and controls

106 lines (74 loc) · 2.83 KB

LAB: Templates

This scenario shows:

  • how to create templates.

Prerequisite

Steps

  • Create 'templates' directory under the '/roles/base/'
  • Copy 'sshd_config' file under 'templates' and create jinja2 file (.j2)
mkdir templates
cd templates
cp /etc/ssh/sshd_config sshd_config_ubuntu.j2

image

  • Open 'sshd_config_ubuntu.j2' file and add 'AllowUsers'.
nano sshd_config_ubuntu.j2
# add following
AllowUsers {{ ssh_users }}

image

  • Go to 'host_vars' directory,
  • Change the content of this host_vars files
nano 172.21.69.156.yml
# add following
ssh_users: "newuser2022"
ssh_template_file: sshd_config_ubuntu.j2

image

  • File contents:
apache_package_name: apache2
apache_service: apache2
php_package_name: libapache2-mod-php
ssh_users: "newuser2022"
ssh_template_file: sshd_config_ubuntu.j2

image

  • Add another task to use templates

image

  • Open and add ('nano main.yml')
  • After generating sshh_config file, it triggers handler (restart_ssh)
- name: generate sshd_config file using templates
  tags: ssh
  template:
    src: "{{ ssh_template_file }}"
    dest: /etc/ssh/sshd_config
    owner: root
    group: root
    mode: 0644
  notify: restart_sshd

image

  • Create handlers and create 'main.yml' file
- name: restart_sshd
  service:
    name: sshd
    state: restarted

image

  • Run:
ansible-playbook site.yml

image

  • After running playbook, when we go to the 'node1' to see the content of sshd_config ('nano /etc/ssh/sshd_config').
  • 'AllowUsers newuser2022' is added.

image

Reference