Ansible automated operation and maintenance --Ansible-playbook Hi, playbook written in simple installation and configuration httpd

一、Ansible-playbook

Ansible command mode is different from a line execution mode, the more powerful and flexible. simply put,playbooks is a very simple configuration management and deployment of multi-host system. It is worth noting,playbook be described is defined by the format YAML.
Step Playbooks be used to declare configuration, playbooks can arrange orderly execution, even in multiple groups do machines ordered specially designated fro. And may be synchronous or asynchronous tasks initiated.

  • Action: a plurality of functional modules to integrate operations; duplication achieve simplification (to improve efficiency); special needs implemented
  • When using adhoc, mainly using the / usr / bin / ansible to perform your tasks. And when using playbooks, more is to put the source code is under control, with the push to confirm your configuration or your remote system configuration complies with configuration specifications.
  • Ansible Chinese Definitive Guide

1.1 core elements

  • List of remote hosts Hosts executed
  • Tasks task set
  • Varniables built-in variable or a custom variable call playbook in
  • Templates template, use the template file that is grammar, such as configuration files, etc.
  • Handlers and notity used in conjunction with actions triggered by specific conditions to meet the conditions just perform, or not perform

1.2 YAML written format

  • The first line of the file should be "-" three hyphens start indicates the start of YAML files. It may be continuously connected three sub-sign (-) to distinguish a plurality of play.
  • In the same line, after the contents # denotes a comment
  • YMAL list elements in order to "-" at the beginning and then followed by a space, the same elements in the list should remain the same indentation.
  • Note indent relationship: two spaces to indent a relationship.Indent level must also be consistent with the same indentation represents the same level, the program determines whether any level is indented by binding wrap achieve.
  • Create a key-value pair: use the colon (such as age: 18) must be a space after the colon; if no spaces are wrong; (such as age: 18 (error))

1.3 Common Commands

Run ymal file : ansible-playbook filename.yml

Check grammar structure : ansible-playbook --syntax-check filename.yml

Simulation execution script : ansible-playbook -C filename.yml

[root@ansible PlayBook]# ansible-playbook -h
#ansible-playbook常用选项:
--check  or -C    #只检测可能会发生的改变,但不真正执行操作
--list-hosts      #列出运行任务的主机
--list-tags       #列出playbook文件中定义所有的tags
--list-tasks      #列出playbook文件中定义的所以任务集
--limit           #主机列表 只针对主机列表中的某个主机或者某个组执行
-f                #指定并发数,默认为5个
-t                #指定tags运行,运行某一个或者多个tags。(前提playbook中有定义tags)
-v                #显示过程  -vv  -vvv更详细

Second, the simple preparation of Ansible-playbook

In order to facilitate the preparation, we can change the current directory is indented:Two spaces to indent a
Write .vimrc file

autocmd FileType yaml setlocal ai ts=2 sw=2 et

Here Insert Picture Description
Here Insert Picture Description

2.1 Installation and Configuration httpd

1、Create a playbook file: vim install.yml

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

  • Note: Calls to copy the module to configure the default access when apache pages, files, and YAML file copy module to be called is in the same directory.

Here Insert Picture Description
2、执行playbook : ansible-playbook apache/install.yml

Here Insert Picture Description
Here Insert Picture Description

3, may also be added a variable http_port so that more user-friendly document YMAL

---
- hosts: test
  vars:
    - http_port: 80
  tasks:
    - name: install apache
      yum:
        name: httpd
        state: present

    - name: config apache
      template:
        src: httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
      notify: restart apache

    - name: start apache
      service:
        name: httpd
        state: started
        enabled: yes

    - name: create index.html
      copy:
        src: index.html
        dest: /var/www/html/index.html

    - name: start firewalld
      service:
        name: firewalld
        state: started
        enabled: yes

    - name: config firewalld
      firewalld:
        service: http
        state: enabled
        permanent: yes
        immediate: yes

  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

- hosts: localhost
  become: false
  tasks:
    - name: test apache
      uri:
        url: 'http://172.25.37.2:{{ http_port }}'
        return_content: yes

Here Insert Picture Description

Here Insert Picture Description

Three, playbook basis Detailed

3.1 Tasks list

  • Each play contains a task list. After a task is finished on all of its corresponding host, the next task will be performed.
  • If a host execution task fails, the host will be when running playbook (from top to bottom execution) from the entire playbook
    to remove the entire execution process. If this fails occurs, correct playbook errors and repeat It can be.
  • Each performing a task that the target moudle, usually performed with a specific parameter may be used variables (Variables) in the parametermodules idempotent, that is, the remote system is experiencing unexpected change, the need to restore the status quo ante, when executed moudle Again, moudle will perform the necessary changes, changes only where it is needed to changeSo repeated several times to perform playbook is also very safe.

3.2 Handlers: operation performed at the time of change

When a change occurs, notify actions will be triggered at the end of each task playbook, and even if there are a number of different task notice changes occur, notify it will only be triggered once.

  • Handlers best scenario is used to restart the service, or trigger a system reboot.
  • handlers will execute the order of declaration

Priority 3.3 variables

If the same variable names are defined in more than one place, then there is the adoption of a certain order, as follows:

  • extra vars (using the -e command line) with the highest priority
  • Is then connected to a variable defined in the inventory (such ansible_ssh_user)
  • Then most of the other variables (command line conversion, play variables, included variables, role variables, etc.)
  • And the other variables are as defined in the inventory
  • Then the facts found by the system
  • Then the "role default variable", this is the default value, it is easy to lose priority
Published 102 original articles · won praise 21 · views 5324

Guess you like

Origin blog.csdn.net/ranrancc_/article/details/103248389