ansible角色的安装及使用

  • 一、前言
  • 一、ansible 角色简介
  • 二、roles目录结构
  • 二、roles角色的创建
  • 三、角色的使用
  • 四、控制任务的执行顺序
  • 五、多重角色的使用
  • 下载角色


一、前言

一、ansible 角色简介

1、Ansible roles 是为了层次化,结构化的组织Playbook
2、roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并可以便捷地include它们
3、 roles一般用于基于主机构建服务的场景中,在企业复杂业务场景中应用的频率很高
4、 以特定的层级目录结构进行组织的tasks、variables、handlers、templates、files等;相当于函数的调用把
各个功能切割成片段来执行。

二、roles目录结构

files

存放copy或script等模块调用的函数

tasks

定义各种task,要有main.yml,其他文件include包含调用

handlers

定义各种handlers,要有main.yml,其他文件include包含调用

vars

定义variables,要有main.yml,其他文件include包含调用

templates

存储由template模块调用的模板文本

meta

定义当前角色的特殊设定及其依赖关系,要有main.yml的文件

main.yml的文件

用于设定默认变量

tests

用于测试角色

二、roles角色的创建

1、更改roles的路径为当前文件路径下的roles

[root@ansible ~] su - ck
[ck@ansible ~]$ cd ansible/
[ck@ansible ansible]$ vim ansible.cfg

ansible 其他用户 ansible 创建用户_apache

2、创建角色文件

[ck@ansible ansible]$ mkdir roles
[ck@ansible ansible]$ cd roles/
[ck@ansible roles]$ ansible-galaxy init vsftpd       创建httpd角色
[ck@ansible roles]$ ansible-galaxy list              查看角色

建立vsftpd角色成功

ansible 其他用户 ansible 创建用户_linux_02

三、角色的使用

例题1:下载安装vsftpd,根据变量更改配置文件。
1、书写task主任务

[ck@ansible ~]$ vim ansible/roles/vsftpd/tasks/main.yml 




---
# tasks file for vsftpd
- name: install vsftpd                     安装模块
  dnf:
    name: vsftpd
    state: latest
  notify:                                  触发器
    - restart vsftpd
    - firewalld set

- name: set vsftpd                        修改配置模块
  lineinfile:                            
    path: /etc/vsftpd/vsftpd.conf
    regexp: "anonymous_enable"
    line: "anonymous_enable={{ STATE }}"
  notify:                                 触发器
    - restart vsftpd
/

2、触发器模块

---
# handlers file for vsftpd
- name: restart vsftpd             vsftpd模块
  service:
    name: vsftpd
    state: restarted
    enabled: yes

- name: firewalld set              防火墙模块
  firewalld:
    name: ftp
    state: enabeld
    permanent: yes
    immediate: yes

3、变量模块

---
# vars file for vsftpd
STATE: YES

4、启用模块

---
- name: install vsftpd server
  hosts: all
  roles:
    - role: vsftpd

ansible 其他用户 ansible 创建用户_ansible 其他用户_03


例题2:

下载http

输入域名westos.westos.org ------得到访问测试页westos.westos.org

输入域名linux.westos.org ------得到访问测试页linux.westos.org

输入其他默认域名 ------得到访问测试页www.westos.org

1、建立角色apache

[ck@ansible ansible]$ cd roles/
[ck@ansible roles]$ ansible-galaxy init apache

2、设置变量

[ck@ansible ansible]$ vim  roles/apache/templates/vhosts.conf.j2

//
---
# vars file for apache
WEBS:
  - docroot: /var/www/html
    index: www.westos.org
    
  - docroot: /var/www/vhosts/westos.org/westos
    name: westos.westos.org
    index: westos.westos.org
    
  - docroot: /var/www/vhosts/westos.org/linux
    name: linux.westos.org
    index: linux.westos.org
//

2、设置task任务

[ck@ansible ansible]$ vim roles/apache/tasks/main.yml

///
---
# tasks file for apache
- name: install apache
  dnf:
    name: httpd
    state: latest
  notify:
    - restart apache
    - firewalld
- name: create documentroot
  file:
    path: "{{ item.docroot }}"
    state: directory
  loop:
    "{{WEBS}}"
- name: create index.html
  copy:
    dest: "{{ item.docroot }}/index.html"
    content: "{{ item.index }}"
  loop:
    "{{WEBS}}"
- name: set vhost
  template:
    src: vhosts.conf.j2
    dest: /mnt/vhost.conf
  notify:
    - restart apache

3、设置j2模板

{% for vhost in WEBS %}
{% if vhost['name'] is not defined %}
  <VirtualHost _default_:80>
{%endif%}
{% if vhost['name'] is defined %}
  <VirtualHost *:80>
  ServerName {{vhost['name']}}
{%endif%}
  DocumentRoot   {{vhost['docroot']}}
</VirtualHost>
{% endfor %}

3、设置触发器

[ck@ansible ansible]$ vim roles/apache/handlers/main.yml 

/
---
# handlers file for apache
- name: restart apache
  service:
    name: httpd
    state: restarted
    enabled: yes

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

3、设置执行任务

[ck@ansible ansible]$ vim apache.yml  

//            
---
- name: install vsftpd server
  hosts: westos
  roles:
    - role: apache
/

四、控制任务的执行顺序

在上一个实验的基础上添加任务的执行顺序。

pre_task

任务执行前

post_tasks

任务执行后

[ck@ansible ansible]$ vim apache.yml 


---
- name: install vsftpd server
  hosts: all
  roles:
    - role: apache
  pre_tasks:                     任务执行前
    - name: show pre
      debug:
        msg: start
  post_tasks:                    任务执行后
    - name: show post
      debug:
        msg: end

结果显示:

在角色任务之前之前有任务执行

ansible 其他用户 ansible 创建用户_apache_04


在角色任务之前之后有任务执行

ansible 其他用户 ansible 创建用户_python_05

五、多重角色的使用

下载角色

访问地址角色下载地址:install https://galaxy.ansible.com roles

ansible 其他用户 ansible 创建用户_python_06


ansible 其他用户 ansible 创建用户_apache_07


下载角色成功

ansible 其他用户 ansible 创建用户_ansible 其他用户_08


执行文件

[ck@ansible ansible]$ vim install_apache_role.yml

/
---
- src: file:///tmp/apache.tar.gz
  name: apache

成功查看到角色

ansible 其他用户 ansible 创建用户_ansible 其他用户_09