Ansible detailed explanation (11) - Ansible Template advanced control

Today, I will continue to introduce the relevant knowledge of Linux operation and maintenance. The main content of this article is the advanced control of Ansible's Template template.
In the above Ansible detailed explanation (10) - Ansible Template template foundation , we talked about some basic uses of Ansible. In fact, Ansible's Template allows for more sophisticated controls. Today, we will learn the following advanced controls of Ansible Template.

1. When statement of Ansible Template

The when statement of Ansible Template can be used for conditional testing. The introduction of when can make Ansible Playbook execute different commands according to different conditions. An example of a when statement in a typical Ansible Playbook is as follows:

---
- hosts: exp
  remote_user: root
  tasks:
  - name: Install Apache
    yum: name=httpd state=installed
  - name: Config CentOS6 Apache
    copy: src=/etc/ansible/centos6_httpd.conf dest=/etc/httpd/conf/httpd.conf
    when: ansible_distribution_major_version == "6"
  - name: Config CentOS7 Apache
    copy: src=/etc/ansible/centos7_httpd.conf dest=/etc/httpd/conf/httpd.conf
    when: ansible_distribution_major_version == "7"
  - name: Start Apache
    service: name=httpd state=started

The above Ansible Playbook can install the Apache service on the client, and install different Apache configuration files according to the client system version.
The execution result of the above command is as follows:
insert image description here

2. The for statement of Ansible Template

Ansible's Template also supports the use of for, which means to iterate over parts of the Template. An example of using for in a typical Ansible Template is as follows:

---
- hosts: exp
  remote_user: root
  vars:
   nginx_vhosts:
    - vhost1:
      port: 81
      server_name: "web1.pzz.com"
      root: "/var/www/nginx/web1"
    - vhost2:
      port: 82
      server_name: "web2.pzz.com"
      root: "/var/www/nginx/web2"
    - vhost3:
      port: 83
      server_name: "web3.pzz.com"
      root: "/var/www/nginx/web3"
  tasks:
  - name: install Nginx
    yum: name=nginx state=installed
  - name: config nginx
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  - name: Start Nginx
    service: name=nginx state=started

The key parts of the template file are as follows:

    {
    
    % for vhost in nginx_vhosts %}
    server{
    
    
        listen {
    
    {
    
     vhost.port }};
        server_name {
    
    {
    
     vhost.server_name }};
        root {
    
    {
    
     vhost.root }};

}
    {
    
    % endfor %}

In the above example, the function we implement is to configure the virtual host of Nginx. The execution process of the above Playbook is as follows:
insert image description here
After the above example is executed, the configuration file generated on the client side is as follows: After the
insert image description here
client opens the Nginx service, the result is as follows:
insert image description here

3. The for if statement of Ansible Template

On the basis of the for statement, Ansible's Template can also introduce if, which can make judgments based on conditions, flexibly generate configuration files, and perform more complex server configurations. An example of a typical for if statement in Ansible Template is as follows:

---
- hosts: exp
  remote_user: root
  vars:
   nginx_vhosts:
    - vhost1:
      port: 81
      server_name: "web1.pzz.com"
      root: "/var/www/nginx/web1"
    - vhost2:
      port: 82
      server_name: "web2.pzz.com"
      root: "/var/www/nginx/web2"
    - vhost3:
      server_name: "web3.pzz.com"
      root: "/var/www/nginx/web3"
  tasks:
  - name: install Nginx
    yum: name=nginx state=installed
  - name: config nginx
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

The nginx.conf.j2 template file section is as follows:

    {
    
    % for vhost in nginx_vhosts %}
    server{
    
    
        {
    
    % if vhost.port is defined%}
        listen {
    
    {
    
     vhost.port }};
        {
    
    % else %}
        listen 88;
        {
    
    % endif %}
        server_name {
    
    {
    
     vhost.server_name }};
        root {
    
    {
    
     vhost.root }};

}
    {
    
    % endfor %}

In the above configuration, vhost is a custom variable, and the value of this variable is iteratively passed in when the Playbook of the template is called. vhost.port, vhost.server_name and vhost.root are variables passed in when Playbook defines nginx_vhosts. Single curly braces are for or if statement identifiers, and double curly braces are variable identifiers.
After completing the above definition, Ansible executes as follows:
insert image description here
After execution, the /etc/nginx/nginx.conf file generated on the controlled end is as follows:
insert image description hereIt can be seen that in this configuration file, the server part is Ansible's Playbook and Constant iteration of variables in Template.
In this way, after we start the Nginx service, the result is as follows:
insert image description here
Therefore, it can be seen that the for and if statement of our Ansible Template is successfully configured!
Originality is not easy, please indicate the source for reprinting: https://blog.csdn.net/weixin_40228200

Guess you like

Origin blog.csdn.net/weixin_40228200/article/details/123538727