本文共 5066 字,大约阅读时间需要 16 分钟。
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_path = path/roles (默认目录:/etc/ansible/roles)vim ansible.cfgroles_path = ~/
$ ansible-galaxy init apache$ ansible-galaxy list
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:
角色:将完整的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
#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
链接: .链接: .
链接: .
链接: .
转载地址:http://mnhzi.baihongyu.com/