综合架构之ansible(角色相关内容)

补充:

#inventory      = /etc/ansible/hosts  #配置主机清单路径的地方。
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp
#local_tmp      = ~/.ansible/tmp
ansible

ansible剧本编写方式: 角色

  1. 规范ansible程序目录结构
  2. 汇总剧本中有定义的主机
    3.将任务分开实现不同主机随时都可以模块

编写的步骤

  • 1首先进入/etc/ansible/roles的目录中
  • 2 不同的服务创建不同的目录
  • 3 然后再进入目录中创建子目录
  • 4 有规划的创建子目录
{vars,tasks,templates,handlers,files}

vars: 定义变量信息
tasks: 定义任务信息 -----------> 先编写
templates: 定义模板文件(jinja2模板文件)
handlers: 定义触发器信息
files: 定义需要分发的文件


利用角色编写nfs服务

第一个历程: 编写文件信息

tasks: 任务信息编写方式一:

vim main.yaml
- name: 01:install nfs rpcbind
  yum:
    name: ['nfs-utils', 'rpcbind'] 
    state: installed
- name: 02:copy conf file
  copy: src=/etc/ansible/ansible_playbook/nfs.conf  dest=/etc/{{ conf_file }}
  notify: 
    - nfs_restart
   # - nfs_create_dir
- name: 03:create data dir 
  file: path={{ data_dir }} state=directory owner=nfsnobody group=nfsnobody
- name: 04:boot server rpcbind
  service: name={{ item.name }} state={{ item.state }} enabled={{ item.enabled }}
  with_items:
    - {name: "rpcbind", state: "started", enabled: "yes"}
    - {name: "nfs",     state: "started", enabled: "yes"}
- name: 01:install nfs
  yum: name=nfs-utils state=installed
- name: 02:mount data dir
  mount: src=172.16.1.31:{{ data_dir }} path=/mnt fstype=nfs state=mounted
- name: 03:check mount info
  shell: df -h|grep mnt
  register: mount_info
- debug: msg={{ mount_info.stdout_lines }}

tasks: 任务信息编写方式二(拆分成多个小任务----->更标准)

[root@m01 tasks]# ll
    total 24
    -rw-r--r-- 1 root root 162 Jul 29 10:30 main.yaml //将所有的任务汇总到这里。每个任务中有判断功能。
    -rw-r--r-- 1 root root 296 Jul 29 10:28 nfs_boot.yaml
    -rw-r--r-- 1 root root 194 Jul 29 10:26 nfs_conf.yaml //注意进行判断 
    -rw-r--r-- 1 root root 156 Jul 29 10:27 nfs_datadir.yaml
    -rw-r--r-- 1 root root  96 Jul 29 10:18 nfs_install.yaml
    -rw-r--r-- 1 root root 273 Jul 29 10:28 nfs_mount.yaml

vim main.yaml
- include_tasks: nfs_install.yaml
- include_tasks: nfs_conf.yaml
- include_tasks: nfs_datadir.yaml
- include_tasks: nfs_boot.yaml
- include_tasks: nfs_mount.yaml

vars:
      vim main.yaml
      conf_file: exports
      data_dir: /data
files:
[root@m01 files]# ll
total 4
-rw-r--r-- 1 root root 42 Jul 29 10:34 nfs.conf
//配置文件在角色中放着就可以了。
handlers:
cat main.yaml 
- name: nfs_restart
  service: name=nfs state=reloaded

可以做一个初始化目录 roles下的init目录


使用ansible角色任务读取的流程图

读取流程
  • 1.执行一键化剧本 剧本中汇总多个角色
  • 2.加载host文件
  • 3.task目录 找到main.yaml找到具体执行什么任务
  • 4.如果配置了变量的信息 会找vars
  • 5.如果设计到文件的信息,会找文件的信息
  • 6.再去找handlers进行触发。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1)安装2)常用模块3)inventory4)playbook(role\tag\template)5) yaml...
    秦记阅读 9,801评论 2 5
  • 运维核心工作: 操作系统安装(物理机、虚拟机)--> 应用程序包部署(安装、配置、服务启动 )--> 批量操作 -...
    Net夜风阅读 5,523评论 0 4
  • 本文作者: wuXing QQ: 1226032602 E-mail: 1226032602@qq.com ht...
    五行哥阅读 6,986评论 0 0
  • 一.ansible (1) ansible: ansible是一款新出现的自动化运维系统,基于python开发并集...
    楠人帮阅读 6,014评论 0 8
  • 简介 架构 原理 组成 ANSIBLE PLAYBOOKS:任务剧本(任务集),编排定义Ansible任务集的配置...
    毛利卷卷发阅读 4,733评论 0 2