AIOps 一场颠覆传统运维的盛筵
880
2022-10-04
自动化运维工具之Ansible-剧本及角色介绍
Ansible提供两种方式去完成任务,一是 ad-hoc 命令,一是写 Ansible playbook.前者可以解决一些简单的任务, 后者解决较复杂的任务。
1.Ansible playboos include 使用
定义一个顶级剧本main.yml 引用 负载均衡,web,DB组剧本。include的时候可以添加变量,顶级模块的变量覆盖子模块的变量。
cat main.yml - name: this is a play at the top level of a file hosts: ihdmgr remote_user: hdm tasks: - name: say hi tags: foo shell: echo "hi..." handlers: - include: handlers/handlers.yml# define vars- include: load_balancers.yml vip=192.168.1.8- include: webservers.yml port=8080- include: dbservers.yml user=mysql
分别定义子剧本,变量引用用{{}}
cat webservers.yml - name: this is webgroup config hosts: webservers vars: port: 80 remote_user: hdm tasks: - name: install httpd shell: echo "httpd has been installed,and port is {{port}}" > tmp/result.txt########DB###########cat dbservers.yml - name: install database hosts: ihdmgr remote_user: hdm tasks: - name: start install DB shell: echo "DB has been installed"########loadblance##########- name: install loadblance hosts: ihdmgr remote_user: hdm tasks: - name: install LB shell: echo "loadblance has been installed and vip is {{vip}}" > tmp/result.txt
检查剧本的语法,执行任务清单,受影响主机。
ansible-playbook main.yml --list-hosts --list-tasks --syntax-checkplaybook: main.yml play #1 (this is a play at the top level of a file): host count=2 11.20.225.24 11.20.225.20 play #1 (this is a play at the top level of a file): say hi play #2 (install loadblance): host count=2 11.20.225.24 11.20.225.20 play #2 (install loadblance): install LB play #3 (this is webgroup config): host count=1 localhost play #3 (this is webgroup config): install httpd play #4 (install database): host count=2 11.20.225.24 11.20.225.20 play #4 (install database): start install DB
2.Ansible tag介绍
3. Ansible roles 介绍
#创建角色目录mkdir -pv ansible_playbooks/roles/{common,trm,apmgr,ihdmgr,activemp,report,ireport,nbitktws,nbiproc,nbitktsocket,fwupd,approbe,zabbix,webservers,mysql,oracle}/{tasks,files,templates,meta,handlers,vars}ansible_playbooks└── roles ├── activemp ├── files ├── handlers ├── meta ├── tasks ├── templates └── vars ├── apmgr ├── approbe ├── common ├── fwupd ├── ihdmgr ├── ireport ├── mysql ├── nbiproc ├── nbitktsocket ├── nbitktws ├── oracle ├── report ├── trm ├── webservers └── zabbix
这个 playbook 为一个角色 ‘x’ 指定了如下的行为:
如果 roles/x/tasks/main.yml 存在, 其中列出的 tasks 将被添加到 play 中如果 roles/x/handlers/main.yml 存在, 其中列出的 handlers 将被添加到 play 中如果 roles/x/vars/main.yml 存在, 其中列出的 variables 将被添加到 play 中如果 roles/x/meta/main.yml 存在, 其中列出的 “角色依赖” 将被添加到 roles 列表中 (1.3 and later)所有 copy tasks 可以引用 roles/x/files/ 中的文件,不需要指明文件的路径。所有 script tasks 可以引用 roles/x/files/ 中的脚本,不需要指明文件的路径。所有 template tasks 可以引用 roles/x/templates/ 中的文件,不需要指明文件的路径。所有 include tasks 可以引用 roles/x/tasks/ 中的文件,不需要指明文件的路径。
创建一个主剧本site.yml, 在idhmgr上用hdm用户进行系统基本的配置(common)和zabbix-agent安装。
cat site.yml - name: first role hosts: ihdmgr remote_user: hdm roles: - common - zabbix
common剧本配置,zabbix剧本照此模拟。
cat roles/common/tasks/main.yml - name: install LB shell: echo "common tasks running and vip is {{vip}}" >> /tmp/result.txt notify: install LB ############## # 定义handlers 注意名称和tasks中的notify一致。cat roles/common/handlers/main.yml - name: install LB shell: echo "common handlers running" >> /tmp/result.txt ########################## # 定义角色变量cat roles/common/vars/main.yml vip: 8888
运行结果校验
cat result.txt common tasks running and vip is 8888zabbix task running and port is 2222common handlers runningzabbix handlers running
参考文档:
马哥Linux培训博客:https://blog.51cto.com/mageedu
马哥Linux培训:https://magedu.com/kczx/linuxyw.html
中文手册:http://ansible.com.cn/
离线安装:https://blog.51cto.com/wutengfei/1973792
发表评论
暂时没有评论,来抢沙发吧~