Playbook script of automated operation and maintenance tool Ansible

a playbook

1. Brief description of playbook
A playbook is a script used by ansible to configure, deploy, and manage controlled nodes. Through the detailed description of the playbook and executing the tasks in it, the remote host can reach the expected state. A playbook is a list of one or more "plays". When initializing the environment of a machine, it is often necessary to do more than one thing. At this time, it is more suitable to use a playbook. With playbook you can execute multiple instructions on multiple machines at once. This pre-engineered configuration keeps the configuration of the machine consistent and makes it easy to perform day-to-day tasks.

Ansible implements corresponding management through different modules. The management method includes the ports managed by the defined list file (hosts) including the ports connected by authentication. All functions are implemented by calling different modules (modules). Whether it is executing a single command or a play-book is based on the manifest file.

2. Playbook script format
The playbook is written in YMAL language. The YMAL format is a file format similar to JSON. There are multiple plays in a file, only one task in a play, and multiple name tasks in a task.

Precautions:

①Case clearly

②Represent hierarchical relationship through indentation

③ does not support tabs for indentation, only spaces can be used for indentation

④The number of indented spaces is not important, as long as the same level is aligned left and right, usually 2 spaces are indented at the beginning

⑤# Note

⑥Symbol characters are indented with 1 space, such as colon: comma, horizontal bar - followed by a space

⑦If it contains special characters, it will be treated as a string if it is surrounded by single quotes and double quotes. Single quotes do not recognize variables, and double quotes recognize variables.

Two playbook startup and detection

ansible-playbook file name.yaml
ansible-playbook file.yaml --start-at-task='task name/label' #Start execution from a task or only execute the name of a certain label #Start
this file
Supplementary parameters:- k(-ask-pass): used to interactively enter the ssh password

​ -K(-ask-become-pass): used to enter the sudo password interactively

​ -U: Specify the user
ansible-playbook file.yaml --list-task #Check the yml file

ansible-playbook file.yaml --list-hosts #Detect hosts

ansible-playbook file.yaml --syntax-check #Check syntax

Three ansible-playbook modules in practice

3.1 Practical example of playbook module 1

vim test1.yaml
#新建编辑yaml文件,内容如下
---
#ymal文件开头,可不写
- name: test
#定义play名称,可不写
  gather_facts: false
#设置不进行facts信息收集,这可以加快执行速度,可省略默认开启
  hosts: webservers
#要执行的组或者主机,webservers为组名需要在/etc/ansible/hosts中配置
  remote_user: root
#执行时所使用的用户
  tasks:
#定义任务列表,列表中任务按顺序执行
   - name: test ping
#自定义name的任务名称
     ping:
#第一个任务执行内容为使用ping模块ping,webservers组的主机
   - name: test selinux
     command: /sbin/setenforce 0
#第二个任务执行内容为使用command模块关闭selinux
     ignore_errors: True
#若出现错误,忽略继续运行下面的任务,默认为true出错立即停止playbook
   - name: disable firewalld
     service: name=firewalld state=stopped
#第三个任务执行内容为使用service模块关闭firewalld防火墙
   - name: test yum
     yum: name=httpd state=latest
#第四个任务执行内容为使用yum模块安装httpd服务状态为latest
   - name: test copy
     copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
#第五个任务为使用copy模块将ansible上的/etc/httpd.conf文件拷贝到指定组的主配置文件下替换,注意此处需要在ansible的/opt目录上有一个httpd.conf的模版否则会报错
     notify: "restart httpd"
#如果上面的copy任务成功,则调用handlers模块的restart httpd,注意要与下面handlers的name名称相同
   - name: test start httpd
     service: enabled=yes name=httpd state=started
#第六个任务执行内容为使用service模块启动httpd服务并设置开机自启
  handlers:
   - name: restart httpd
     service: name=httpd state=restarted
#handlers模块,若notify成立则执行此模块内容调用service模块重启httpd服务。注意使用name名称调用

Please add a picture description
Please add a picture description

3.2 Practical example of vars module 2

---
- name:
  hosts: webservers
  remote_user: root
  ignore_errors: false
  vars:
#使用变量模块
   - groupname: testgroup
#定义变量groupname的值为testgroup
   - username: testuser
#定义变量username的值为testuser
  tasks:
   - name: create group
     group: name={
    
    {
    
    groupname}} gid=111
#第一个任务使用group模块调用定义的groupname变量创建组
   - name: create user
     user: name={
    
    {
    
    username}}  uid=10086 group={
    
    {
    
    groupname}}
#第二个任务使用user模块调用定义的username变量创建用户指定uid和组

Please add a picture description

Please add a picture description

3.3 Practical example of when module 3

---
 - hosts: webservers
   remote_user: root
   tasks:
    - name: test when
      service: name=httpd state=stopped
      when: ansible_default_ipv4.address == "192.168.10.20"
#当内置的变量ipv4.address等于192.168.10.20时调用service模块关闭httpd服务

Please add a picture description
Please add a picture description
Please add a picture description

3.4 Practical example of with_items loop module 4

---
 - name: test1
   hosts: webservers
   gather_facts: false
   tasks:
    - name: create directories
      file:
        path: "{
    
    {item}}"
        state: directory
#路径调用变量item,变量值为下面的with_items的内容,一次执行一个有几个值执行几次,state为创建类型为目录
      with_items:
        - /tmp/test1
        - /tmp/test2
    - name: create file
      file:
        path: "{
    
    {item}}"
        state: touch
#路径调用变量item,变量值为下面的with_items的内容,一次执行一个有几个值执行几次,state为创建类型为文件
      with_items:
        - /tmp/test1/a.txt
        - /tmp/test2/b.txt

Please add a picture description
Please add a picture description
Please add a picture description

3.5 Practical examples of the tags module 5

---
- hosts: webservers![请添加图片描述](https://img-blog.csdnimg.cn/2e546c3c2be5458c9dff757d5e200d69.png)

  remote_user: root
  gather_facts: false
  tasks:
    - name: copy hosts
      copy: src=/etc/hosts dest=/opt/hosts
      tags:
      - tags1
#自定义标签名,可以在执行yaml文件时使用 --tag="tags1"只执行此任务,always标签任务除外
    - name: touch file
      file: path=/opt/testhost state=touch
      tags:
      - always
#always标签,无论执行那个标签都会将此标签的任务内容执行

Please add a picture description
Please add a picture description

おすすめ

転載: blog.csdn.net/m0_75015568/article/details/130466415