博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ansible中的角色使用
阅读量:3961 次
发布时间:2019-05-24

本文共 5066 字,大约阅读时间需要 16 分钟。

ansible中的角色使用

ansible roles

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		##用于测试角色

role存放的路径在配置文件ansible.cfg中定义

roles_path = path/roles  (默认目录:/etc/ansible/roles)vim ansible.cfgroles_path = ~/

创建目录结构

$ ansible-galaxy init apache$ ansible-galaxy list

playbook中使用roles

playbook中使用roles:---- hosts: server2  roles:    - role: role1    - role: role2      var1: value1		##此处变量会覆盖roles中的定义变量

控制任务执行顺序

---- hosts: server2  roles:    - role: role1	##角色任务  pre_tasks:		##角色执行前执行的play    - tasks1  tasks:		    ##普通任务    - tasks2  post_tasks:		##在角色和普通任务执行完毕后执行的play    - tasks3  handlers:
  • example:
角色:将完整的playbook拆分开default   www.westos.com  pagewww.westos.com    /var/www/www.westos.com/html  index:www.westos.comlinux.westos.com  / var/www/linux.westos.com/html   index:linux.westos.comvim ansible.cfgroles_path = ~/1 生成一个角色ansible-galaxy init apache2 在生成的角色目录下[westos@ansible ~]$ cat apache/vars/main.yml###用来写变量---vhost:  - root: /var/www/html  - root: /var/www/virtual/westos.com/www/html    name: www.westos.com  - root: /var/www/virtual/westos.com/linux/html    name: linux.westos.com[westos@ansible ~]$ cat apache/tasks/main.yml ##写任务---- name: template  block:    - name: install apache      dnf:        name: httpd        state: latest      notify: firewalld    - name: configure apche file      template:        src: vhost.j2        dest: /etc/httpd/conf.d/vhost.conf      notify: restart apache    - name: mkdir Document      file:         path: "{
{item}}" state: directory loop:: - /var/www/www.westos.com/html - /var/www/linux.westos.com/html - name: create index.html copy: dest: "{
{ item.root}}/index.html" content: "{
{ item.index }}" loop: - root: /var/www/html index: default - root: /var/www/www.westos.com/html index: www.westos.com - root: /var/www/linux.westos.com/html index: linux.westos.com rescue: - debug: msg: dnf repo is not created[westos@ansible ~]$ cat apache/templates/vhost.j2 ##模板{% for webserver in vhost %}{% if webserver.name is not defined %}
{% endif %}{% if webserver.name is defined %}
{% endif %}{% if webserver.name is defined %} ServerName {
{ webserver.name }}{% endif %} DocumentRoot {
{webserver.root}}{% if webserver.name is not defined %} CustomLog logs/default.log combined{% endif %}{% if webserver.name is defined %} CustomLog logs/{
{ webserver.name }}.log combined{% endif %}
{% endfor %}[westos@ansible ~]$ cat apache/handlers/main.yml ###触发器---- name: restart apache service: name: httpd state: restarted enabled: yes- name: firewalld firewalld: service: http permanent: yes state: enabled immediate: yes[westos@ansible ~]$ cat vhostest.yml ##测试playbook--- name: test roles hosts: 172.25.11.1 roles: - role: apacheansible-playbook vhostest.yml

ansible—galaxy命令工具

  • Ansible Galaxy 是一个免费共享和下载 Ansible 角色的网站,可以帮助我们更好的定义和学习roles。
  • ansible-galaxy命令默认与https://galaxy.ansible.com网站API通信,可以查找、下载各种社区开发的 Ansible 角色
  • ansible-galaxy在 Ansible 1.4.2 就已经被包含了
  • 在galaxy.ansible.com网站查询roles

安装选择的角色

#install https://galaxy.ansible.com roles$ansible-galaxy install geerlingguy.nginx#在从网上下载他人的角色前,要了解其中的具体内容#install local roles$ vim install_apache_role.yml---- src: file:///mnt/apache.tar.gz  name: apache$ ansible-galaxy  install -r install_apache_role.yml

练习:使用roles配置DNSDHCP(DDNS) vim ~/ddns.yml---- name: ddns  hosts: all  roles:    - role: ~/ansible/dnstasksvim ~/ansible/dns/tasks/main.yml---- name: install dhcp-server and bind  dnf:    name: "{
{item}}" state: present loop: "{
{SOFTWARE}}" notify: set firewalld - name: create dhcpd.conf and named.conf template: src: "{
{item.src}}" dest: "{
{item.dest}}" group: "{
{item.group}}" loop: "{
{FILES}}" notify: restart servervars---SOFTWARE: - bind - dhcp-server FILES: - src: named.conf.j2 dest: /etc/named.conf group: named - src: westos.key.j2 dest: /etc/westos.key group: named - src: westos.com.zone.j2 dest: /var/named/westos.com.zone group: named - src: named.rfc1912.zones.j2 dest: /etc/name.rfc1912.zones.zones group: named - src: dhcpd.conf.j2 dest: /etc/dhcp/dhcpd.conf group: roothandlers---- name: restart server service: name: "{
{item}}" state: restarted enabled: yes loop: - named - dhcpd- name: set firewalld firewalld: service: "{
{item}}" permanent: yes state: enabled immediate: yes loop: - dns - dhcp

templates

链接: .

ansible中的角色使用

链接: .

ansible变量应用实例

链接: .

Linux高级存储管理(LVM、VDO)

链接: .

转载地址:http://mnhzi.baihongyu.com/

你可能感兴趣的文章
Hibernate中Criteria的完整用法
查看>>
sql jsp
查看>>
spring beans beanfactory applicationcontext
查看>>
使用ORM工具进行数据访问
查看>>
使用ORM工具进行数据访问
查看>>
编译与部署Eclipse+Tomcat+MySQL+Liferay4.1.2
查看>>
POJ3728,The merchant(倍增LCA+分治)
查看>>
2019 ICPC Malaysia National,E. Optimal Slots(01背包变形)
查看>>
洛谷P1638 逛画展(双向队列)
查看>>
POJ2892,Tunnel Warfare(线段树维护连续区间)
查看>>
POJ3468,A Simple Problem with Integers(线段树-区间查询-区间更新)
查看>>
杭电ACM——6463(思维)
查看>>
杭电ACM——2069,Coin Change(DP)
查看>>
杭电ACM——2110,Crisis of HDU(母函数)
查看>>
杭电AM——2152,Fruit(母函数)
查看>>
杭电ACM——2566,统计硬币(DP)
查看>>
堆栈(数据结构)
查看>>
队列(数据结构)
查看>>
Mule ESB-Content-Based Routing Tutorial(1)
查看>>
Mule ESB-Content-Based Routing Tutorial(2)
查看>>