ansible roles 简介
Ansible Roles是一种组织和管理Ansible Playbooks的方法。它们允许将相关的配置和任务分组到一个可重用的单元中,使得代码更加模块化和可维护。
一个Ansible Role包含了一组预定义的变量、任务和文件结构。它可以被其他Playbooks调用和使用,从而实现代码的重用。Roles可以用于定义各种不同的配置,从简单的软件安装和配置到复杂的系统部署。
Roles通常包含以下几个重要的目录和文件:
- tasks:包含了要执行的任务列表,可以是Ansible模块的调用或者是其他角色的引用。
- handlers:包含了用于响应特定事件的任务,比如重新启动服务或者重新加载配置文件。
- vars:包含了角色的变量定义,这些变量可以在角色的其他部分中引用。
- defaults:包含了角色的默认变量值。
- templates:包含了配置文件的模板,可以使用Jinja2模板引擎来渲染这些文件。
- files:包含了需要复制到被管理节点的文件。
使用Roles可以极大地简化Ansible代码的编写和维护工作,提高代码的可读性和可重用性。同时,Roles还使得团队协作更加容易,可以方便地共享和复用角色。
ansible roles 语法
Ansible roles 是一种组织和结构化Ansible playbook的方法。它允许您将任务、变量和模板组织到可重复使用的模块中,以便在多个playbook中重复使用。
下面是一个典型的Ansible role的目录结构:
roles/myrole/tasks/main.ymlvars/main.ymlfiles/myfile.txttemplates/mytemplate.j2handlers/main.ymlmeta/main.yml
在此目录结构中,各个目录的作用如下:
tasks/
:包含执行任务的YAML文件。vars/
:包含变量定义的YAML文件。files/
:包含要传输到目标主机的文件。templates/
:包含要在目标主机上生成的模板。handlers/
:包含处理器定义的YAML文件。meta/
:包含角色元数据的YAML文件。
在main.yml
文件中,您可以定义要在角色中执行的任务、变量和文件。
通过使用role,您可以在playbooks中使用include_role
或roles
关键字来导入并使用role。
以下是一个使用role的示例:
- name: Example playbookhosts: webroles:- myrole
上面的示例将导入名为myrole
的role,并在web
组的主机上运行。
1、使用ansible playbook部署httpd,要求httpd.conf由模板生成,对应的servername改为受控端IP
cd /etc/ansible/roles
ansible-galaxy init httpd
主yaml rolets.yml
---
- hosts: h1remote_user: rootroles:- httpd
---
# tasks file for httpd
- name: install httpd packageyum: name={{package}} state=latest
- name: install configure file#使用template模板template: src=/etc/ansible/httpd.conf.j2 dest=/etc/httpd/conf/httpd.confnotify:- restart httpd
- name: create root dirfile: path=/etc/httpd/htdocs state=directory
- name: start httpd serverservice: name={{service}} enabled=true state=started
---
# handlers file for httpd
- name: restart httpdservice: name={{service}} state=restarted
---
# vars file for httpd
package: httpd
service: httpd
......
#/etc/ansible/roles/httpd/templates/httpd.conf.j2 95行
ServerName {{ansible_ens33.ipv4.address}}:80
......
2、编写一个playbook,当模板任务执行完成时触发一个debug任务
ansible-galaxy init debug823
---
# tasks file for debug823
- name: copy filecopy: content: |#!/bin/bashecho "Hello World"dest: /tmp/hello.shmode: '0755'- name: run scriptcommand: /tmp/hello.shnotify: success
---
# handlers file for debug823
- name: successdebug:msg: "This is a success message"
/etc/ansible/debugtestplaybook.yml
---
- hosts: h1remote_user: rootroles:- debug823
3、编写一个复制一文件到被控端的playbook
echo 123 > /etc/ansible/roles/debug823/files/testcopy.txt
---
# tasks file for debug823
- name: copy filecopy: src: testcopy.txtdest: /tmp/testcopy.txtmode: '0644'owner: rootgroup: root- name: debug taskdebug:msg: "This is a debug message"
4、编写一个playbook,部署前后端分离项目
192.168.99.132 nginx
192.168.99.144 java
192.168.99.177 mysql
主playbook yaml
- name: nginx installhosts: nginxroles:- nginx- name: java installhosts: javaroles:- java- name: mysql installhosts: mysqlroles:- mysql
nginx role
---
# tasks file for nginx
- name: Install epel-releaseyum:name: epel-releasestate: present- name: Install nginxyum:name: nginxstate: present- name: Start nginx serviceservice:name: nginxstate: started- name: Enable nginx serviceservice:name: nginxenabled: yes- name: copy project bmcopy:src: bmdest: /usr/share/nginx/html/mode: 0644owner: nginxgroup: nginx- name: Configure nginxtemplate:src: nginx.conf.j2dest: /etc/nginx/nginx.confnotify:- restart nginx
nginx.conf.j2
……server {listen 80; server_name {{ansible_ens33.ipv4.address}};location / {root /usr/share/nginx/html/bm;index index.html index.htm;} location /bm {proxy_pass http://java:8090;} ……
java role
---
# tasks file for java
- name: copy jdkcopy:src: jdk-8u371-linux-x64.rpmdest: /tmp/jdk-8u371-linux-x64.rpm- name: install jdkyum:name: /tmp/jdk-8u371-linux-x64.rpmstate: present- name: copy carton.jarcopy:src: carbon.jardest: /opt/carbon.jarmode: 0755- name: copy hostscopy:src: /etc/hostsdest: /etc/hosts- name: start carbon.jarcommand: nohup java -jar /opt/carbon.jar ®ister: resultasync: 3600poll: 0
mysql role
---
# tasks file for mysql- name: install mariadbyum:name: mariadb-serverstate: present- name: start mariadbservice:name: mariadbstate: started- name: copy carbon.sql to /tmpcopy: src=carbon.sql dest=/tmp/carbon.sql- name: run init.expscript: init.exp- name: execute SQLshell: mysql -uroot -p{{mysql_password}} < /tmp/carbon.sql
init.exp
#!/usr/bin/expect
set timeout 30
spawn mysql -uroot -p
expect {"*password*" {send "\r"}
}
expect ">*"
send "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');\r"
expect ">*"
send "create user 'root'@'%' identified by 'root';\r"
expect ">*"
send "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';\r"
expect ">*"
send "flush privileges;\r"
expect ">*"
send "quit\r"
expect eof