This scenario shows:
- how to create templates.
- You should have a look following lab, nodes that are created in that LAB, are using in ansible commands
- 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
- Open 'sshd_config_ubuntu.j2' file and add 'AllowUsers'.
nano sshd_config_ubuntu.j2
# add following
AllowUsers {{ ssh_users }}
- 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
- 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
- Add another task to use templates
- 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
- Create handlers and create 'main.yml' file
- name: restart_sshd
service:
name: sshd
state: restarted
- Run:
ansible-playbook site.yml
- 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.