Ansible_Roles

Ansible Roles

1 Roles Introduction

ansible from new features introduced in version 1.2, for hierarchical structured organized playbook. roles can be loaded automatically variable according to the hierarchical file structure, tasks and other handlers. To use roles only need to use the include directive can be introduced in the playbook. Simply put, roles is through variable respectively, files, tasks, templates, and the processor is placed in a separate directory, and can be conveniently include a mechanism for them. Roles are generally used for host-based building services scenario, but can also be used to build daemons scene. The main use case scenario the higher costs of code reuse.

2 Roles directory structure

Here Insert Picture Description
Explain the meaning of each directory

roles:          <--所有的角色必须放在roles目录下,这个目录可以自定义位置,默认的位置在/etc/ansible/roles
  project:      <---具体的角色项目名称,比如nginx、tomcat、php
    files:     <--用来存放由copy模块或script模块调用的文件。
    templates: <--用来存放jinjia2模板,template模块会自动在此目录中寻找jinjia2模板文件。
    tasks:     <--此目录应当包含一个main.yml文件,用于定义此角色的任务列表,此文件可以使用include包含其它的位于此目录的task文件。
      main.yml
    handlers:  <--此目录应当包含一个main.yml文件,用于定义此角色中触发条件时执行的动作。
      main.yml
    vars:      <--此目录应当包含一个main.yml文件,用于定义此角色用到的变量。
      main.yml
    defaults:  <--此目录应当包含一个main.yml文件,用于为当前角色设定默认变量。
      main.yml
    meta:      <--此目录应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系。
      main.yml

Example 3 Roles

Installation Configuration httpd service by ansible roles, roles used herein default path / etc / ansible / roles

1) Create a directory

[root@ansible ~]# cd /etc/ansible/roles/
# 创建需要用到的目录
[root@ansible roles]# mkdir -p httpd/{handlers,tasks,templates,vars}
[root@ansible roles]# cd httpd/
[root@ansible httpd]# tree .
.
├── handlers
├── tasks
├── templates
└── vars
directories, 0 file

2) variable document preparation vars / main.yml

[root@ansible httpd]# vim vars/main.yml
PORT: 8088        #指定httpd监听的端口
USERNAME: www     #指定httpd运行用户
GROUPNAME: www    #指定httpd运行组

3) profile template ready templates / httpd.conf.j2

# copy一个本地的配置文件放在templates/下并已j2为后缀
[root@ansible httpd]# cp /etc/httpd/conf/httpd.conf templates/httpd.conf.j2

# 进行一些修改,调用上面定义的变量
[root@ansible httpd]# vim templates/httpd.conf.j2
Listen {{ PORT }} 
User {{ USERNAME }}
Group {{ GROUPNAME }}

4) writing the script tasks, create users, create groups, install software, configure, start, etc.

# 创建组的task
[root@ansible httpd]# vim tasks/group.yml
- name: Create a Startup Group
  group: name=www gid=60 system=yes

# 创建用户的task
[root@ansible httpd]# vim tasks/user.yml
- name: Create Startup Users
  user: name=www uid=60 system=yes shell=/sbin/nologin

# 安装软件的task
[root@ansible httpd]# vim tasks/install.yml
- name: Install Package Httpd
  yum: name=httpd state=installed

# 配置软件的task
[root@ansible httpd]# vim tasks/config.yml
- name: Copy Httpd Template File
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: Restart Httpd

# 启动软件的task
[root@ansible httpd]# vim tasks/start.yml
- name: Start Httpd Service
  service: name=httpd state=started enabled=yes

# 编写main.yml,将上面的这些task引入进来
[root@ansible httpd]# vim tasks/main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: config.yml
- include: start.ym

5 write restart the httpd handlers), handlers / main.yml

[root@ansible httpd]# vim handlers/main.yml
# 这里的名字需要和task中的notify保持一致
- name: Restart Httpd
  service: name=httpd state=restarte

6) the preparation of the master file called httpd role of httpd_roles.yml

[root@ansible httpd]# cd ..
[root@ansible roles]# vim httpd_roles.yml
---
- hosts: all
  remote_user: root
  roles:
    - role: httpd        #指定角色名称

7) the overall view of a directory structure

[root@ansible roles]# tree .
.
├── httpd
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   ├── config.yml
│   │   ├── group.yml
│   │   ├── install.yml
│   │   ├── main.yml
│   │   ├── start.yml
│   │   └── user.yml
│   ├── templates
│   │   └── httpd.conf.j2
│   └── vars
│       └── main.yml
└── httpd_roles.yml
directories, 10 files

8) Test playbook correct syntax

[root@ansible roles]# ansible-playbook -C httpd_roles.yml 

