Ansible's Playbook commonly used detailed explanation-extremely detailed

Ansible's Playbook commonly used detailed explanation-extremely detailed

YAML markup language

YAML is a format for expressing data sequences. Because it refers to many other languages, it has high readability. Its characteristics are as follows:

  • It is very readable and easy to implement
  • Strong expressive ability and good scalability
  • Good interaction with scripting language
  • Have a consistent information model
  • Can be processed based on stream

Basic grammar rules

1. Case sensitive
2. Use indentation to indicate hierarchical relationships
3. Tab key is not allowed when indenting, only spaces are allowed.
4. The number of indented spaces is not important, as long as the elements of the same level are aligned to the left

Data structure supported by YAML

1. Object: a collection of key-value pairs, also known as mapping / hashes / dictionary (dictionary) For example: name: Example Developer key value
2. Array: a set of values ​​arranged in order, also known as For sequence (sequence) / list (list) For example:
-Apple -Orange
3. Scalar : a single, indivisible value For example: number: 11.20 sure: true

Playbook overview

A playbook is a list composed of one or more plays. The main function is to define the roles of tasks and manage them as a group, that is, to organize multiple palys in a playbook by calling the Ansible module of the task. The playbook itself consists of the following parts:

  • Tasks: tasks, that is, an operation completed by calling the module
  • Varibles: Variable
  • Templates: templates
  • Handlers: Handlers, when a certain condition is met, the operation triggered
  • Roles: roles

Introduction to Hosts and Users

The design purpose of the playbook is to allow a certain host or certain hosts to perform corresponding tasks with a certain identity. Among them, it is used to specify the host definition of the host to perform the task. It can be a host or multiple host groups separated by colons; it is used to specify the user performing the task on the managed host to be defined by remote_user, for example:

- hosts: webserver
  remote_user: root

The remote_user can also define the specified user to run commands on the managed host through the sudo method, and even specify the user switched by sudo using become.

Instance

- hosts: mysql
  remote_user: root
  become: yes
  become_user: lisi
  tasks:
   - name: copy text
     copy: src=/etc/fstab dest=/home/lisi/fstab.back  

task list and action

1. The main part of Play is the task list. The tasks in the task list are executed one by one on the host specified in the hosts, that is, the second task is started after the first task is completed on all hosts.
When running the playbook (executed from top to bottom), if a host fails to execute the task, the entire tasks will be rolled back. Please correct the error in the playbook and execute it again.
The purpose of Task is to execute the module with specified parameters, and variables can be used in module parameters.

2. Each task must have a name, so that when the playbook is running, it can be easily distinguished from the task execution information it belongs to. If the name is not defined, the value of'action' will be used to mark the specific task in the output message.

3. Define a task, the common format: "module: options" For example: yum: name=httpd

Simple example of playbook:

[root@master demo]# vim lll.yaml 
- hosts: mysql
  remote_user: root
  tasks:
   - name: ceshi
     ping:
     remote_user: root
[root@master demo]# ansible-playbook lll.yaml --syntax-check #测试语法是否正确

playbook: lll.yaml

[root@master demo]# ansible-playbook lll.yaml  #运行剧本

PLAY [mysql] ******************************************************************************************

TASK [Gathering Facts] ********************************************************************************
ok: [20.0.0.15]

TASK [ceshi] ******************************************************************************************
ok: [20.0.0.15]

PLAY RECAP ********************************************************************************************
20.0.0.15                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

Insert picture description here

Switch user and copy files to user directory

[root@master demo]# vim lll.yaml 
- hosts: mysql
  remote_user: root
  become: yes
  become_user: zhangsan
  tasks:
   - name: copy
     copy: src=/etc/fstab dest=/home/zhangsan/fstab.bak
         
[root@master demo]# ansible-playbook lll.yaml  #执行剧本

Insert picture description here

View on mysql node
Insert picture description here

Write and install httpd, open httpd service, turn off the firewall, and write a httpd web page

[root@master demo]# vim ggg.yml 
- hosts: mysql
  remote_user: root
  vars:
  - user:
  tasks:
   - name: stop firewalld
     service: name=firewalld state=stopped
   - name: install httpd
     yum: name=httpd
   - name: start httpd
     service: name=httpd state=started
   - name: touch index
     copy: content="this is ggg" dest=/var/www/html/index.html
   - name: copy
     copy: content="{
    
    {ansible_all_ipv4_addresses}},{
    
    {nu}}" dest=/opt/g.txt

Insert picture description here
[root@master demo]# ansible-playbook ggg.yml #Run the playbook
Insert picture description here
View on the mysql node


[root@node2 ~]# cd /var/www/html/
[root@node2 html]# cat index.html 
this is ggg

Insert picture description here
Insert picture description here

