Automated operation and maintenance tools-Ansible introduction (2) playbook

3. Implement playbook #########

The goal of this chapter: write a basic ansible playbook

3.1 Write and run playbook

3.1.1 View temporary commands of a specified user

'The yaml format usually has the extension yml, and yaml has no strict requirements for indentation, but there are two basic principles'
' 1. Data elements at the same level in the same hierarchy must have the same indentation '
' 2. If the item belongs to a child of another item, the indentation must be greater than the parent item '

 ansible -m user -a "name=student uid=1000state=present" servera.lab.example.com

Insert picture description here
Rewritten as playbook

[root@workstation ~]# cat user.yml

Insert picture description here

---                                     开头三个破折号,文档的开始标记
- name: Configure User            可写可不写,但是建议使用

...                      结尾三个省略号,结束标记(通常省略)

Play itself is a set of key and value pairs. The keys in the same play should use the same indentation.
hosts is a key, and the following content is the value. This is a nested collection.

playbook execution:
Insert picture description here

3.1.2 Install apache

Create a new directory for easy operation

mkdir /demo
vim webserver.yml

Insert picture description hereInsert picture description here
Insert picture description here
It can be seen that the httpd service has been installed.

3.1.3 Make sure the httpd service is started

vim service.yml

Insert picture description here
Insert picture description here
effect:
Insert picture description here

3.1.4 Improve output detail level

The default output of ansible-playbook does not provide detailed task execution information. The -v parameter provides four levels:

  1. -v #Display task results
  2. -vv #Display task results and task configuration
  3. -vvv #contains information about connections to managed hosts
  4. -vvvv #Add additional verbosity options related to connecting plugins (including users who execute scripts and scripts executed on managed hosts)

# Best to perform syntax verification before executing playbook
Insert picture description here
# No syntax error
Change the playbook:
Insert picture description here

Insert picture description here
If there is a grammatical error, it will prompt the wrong location.

3.1.5 Perform dry run, just preview the result

We first remove the httpd service on the 71 host:
Insert picture description here
then execute the playbook;
ansible-playbook -C webserver.yml
Insert picture description here
Insert picture description here
is still not installed. As can be seen.
'Dry run will report what will happen when this playbook is executed, but will not change the target host'

3.1.6 Install, configure the default release page and start apache

 vim site.yml

Insert picture description here

Detection grammar

ansible-playbook --syntax-check site.yml

Insert picture description here
no problem.
# Create the configuration file and list
Insert picture description here
# Create the default publishing page
cat files / index.html to
Insert picture description here
execute the playbook:
Insert picture description here
Insert picture description here
service installation and enable.
Insert picture description here
Insert picture description here
The publishing page was changed successfully.

# Test
curl obtain remote browser content.
Insert picture description here

3.2 Implement multiple play

Template:
Insert picture description here
ansible-doc -l
Insert picture description here
#List all modules ansible-doc yum #List usage and examples of
Insert picture description here
yum ansible-doc -s yum #Use terminal to output usage of each parameter in yum module
Insert picture description here

3.3 PLAYBOOK yaml syntax changes

  1. Comments by yaml
    Insert picture description here
  2. yaml string
    directly input
    this is a string
    single quotes
    'this is a string'
    multiple quotes
    "this is a string"

# Exercise
Change the remote host page without closing the firewall and visit:

Insert picture description here

 vim webserver.yml
---
- name: Enable internet service
  hosts: web
  become: yes
  tasks:
          - name: apache and firewalld installed   # 检测firewalld和httpd是否为最新版  
            yum:
                    name:
                            - httpd
                            - firewalld
                    state: latest          

          - name: Configure index.html    # 配置默认发布页面
            copy:
                    content: "welcome to westos!\n"
                    dest: /var/www/html/index.html
                    
          - name: firewalld enabled and runing    # 检测firewalld是否开机自启并启用
            service:
                    name: firewalld
                    enabled: true
                    state: started
                    
          - name: firewald primits http           # 检测firewalld是否添加http服务
            firewalld:
                    service: http
                    permanent: true
                    state: enabled
                    immediate: yes
                    
          - name: httpd enabled and runing            # 检测httpd服务是否自启并运行
            service:
                    name: httpd
                    enabled: true
                    state: started
                    
- name: test webserver					本地主机测试访问
  hosts: localhost
  become: no
  tasks:
          - name: connect web server
            uri:
                    url: http://rhel71.com
                    return_content: yes
                    status_code: 200

# Detection grammar
Insert picture description here
# run

ansible-playbook -v webserver.yml

Insert picture description here
You can see the content welcome to westos
and status code 200 are returned

3.4 Management variables and facts

Use some variables in playbook instead of variables to simplify playbook writing

3.4.1 Management variables

Introduction to ansible variables

Variables may contain the following values:
users to be created,
software packages to be installed,
services to be restarted,
files to be deleted, documents to
be retrieved from the Internet

Named variable

Variable names must start with a letter, and can only contain letters, numbers, and underscores.
Incorrect
web server web_server
westos.file remote_file
1file file1

Define variables

Three scope levels
Global scope: Variables set from the command line or ansible configuration
Play scope: Variables set in play and related structures
Host scope: Tasks collected or registered by inventory, facts, set on host groups and individual hosts Variables
' If variables with the same name are defined on multiple levels, the variable with the highest level is preferred, and narrow scope takes precedence over wide scope

3.4.2 Variables in playbook

Define variables in playbook

1. Common way: In the vars block at the beginning of the playbook:
Insert picture description here
2. Define the playbook variable in the external file
Insert picture description here

cat user.yml

Insert picture description here

Use variables in playbook

Put the variable name in curly brackets.
Insert picture description here
'Note: When the variable is used as the first element to start a value, you must use quotation marks.'

Published 50 original articles · Liked 18 · Visits 3780

Guess you like

Origin blog.csdn.net/thermal_life/article/details/105308669