动漫制作专业名称,seo扣费系统源码,深圳高端女装品牌排行榜,网络教育室内设计专业Ansible中的角色使用#xff1a;
目录
一、ansible角色简介
二、roles目录结构 三、roles的创建
四、roles的使用
1、书写task主任务
2、触发器模块
3、变量模块 4、j2模块 5、files模块 6、启用模块
7、执行playbook
五、控制任务执行顺序
六、多重角色的使用 一…Ansible中的角色使用
目录
一、ansible角色简介
二、roles目录结构 三、roles的创建
四、roles的使用
1、书写task主任务
2、触发器模块
3、变量模块 4、j2模块 5、files模块 6、启用模块
7、执行playbook
五、控制任务执行顺序
六、多重角色的使用 一、ansible角色简介
Ansible roles 是为了层次化结构化的组织Playbook roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中并可以便捷地include它们roles一般用于基于主机构建服务的场景中在企业复杂业务场景中应用的频率很高以特定的层级目录结构进行组织的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命令默认与https://galaxy.ansible.com网站API通信可以查找、下载各种社区开发的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.htmlline: {{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 httpd2、触发器模块
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.orgs page- name: bbs.westos.orgdoc: /var/www/virtual/westos.org/bbs/htmlindex: bbs.westos.orgs page- name: login.westos.orgdoc: /var/www/virtual/westos.org/login/htmlindex: login.westos.orgs page 4、j2模块
vim templates/vhosts.conf.j2
cat templates/vhosts.conf.j2{% for web in webs %}
{% if web.name is defined %}
VirtualHost *:80ServerName {{web.name}}
{% endif %}
{% if web.name is not defined %}
VirtualHost _default_:80
{% endif %}DocumentRoot {{web.doc}}
/VirtualHost
{% endfor %}
Directory /var/www/virtual/westos.org/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: apache7、执行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.com roles 2、搜索nginx往后翻一下找到roles 3、下载角色
ansible-galaxy collection install ivansible.nginx
要注意出于安全原因下载的代码需要仔细研读每一行代码