variable

[root@master demo]# vim b.yaml
- hosts: mysql
  remote_user: root
  vars:
   - username: lisi
  tasks:
   - name: create user
     user: name={
    
    {
    
    username}}
	 
[root@master demo]# ansible-playbook b.yaml --syntax-check #检查语法

playbook: b.yaml

[root@master demo]# ansible-playbook b.yaml#运行剧本

在mysql节点上查看
[root@node2 ~]# cd /home/
[root@node2 home]# ll
总用量 0
drwx------. 5 ggg      ggg      128 1219 08:59 ggg
drwx------. 5 jjj      caiwu    128 1219 08:59 jjj
drwx------. 3 lisi     lisi      78 1220 02:24 lisi
drwx------. 6 zhangsan zhangsan 161 1219 21:31 zhangsan

Where the variable is empty, you can add variables when running the script

[root@master demo]# vim b.yaml  #其中变量为空  可以在运行剧本的时候添加变量

- hosts: mysql
  remote_user: root
  vars:
   - username:
  tasks:
   - name: create user
     user: name={
    
    {
    
    username}}
~                               
[root@master demo]# ansible-playbook b.yaml -e username="wangwu"
 在mysql节点上查看
 [root@node2 home]# ll
总用量 0
drwx------. 5 ggg      ggg      128 1219 08:59 ggg
drwx------. 5 jjj      caiwu    128 1219 08:59 jjj
drwx------. 3 lisi     lisi      78 1220 02:24 lisi
drwx------. 3 wangwu   wangwu    78 1220 03:35 wangwu
drwx------. 6 zhangsan zhangsan 161 1219 21:31 zhangsan

Built-in variables

[root@master demo]# vim ggg.yml #nsible_all_ipv4_addresses内置的变量  查看IP地址

   - hosts: mysql
  remote_user: root
  vars:
  - user:
  tasks:
   - name: stop firewalld
     service: name=firewalld state=stopped
   - name: install httpd
     yum: name=httpd
   - name: start httpd
     service: name=httpd state=started
   - name: touch index
     copy: content="this is ggg" dest=/var/www/html/index.html
   - name: copy
     copy: content="{
    
    {ansible_all_ipv4_addresses}}}}" dest=/opt/t.txt
 [root@master demo]# ansible-playbook ggg.yml #执行
 在mysql节点上查看
[root@node2 home]# cd /opt/
[root@node2 opt]# ll
总用量 8
-rw-r--r--. 1 root root 45 1219 22:16 g.txt
-rw-r--r--. 1 root root 34 1220 03:41 t.txt
[root@node2 opt]# cat t.txt 
[u'192.168.122.1', u'20.0.0.15']}}[root@node2 opt]# 

Add variables in the main configuration file

[root@master demo]# vi /etc/ansible/hosts 
[webservers]
20.0.0.14
[mysql]
20.0.0.15 nu=124335246

Insert picture description here

[root@master demo]# vim ggg.yml
- hosts: mysql
  remote_user: root
  vars:
  - user:
  tasks:
   - name: stop firewalld
     service: name=firewalld state=stopped
   - name: install httpd
     yum: name=httpd
   - name: start httpd
     service: name=httpd state=started
   - name: touch index
     copy: content="this is ggg" dest=/var/www/html/index.html
   - name: copy
     copy: content="{
    
    {ansible_all_ipv4_addresses}},{
    
    {nu}}}}" dest=/opt/t.txt

[root@master demo]# ansible-playbook ggg.yml 
在mysql节点上查看
[root@node2 ~]# cd /opt/
[root@node2 opt]# cat t.txt 
[u'192.168.122.1', u'20.0.0.15'],124335246}}[root@node2 opt]# 

Condition test

[root@master demo]# vim q.yaml  #关闭mysql节点

- hosts: mysql
  remote_user: root
  tasks:
   - name: "shutdown CentOS"
     command: /sbin/shutdown -h now
     when: ansible_distribution == "CentOS"
	 
[root@master demo]# ansible-playbook q.yaml 

Insert picture description here
The reason for the error is that the use of mysql has been closed

Custom variables for conditional testing

Iteration

[root@master demo]# vim e.yaml

- hosts: mysql
  remote_user: root
  tasks:
   - name: intasll hPackages
     yum: name={
    
    {
    
     item }} state=latest
     with_items:
      - httpd
      - php
[root@master demo]# ansible-playbook e.yaml #运行剧本
在mysql节点上查看              
[root@node2 ~]# rpm -q php
php-5.4.16-42.el7.x86_64
[root@node2 ~]# rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64

Write a command line under the task in the script

iignore_errors: True #Ignore errors and force a successful return

Guess you like

Origin blog.csdn.net/weixin_50346902/article/details/112970304