Ansible中的角色使用

编程入门 行业动态 更新时间:2024-10-19 22:38:31

Ansible中的<a href=https://www.elefans.com/category/jswz/34/1771328.html style=角色使用"/>

Ansible中的角色使用

Ansible中的角色使用:

目录

一、ansible角色简介

二、roles目录结构

 三、roles的创建

四、roles的使用 

1、书写task主任务

2、触发器模块

3、变量模块

 4、j2模块

 5、files模块 

 6、启用模块

7、执行playbook

五、控制任务执行顺序

六、多重角色的使用


 

一、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的文件
defaults要有main.yml的文件,用于设定默认变量
tests用于测试角色

 三、roles的创建

ansible—galaxy命令工具:
Ansible Galaxy 是一个免费共享和下载 Ansible 角色的网站,可以帮助我们更好的定义和学习roles;
ansible-galaxy命令默认与,可以查找、下载各种社区开发的Ansible 角色。

vim ansible.cfg

 

mkdir rolescd roles/
ansible-galaxy init apache

cd ..
ansible-galaxy list

四、roles的使用 

例子:下载httpd,配置虚拟主机并认证加密

1、书写task主任务

vim tasks/main.yml- name: yumyum:name: httpdstate: present- name: serviceservice:name: httpdstate: startedenabled: yes- name: create doclineinfile:path: "{{item.doc}}/index.html"line: "{{item.index}}"create: yesloop: "{{webs}}"- name: create vhosts.conftemplate:src: vhosts.conf.j2dest: /etc/httpd/conf.d/vhost.confnotify: restart httpd- name: authcopy:src: .htpasswddest: /etc/httpd/.htpasswdnotify: restart httpd

2、触发器模块

vim handlers/main.yml
cat handlers/main.yml- name: restart httpdservice:name: httpdstate: restarted

3、变量模块

vim vars/main.yml
cat vars/main.ymlwebs:
- doc: /var/www/htmlindex: "www.westos's page"- name: bbs.westosdoc: /var/www/virtual/westos/bbs/htmlindex: "bbs.westos's page"- name: login.westosdoc: /var/www/virtual/westos/login/htmlindex: "login.westos's page"

 4、j2模块

vim templates/vhosts.conf.j2
cat templates/vhosts.conf.j2{% for web in webs %}
{% if web.name is defined %}
<VirtualHost *:80>ServerName {{web.name}}
{% endif %}
{% if web.name is not defined %}
<VirtualHost _default_:80>
{% endif %}DocumentRoot {{web.doc}}
</VirtualHost>
{% endfor %}
<Directory "/var/www/virtual/westos/login/html">
AuthUserfile "/etc/httpd/.htpasswd"
AuthName "Please input your name and password"
AuthType basic
Require user yyl
</Directory>

 5、files模块 

touch .htpasswd
htpasswd -cm .htpasswd yyl
cp /etc/httpd/.htpasswd files/

 6、启用模块

vim httpd.yml- name: instell httphosts: allroles:- role: apache

7、执行playbook

ansible-playbook httpd.yml

 

 

五、控制任务执行顺序

pre_task:任务执行前
post_tasks:任务执行后

例子:

- name: instell httphosts: allpre_tasks:- name: testdebug:msg: this is start !!roles:- role: apachepost_tasks:- name: show postdebug:msg: this is end !!

 

六、多重角色的使用

1、访问地址角色下载地址:galaxy.ansible roles

2、搜索nginx,往后翻一下找到roles

 

3、下载角色 

ansible-galaxy collection install ivansible.nginx

要注意出于安全原因,下载的代码需要仔细研读每一行代码!!!

更多推荐

Ansible中的角色使用

本文发布于:2023-11-16 17:34:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:角色   Ansible

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!