ansible use of roles

ansible roles

A. Roles role

The main role is to reuse playbook, for example, we will install whatever software installation time synchronization service, then each must playbook written ntp task. We can ntp task written, until the time of use and then call on the line. Ansible which will be organized into role, he has a fixed format organization. Calls for playbook

Two. Relos Introduction

specific tasks organized in the hierarchical directory structure, variables, handlers, templates, files and the like; function call corresponding to respective functions to be performed into fragments

1. hierarchical directory structure

role_name/:我们定义的role的名字
    file/:用于存放copy或script等模块调用的函数
    tasks/:用于定义各种task,此目录一定要有main.yml;其他文件需要main.yml包含调用
    handlers:用于定义各种handlers,此目录一定要有main.yml;其他文件需要main.yml包含调用
    vars:用于定义variables,此目录一定要有main.yml;其他文件需要main.yml包含调用
    templates/:存储由template模块调用的模板文本;
    meta/:定义当前角色的特殊设定及其依赖关系,此目录中至少应该有一个名为main.yml的文件;其它的文件需要由main.yml进行“包含”调用;
    default/:此目录中至少应该有一个名为main.yml的文件,用于设定默认变量;

2. Call role in the playbook

Role path defined in the stored profile /etc/ansible/ansible.cfg

roles_path    = /etc/ansible/roles:/usr/share/ansible/roles
第一种:
- hosts: HOSTS
  remote_user: root roles: - ROLE_NAME1 - ROLE_NAME2 第二种:除了字典第一个元素指明调用的role,后面是传递给role的变量 - hosts: HOSTS remote_user: root roles: - { role: ROLE_NAME1, VARIABLE1: VALUE1, ...} 第三种:when指明role调用的条件 - hosts: HOSTS remote_user: root roles: - { role: ROLE_NAME1, when: CONDITIONS}

3. Example

1) directory structure:

 

2). Tasks file

- name: install httpd package
  yum: name=httpd state=present
- name: install configure file template: src=httpd.conf.c{{ ansible_distribution_major_version }}.j2 dest=/etc/httpd/conf/httpd.conf tags: instconf notify: restart httpd service - name: start httpd service service: name=httpd state=started enabled=true

 

3). Handlers file

 

4) template file

 

5) Variable

 

6). Playbook file

- hosts: webservers
  remote_user: root roles: - { role: httpd }

 

7) Test playbook file

 

8) Run playbook and view the httpd port

 

Guess you like

Origin www.cnblogs.com/yhq123/p/11210052.html