Ansible's seven weapons and JSON, YAML, Jinja2 introduction

The seven weapons of ansible

The first

-ansible command, used to perform temporary work, must be mastered

The second

-ansible-doc is the documentation of the ansible module. There are detailed instructions and application case introductions for each module. The function is similar to the man of the Linux system and must be mastered

The third

-ansible-console is an interactive tool provided by ansible for users. Users can use the various commands built in ansible on the terminal virtualized by ansible-console like a shell. This provides a good use for users who are accustomed to using shell interaction. Experience

The fourth

-ansible-galaxy downloads a tool for managing Roles from github, similar to python's pip

The fifth

-ansbile-playbook is the most frequently used command in daily applications. The working mechanism: batch management is realized by reading the first written playbook file, which can be understood as an ansible task set composed according to certain conditions, which must be mastered

Sixth

-ansible-vault is mainly used for configuration file encryption. For example, the written playbook file contains sensitive information, unlike others who can view it at will, it can be used to encrypt/decrypt this file

Seventh

-ansible-pull
-Ansible has two working modes, pull/push, and the push mode is used by default. The pull and push working mode mechanisms are just the opposite.
-Usage scenario: There are a large number of machines that need to be configured, even if high concurrent threads are used, it still takes a lot of time.
-Usually used in the scenario of configuring a large number of machines, the flexibility is slightly lacking, but the efficiency can be improved almost infinitely, and the technical level and forward-looking planning of the operation and maintenance personnel are required to be higher.
================================================================================================================================================ ================================================== ================================================== ============================== Introduction to JSON

What is JSON

-JSON is a JavaScript object notation, which is a lightweight data exchange format based on text independence and language.-The
delimiter in JSON is limited to single quotes "'", parentheses "()", square brackets "[]" , Braces "{}", colon ": "and comma","

JSON features-JSON
is plain text-
JSON is "self-descriptive" (human readable)
-JSON has a hierarchical structure (there is a value in the value)
-JSON can be parsed by JavaScript

JSON syntax rules

-Data is in name/value pairs
-Data is separated by commas
-Curly brackets hold objects
-Square brackets hold arrays

The writing format of JSON data is: name/value pair

-The name/value pair includes the field name (in double quotes), followed by a colon, and then the value, for example: "诗仙": "李白"

Array of JSON syntax rules

{"Poet":
["Li Bai", "Du Fu", "Bai Juyi", "Li He"]
}

Compound complex type

{"Poet":
[{"Li Bai": "Poetry Fairy","Year":"Tang"},
{"Du Fu":"Poetry Sage","Year":"Tang"},
{"Bai Juyi":" Poetry Demon","Year":"Tang"},
{"Li He":"Poetry Ghost","Year":"Tang"}
]
}

Introduction to YAML

What
is YAML -is a highly readable format used to express data sequences-
YAML (YAML Ain't Markup Language)
-YAML refers to multiple languages, such as: C language, Python, Perl, etc., and from XML, Inspired by the data format of emails, Clark Evans first published this language in 2001. There are currently several programming languages ​​or scripting languages ​​that support this language.

YAML basic syntax

-The structure of YAML is displayed by spaces
-the array is represented by "-"
-the key-value pair is represented by ":"-
YAML uses a fixed indentation style to express the relationship of the data hierarchy
-generally there are two indentation levels The above spaces are composed of
-# means comment

  • Note:
    -Do not use tabs, indentation is one of the places that beginners can make mistakes
    -the same level of indentation must be aligned

YAML key value representation method

-Separated by a colon
-: There must be a space after the
-YAML key-value pair example

"诗仙":"李白""李白":
   "诗仙"

-Nesting of complex YAML key-value pairs

"诗人":
    "李白":"诗仙""诗人":
    "李白":
       "诗仙"

Array

["李白","杜甫","白居易","李贺"]

YAML array representation method
-use a dash and a space-
YAML array example

-"杜甫"
-"李白"
-"白居易"
-"李贺"

-Hash array compound expression

"诗人":
  -"李白"
  -"杜甫"
  -"白居易"
  -"李贺"

-Advanced compound expressions

 "诗人":
    - "李白":"诗仙"
      "年代":"唐"
    - "杜甫":"诗圣"
      "年代":"唐" 
    - "白居易":"诗魔"
      "年代":"唐"
    - "李贺":"诗鬼"
      "年代":"唐"

Introduction to Jinja2 templates

What is Jinja2

-Jinja2 is a Python-based template engine, which contains two parts: variables and expressions. Both of them will be replaced with values ​​when the template is evaluated. There are also tags in the template to control the logic of the template.

Why learn Jinja2 template

-Because the playbook template uses the PythonDEJinja2 module to process

Basic syntax of Jinja2 template

-Template expressions are all contained in the separator "{ {}}"
-Control statements are all contained in the separator "{% %}"
-The template supports comments, which are all contained in the separator "{# #} "the support block comment
- call variables
{ {}} // query connotation varname variable kibana -m Setup ansible
- calculation
{ {3}} + 2
- Analyzing
{ {. 1 in [l, 2,3]}}

Jinja2 template control statement

{% if name == '诗仙'%}
李白
{% eif name == '诗仙'%}
杜甫
{% eif name == '诗仙'%}
白居易
{% else %}

Li He
{% endif %}

{% if name == ... ... %}
   ... ...
{% elif name == '于谦' %}
  {% for method in [抽烟,喝酒,烫头]%}
     {
   
   {do method}}
  {% endfor %}
   ... ...
{% endif %}

=====================================================================================================

Jinja2 filter

-Variables can be modified by filters. Filters and variables are separated by a pipe symbol (|), or optional parameters can be passed in parentheses, multiple filters can be chained, and the output of the previous filter will be used as the next one Filter input

For example: -Encryption
-a string: { ('astr'|password_hash('sha512')))
-Filters will not be listed here, you can check the online documentation if you need it.
http://docs.jinkan.org/docs /jinja2/templates.html
#builtin-filters
++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++

Guess you like

Origin blog.csdn.net/weixin_45942735/article/details/104288465