ansible 自动化运维角色简单应用(role)

Ansible Role详细总结

Ansible Role角色用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、task以及handlers。要使用roles只需要在playbook中使用include指令即可。简单来说,roles将变量、文件、任务、模板以及处理器放置于单独的目录中,通过include指令便捷的使用变量、文件、任务、模板等。角色一般用于基于主机构建服务的场景中,也可以是用于构建守护进程等单独场景中。

一、roles各目录作用

命令:ansible-galaxy init web 创建一个web role模板

  • files:存放由copy或script模块等调用的文件
  • templates:template模块查找所需要模板文件的目录
  • tasks:定义task、role的基本元素,至少应该包含一个名为main.yml的文件。其他文件需要在此文件中通过include进行包含
  • handlers:至少应该包含一个名为main.yml的文件,其他文件需要在此文件中通过include进行包含
  • vars:定义变量,至少应该包含一个名为main.yml的文件,其他文件需要在此文件中通过include进行包含
  • meta:定义当前角色的特殊设定及其依赖关系,至少应该包含一个名为main.yml的文件,其他文件需在此文件中通过include进行包含
  • default:设定默认变量时使用此目录中的main.yml文件,比vars的优先级低

   二、利用web模板对受控主机部署web服务

      部署思路: 写本地源(利用本地源安装),安装httpd,启动httpd服务,关闭防火墙(本此实验的ansible环境在/root/test/)

 进入web/tasks下编写剧本

在tasks目录下用touch命令创建四个后缀为.yml的文件(分别是本地源、安装、开启httpd,关闭firewalld)

[root@localhost tasks]# touch intall_httpd.yml
[root@localhost tasks]# touch start_httpd.yml
[root@localhost tasks]# touch stop_firewalld.yml
[root@localhost tasks]# touch write_local_repo.yml
[root@localhost tasks]# touch cop_index_httpd.yml
[root@localhost tasks]# tree .
.
├── cop_index_httpd.yml
├── intall_httpd.yml
├── main.yml
├── start_httpd.yml
├── stop_firewalld.yml
└── write_local_repo.yml

0 directories, 6 files
[root@localhost tasks]# 
 

 文件编写(cd web/task)

[root@localhost tasks]# cat cop_index_httpd.yml 
---
- name: copy index.html > /var/www/html/
  copy:
   src: index.html
   dest: /var/www/html/

[root@localhost tasks]# cat write_local_repo.yml 
---
- name: mount sr0
  mount:
   src: /dev/sr0
   fstype: iso9660
   path: /mnt
   state: mounted
- name: mkdir bak
  file:
   path: /etc/yum.repos.d/bak
   state: directory
- name: mv C*
  shell:
   cmd: 'mv /etc/yum.repos.d/C* /etc/yum.repos.d/bak/'
  ignore_errors: yes
- name: write_local_repo
  yum_repository:
   name: centos7
   baseurl: file:///mnt
   gpgcheck: no
   enabled: yes
   description: centos7

[root@localhost tasks]# cat intall_httpd.yml 
---
- name: install httpd
  yum:
   name: httpd
   state: latest
[root@localhost tasks]# cat stop_firewalld.yml 
---
- name: stop firewalld
  service:
   name: firewalld
   state: stopped
[root@localhost tasks]# cat start_httpd.yml 
---
- name: start httpd service
  service: 
   name: httpd
   state: started

主文件编写

[root@localhost tasks]# cat main.yml 
---
# tasks file for web
- include_tasks: "{{ item }}"
  loop:
  - write_local_repo.yml
  - intall_httpd.yml
  - start_httpd.yml
  - stop_firewalld.yml
  - cop_index_httpd.yml 

因为上面有使用copy网页文件到受控主机/var/www/html/

 所以要在控制主机上(web/files)下创建index.html文件

[root@localhost tasks]# cd ../files/
[root@localhost tasks]# echo "<h1>This is a page written in Ansible role</h1>" >> index.html

编写完成

[root@localhost files]# tree ../../web/
../../web/
├── defaults
│   └── main.yml
├── files
│   └── index.html
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   ├── cop_index_httpd.yml
│   ├── intall_httpd.yml
│   ├── main.yml
│   ├── start_httpd.yml
│   ├── stop_firewalld.yml
│   └── write_local_repo.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 14 files
[root@localhost files]# 

回到/root/test目录编写yml运行roles(hosts文件要有主机的ip)

[root@localhost test]# cat role.yml 
---
- name: go role
  hosts: web
  roles:
  - web

检查语法有没有问题

[root@localhost test]# ansible-playbook --syntax-check role.yml 

playbook: role.yml
[root@localhost test]#

没问题就运行 ansible-playbook  role.yml(红色那里是遇到错误跳过,因为那里用的是shell模块)

 成功浏览

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值