Ansible playbook component, ansibleplaybook

Source: Internet
Author: User
Tags ansible playbook

Ansible playbook component, ansibleplaybook

Core elements of playbook:

Tasks: Task

Variables: Variable

Templates: Template

Handlers: Processor

Roles: Role

Organization format: YAML (also known as a Markup Language)

Syntax: (the list is represented by a horizontal bar. Key-value pairs are separated by colons, and other key-value pairs can be nested in key-value pairs)

Example: name: tom

Age: 21

Gender: Male

Spourse:

Name: lily

Gender: female

Children:

-Name: susan

Age: 2

Gender: female

-Name: sunny

Age: 10

Gender: male

List: Use-yindao

Dictionary: contains multiple k: v Key-value pairs. For example, {name: tom age: 21 gender: male} indicates a dictionary, which can also be nested.

A dictionary is nested in spourse, and children contains two lists. Each list contains another dictionary.

 

Create a playbook file: vim/root/first. yml (these files must end with. yml or. yaml)

The general format is:

-Hosts:

Vars:

Remote_user:

Tasks:

Handlers:

Host defines a single host or group, vars defines variables, remote_user defines remote users who execute commands, tasks defines which commands to execute, and handlers defines which calls

Processor

 

Is a simple example:

How can I run my defined scripts? Use the ansible-playbook command, for example, ansible-playbook/root/first. yml.

Variable:

Variable name: it may consist of letters, numbers, and underscores. It can only start with a letter.

Variable type: facts (built-in variable)

Custom variables: (1) pass through command lines, (2) pass through roles, (3) host variables, (4) group variables

Facts: Host attribute information sent back by the remote host, which is saved in the ansible variable

For example, ansible 192.168.238.170-m setup is used to obtain the attribute information on the remote host.

 

Pass through the command line: ansible-playbook test. yml -- extra-vars "host = www user = tom" (if the variable defined here is already in the script, it will be

Overwrite)

 

Host variable: defined in/etc/ansible/hosts, as shown in figure

  

Name = tom is the variable separately defined for host 192.168.238.168 (host variable)

 

Group variable: [group_name: vars]

Var1 = value

Var2 = value

 

Inventory parameter: When ansible connects to the remote host specified in inventory (/etc/ansible/hosts) based on ssh, the attribute specified by this parameter is performed.

Ansible_ssh_port: port used by the remote host

Ansible_ssh_user: User used to connect to the remote host

Ansible_ssh_pass is the password used to connect to the remote host (it is recommended to use key-based authentication instead of using this option)

Example: vim/etc/ansible/hosts

 

 

The second Script: vim/root/second. yml

The method for calling variables in playbook is {variable }}

 

Conditional test: You can add the when clause after a task to implement the conditional test function. For example:

Ansible_ OS _family is the property information built in facts.

 

Iteration:

Call the built-in item variable in a task. Use the with_items statement after a task to define the element list.

 

For example:

-Name: add four users

User: name = {item} state = present

With_items:

-Testuser1

-Testuser2

-Testuser3

-Testuser4

 

During iteration, each element in the list can be in dictionary format.

For example:

-Name: add two users

User: name = {item. name} state = present groups = {item. groups }}

With_items:

-{Name: 'testuser5', groups: 'wheel '}

 

-{Name: 'testuser6', groups: 'root '}

 

Handlers: it is also a task, but the execution will be triggered only when the following conditions are met:

Vim/root/fourth. yml

 

The above means that the copied files in the copy process are different from those on the remote host, and handlers is called through ipvy to restart the httpd service.

 

Templates: used to generate a text file (configuration file). The jinja2 expression can be used in the template file. The expression must be defined in {}. You can also simply replace the variable

Change;

Example: vim/etc/ansible/hosts

 

Variable with http_port added

 

Next: vim/root/httpd. conf

 

The Listen port uses {http_port} to call this variable.

 

Next: vim/root/fourth. yml

 

Modify copy to template

Ansible-playbook/root/fourth. yml, 192.168.238.168 will listen to port 8080 instead of the default port 80.

 

 

Roles: roles is used to achieve "code reuse". roles are playbook elements (variables, tasks, templates,

Handlers); can be called directly by playbook in role name

Roles file structure:

Files/: All files used in this role are stored in this directory;

Templates/: Jinja2 template file storage location

Tasks/: the task list file. There can be multiple files, but at least one file is called main. yml.

Handlers/: processor list file; there can be multiple, but at least one file called main. yml

Vars/: Variable dictionary file. There can be multiple files, but at least one file is called main. yml.

Meta/: Special settings and dependencies of this role

 

Example: mkdir/etc/ansible/roles cd/etc/ansible/roles mkdir web1/{files, templates, tasks, handlers, vars, meta}

 

(1) vim web1/vars/main. yml

  

 

 

 

(2) vim web1/tasks/main. yml

 

Here, the template refers to the relative path --> web1/templates

 

(3) vim web1/handlers/main. yml

 

 

Cp/root/httpd. conf/etc/ansible/roles/web1/templates/

 

Vim/etc/ansible/roles/web1/templates/httpd. conf

 

Replace User and Group with the variables defined above.

 

Define a roles call file outside roles. The file name of vim/etc/ansible/web1.yml must end with. yml.

Hosts: web1 refers to the group defined in/etc/ansible/hosts.

The last-web1 indicates the/etc/ansible/roles/web1 directory.

In this case, you only need to change the value specified by hosts to reuse code for different hosts.

 

You can also pass variables through role, such

 

 

You can also call multiple roles, such

 

Every time you run palybook, you will run all the content in it, and sometimes you only want to run some content, then you can use tags

Vim/etc/ansible/roles/web1/tasks/main. yml

 

Run the command ansible-playbook-t conf web1.yml to only execute the task that defines the tags tag of the conf file. In this example, install config file

Tags can be defined in multiple places

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.