PLAY [all] **************************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [192.168.1.33]
ok: [192.168.1.32]
ok: [192.168.1.31]
ok: [192.168.1.36]

TASK [httpd : Create a Startup Group] ***********************************************************************
changed: [192.168.1.31]
changed: [192.168.1.33]
changed: [192.168.1.36]
changed: [192.168.1.32]

TASK [httpd : Create Startup Users] *************************************************************************
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.31]
changed: [192.168.1.36]

TASK [httpd : Install Package Httpd] ************************************************************************
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.31]
changed: [192.168.1.36]

TASK [httpd : Copy Httpd Template File] *********************************************************************
changed: [192.168.1.33]
changed: [192.168.1.36]
changed: [192.168.1.32]
changed: [192.168.1.31]

TASK [httpd : Start Httpd Service] **************************************************************************
changed: [192.168.1.36]
changed: [192.168.1.31]
changed: [192.168.1.32]
changed: [192.168.1.33]

RUNNING HANDLER [httpd : Restart Httpd] *********************************************************************
changed: [192.168.1.36]
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.31]

PLAY RECAP **************************************************************************************************
192.168.1.31               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.32               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.33               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.36               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

9) The above test is no problem, the formal implementation playbook

[root@ansible roles]# ansible-playbook -C httpd_roles.yml 

PLAY [all] **************************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [192.168.1.33]
ok: [192.168.1.32]
ok: [192.168.1.31]
ok: [192.168.1.36]

TASK [httpd : Create a Startup Group] ***********************************************************************
changed: [192.168.1.31]
changed: [192.168.1.33]
changed: [192.168.1.36]
changed: [192.168.1.32]

TASK [httpd : Create Startup Users] *************************************************************************
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.31]
changed: [192.168.1.36]

TASK [httpd : Install Package Httpd] ************************************************************************
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.31]
changed: [192.168.1.36]

TASK [httpd : Copy Httpd Template File] *********************************************************************
changed: [192.168.1.33]
changed: [192.168.1.36]
changed: [192.168.1.32]
changed: [192.168.1.31]

TASK [httpd : Start Httpd Service] **************************************************************************
changed: [192.168.1.36]
changed: [192.168.1.31]
changed: [192.168.1.32]
changed: [192.168.1.33]

RUNNING HANDLER [httpd : Restart Httpd] *********************************************************************
changed: [192.168.1.36]
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.31]

PLAY RECAP **************************************************************************************************
192.168.1.31               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.32               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.33               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.36               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible roles]# ansible-playbook httpd_roles.yml 

PLAY [all] **************************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [192.168.1.32]
ok: [192.168.1.33]
ok: [192.168.1.31]
ok: [192.168.1.36]

TASK [httpd : Create a Startup Group] ***********************************************************************
changed: [192.168.1.32]
changed: [192.168.1.31]
changed: [192.168.1.33]
changed: [192.168.1.36]

TASK [httpd : Create Startup Users] *************************************************************************
changed: [192.168.1.31]
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.36]

TASK [httpd : Install Package Httpd] ************************************************************************
changed: [192.168.1.31]
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.36]

TASK [httpd : Copy Httpd Template File] *********************************************************************
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.31]
changed: [192.168.1.36]

TASK [httpd : Start Httpd Service] **************************************************************************
fatal: [192.168.1.36]: FAILED! => {"changed": false, "msg": "httpd: Syntax error on line 56 of /etc/httpd/conf/httpd.conf: Include directory '/etc/httpd/conf.modules.d' not found\n"}
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.31]

RUNNING HANDLER [httpd : Restart Httpd] *********************************************************************
changed: [192.168.1.33]
changed: [192.168.1.32]
changed: [192.168.1.31]

PLAY RECAP **************************************************************************************************
192.168.1.31               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.32               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.33               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.36               : ok=5    changed=4    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

4 ansible roles summary

1, the time of writing task (task), and there is no need to write the host needs to perform, write a simple task can be doing, installed software is installed the software, start is activated. Alone can do a certain thing, and finally by main.yml these individual tasks include installing execution order can come in so clear and easy maintenance.
2, when installed directly define the variable k: v variable format will write in vars / main.yml file can then task or template can be called directly, it will automatically go vars / main.yml file to look inside.
3, when the definition of handlers, write directly handlers / main.yml files need to do something, and more can, then you can write all the documents which can also be written as separate as the task, by introducing the same can include. When the task calls notify write directly corresponds to the name of the handlers (both height must have been).
4, can be placed in the same template file, the task when called to write directly to the file name, it will automatically go inside to find the templates under templates directory. Note: If the character is a single task call when another role, then the task if some template or file, you have to write the absolute path.

Published 110 original articles · won praise 12 · views 8043

Guess you like

Origin blog.csdn.net/qq_43141726/article/details/104426591