ansible-playbook script

Introduction Playbook

ansible playbook is used to configure, deploy, and manage scripts accused of nodes. Detailed description of the playbook, which perform tasks, allowing remote host to the expected state. playbook is a list of one or more "play" component. When a machine to do the initialization of the environment often need to do more than one thing, then use the playbook will be more suitable. You can execute multiple instructions at once multiple machines through the playbook. With this pre-designed configuration maintains the uniform configuration of the machine, and perform simple daily tasks.

Ansible way to achieve the appropriate management, the management of the different modules by the host defined by the manifest file (the hosts) managed by authentication mode comprises ports connected. All functions are done by calling the different functions of different modules (modules). Whether to execute a single command or a play-book is based on the manifest file.

playbook format

playbook written by YMAL language. YMAL format is similar to JSON file format for people to understand and read, and easy to write.

A script which can have multiple play, every play only one tasks, each of the tasks can have multiple name

核心元素:
Playbooks  
Variables     #变量元素,可传递给Tasks/Templates使用;  
Tasks          #任务元素,由模块定义的操作的列表,即调用模块完成任务;  
Templates   #模板元素,使用了模板语法的文本文件;  
Handlers     #处理器元素,通常指在某事件满足时触发的操作;  
Roles          #角色元素

playbook of basic components:
name:
define the playbook or task name (descriptive information), each play can complete a task.
hosts:
hosts used to specify the host to perform a given task.
the User:
REMOTE_USER is for the user to perform a task on the specified remote host
Tasks:
. the main part of the task list task list is to play each task task list one by one in an in-order execution on all hosts hosts specified, to complete the first task on all hosts before starting the second.
vars:
define the variable (if you do not use internal variables need to be defined in advance)
vars_files:
call to define the variable document
notify:
task execution results if it changes the trigger is defined in the task execution handler's
handlers:
changes in resources for the current concern occurs when take certain specified action

实例一:
[root@ansible-server ~]# cd /etc/ansible/
[root@ansible-server ansible]# vim test.yml  #创建文件必须以.yml或者.pyayyaml结尾
---
 - hosts: webservers1
   user: root
   tasks:
   - name: playbook_test(模块的名字 可以自定义)
     file: state=touch path=/tmp/playbook.txt
===========================================
参数解释:
    hosts: 参数指定了对哪些主机进行操作;
    user: 参数指定了使用什么用户登录远程主机操作;
    tasks: 指定了一个任务.
    name:参数同样是对任务的描述,在执行过程中会打印出来。
检测语法:
[root@ansible-server ansible]# ansible-playbook --syntax-check test.yml 
playbook: test.yml
运行Playbook:
[root@ansible-server ansible]# ansible-playbook test.yml #加剧本名称
实例二
handlers:由特定条件触发的Tasks
handlers:处理器
notify:触发器
语法:
tasks:
- name: TASK_NAME
  module: arguments               #1.上面任务执行成功,然后
  notify: HANDLER_NAME        #2.通知他
handlers:
- name: HANDLER_NAME        #3.一一对应,这里的描述与notify定义的必须一样
  module: arguments         #4.执行这个命令
=======================================================
[root@ansible-server ansible]# vim handlers.yml
- hosts: webservers1
  user: root
  tasks:
  - name: test copy
    copy: src=/root/a.txt dest=/mnt
    notify: test handlers
  handlers:
  - name: test handlers
    shell: echo "abcd" >> /mnt/a.txt
========================================================
说明:只有 copy 模块真正执行后,才会去调用下面的 handlers 相关的操作,追加内容。所以这种比较适合配置文件发生更改后,需要重启服务的操作。
案例三
循环:迭代,需要重复执行的任务;
对迭代项的引用,固定变量名为”item”,使用with_item属性给定要迭代的元素; 

基于字符串列表元素实战:
[root@ansible-server ansible]# vim list.yml
- hosts: webservers2
  remote_user: root
  tasks:
  - name: install packages
    yum: name={{ item }} state=latest         #相当于for循环里面的i 
    with_items:                               #取值 。但是不支持通配符
     - httpd
     - php
     - php-mysql
     - php-mbstring
     - php-gd 
案例四、自定义vars_files变量
变量调用语法: 
{{ var_name }}
====================================================
创建变量目录:
[root@ansible-server ~]# mkdir /etc/ansible/vars
[root@ansible-server ~]# cd /etc/ansible/vars/
[root@ansible-server vars]# vim file.yml     #创建变量文件。
src_path: /root/test/a.txt
dest_path: /opt/test/
案例四、自定义vars_files变量
变量调用语法: 
{{ var_name }}
====================================================
创建一个测试文件
[root@ansible-server vars]# mkdir /root/test
[root@ansible-server vars]# vim /root/test/a.txt  #编辑测试文件
123
创建play-book引用变量文件:
[root@ansible-server vars]# cd /etc/ansible/
[root@ansible-server ansible]# vim vars.yml
- hosts: ansible-web1
  user: root
  vars_files:
   - /etc/ansible/vars/file.yml
  tasks:
   - name: create directory
     file: path={{ dest_path }} mode=755 state=directory
   - name: copy file
     copy: src={{ src_path }} dest={{ dest_path }}

```powershell
语法检测:
[root@ansible-server ansible]# ansible-playbook --syntax-check apache.yml 
playbook: apache.yml
执行play-book
[root@ansible-server ansible]# ansible-playbook apache.yml
Published 48 original articles · won praise 18 · views 3639

Guess you like

Origin blog.csdn.net/wx912820/article/details/105005419