Today, I will continue to introduce the knowledge related to Linux operation and maintenance. The main content of this article is Ansible's Template template basis.
1. Ansible Template concept
Ansible's Playbook supports the use of Template templates. The Template template is similar to the copy module when used. The difference is that when Ansible's Playbook is executed, some key content in the file can be flexibly set to be copied according to certain conditions. At the same time, the use of Template in Ansible's Playbook supports conditional judgment, loop, logical operation, comparison, etc., which enhances the flexibility of configuration.
In Ansible, the Template template is configured using the jinjia2 language, and the suffix of the template file is .j2.
2. Simple use of Ansible Template templates
Below, we use a simple example to demonstrate the use of templates in Ansible's Playbook. Try to control the controlled device to install the Apache service, and change the Apache open port number to 81.
We first install Apache on the Ansible device, then copy the Apache configuration file /etc/httpd/conf/httpd.conf to the /etc/ansible/ directory and modify it to httpd.conf.j2. vim opens the file, finds the Listen, and modifies it to the following content:
The above template file defines a listen_port variable with double brackets. We need to assign a value to this variable when calling Playbook, and then this variable will replace the template file. variables in .
Then edit the Playbook as follows:
---
- hosts: exp
remote_user: root
tasks:
- name: Install Apache by YUM
yum: name=httpd state=installed
- name: Config Apache
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
After that, we make a call to the Playbook as follows:
ansible-playbook tmp.yml -e "listen_port=81"
The execution result is as follows:
Three, Ansible Tmeplate template iteration
Ansible also supports the use of iterative variables. The iterable data type can be a list or a dictionary. The iteration example in Ansible is as follows:
---
- hosts: exp
remote_user: root
tasks:
- name: Greate new group
group: name={
{
item }} state=present
with_items:
- group1
- group2
- group3
- name: Greate new user
user: name={
{
item.user }} group ={
{
item.group }} state=present
with_items:
- {
user: "user1", group: "group1"}
- {
user: "user2", group: "group2"}
- {
user: "user3", group: "group3"}
In the above Playbook, the two tasks use the list and dictionary data respectively, so that in one task, the following data can be iterated to execute three tasks. Therefore, the first task is equivalent to executing 3 times, and each execution calls a value in the item below. The second task is similar to the first task.
The execution result of the above Palybook is as follows:
After the execution, the client device is as follows:
It can be seen that our iteration variable is executed successfully!
Originality is not easy, please indicate the source for reprinting: https://blog.csdn.net/weixin_40228200