目录
1.ansible角色简介
*Ansible roles 是为了层次化,结构化的组织Playbook
*roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并可以便捷地include它们
*roles一般用于基于主机构建服务的场景中,在企业复杂业务场景中应用的频率很高
*以特定的层级目录结构进行组织的tasks、variables、handlers、templates、files等;相当于函数的调用把各个功能切割成片段来执行。
2.roles使用
roles_path = ~/.ansible/roles 存放roles目录的定义
[Devopes@westoslinux .ansible]$ cat ansible.cfg
[defaults]
inventory = ~/.ansible/inventory
remote_user = Devopes
host_key_checking= False
modul_name = shell
roles_path = ~/.ansible/roles
[privilege_escalation]
become=True
become_method=sudo
become_user=root
#become_ask_pass=False
makdir ~/.ansible/roles
ansible-galaxy init apache 设定apache目录
ansible-galaxy list 查看目录
[Devopes@westoslinux apache]$ ls
defaults files handlers meta README.md tasks templates tests vars
定义各种handles 定义task j2文本 测试角色 vars_file
[Devopes@westoslinux .ansible]$ cat roles/apache/handlers/main.yml
---
- name: restart
service:
name: httpd
state: restarted
[Devopes@westoslinux .ansible]$ cat roles/apache/tasks/main.yml
- name: install apache
dnf:
name: httpd
state: present
- name: start http
service:
name: httpd
state: started
enabled: yes
- name:
template:
src: ./vhost.j2
dest: /etc/httpd/conf.d/vhost.conf
notify: restart
- name: mkdir doc
file:
path: "{{item['doc']}}"
state: directory
loop: "{{webs}}"
- name: edit indext
copy:
content: "{{item['index']}}"
dest: "{{item['doc']}}/index.html"
loop: "{{webs}}"
- name: firewalld
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
[Devopes@westoslinux .ansible]$ cat roles/apache/vars/main.yml
---
webs:
- doc: /var/www/html/
index: www.westos.com
- name: linux.westos.com
doc: /var/www/westos.com/linux
index: linux.westos.com
- name: new.westos.com
doc: /var/www/westos.com/new
index: new.westos.com
[Devopes@westoslinux .ansible]$ cat roles/apache/templates/
cat: roles/apache/templates/: Is a directory
[Devopes@westoslinux .ansible]$ cat roles/apache/templates/vhost.j2
{% for vhost in webs %}
{% if vhost.name is not defined %}
<VirtualHost _default_:80>
{%endif%}
{% if vhost.name is defined %}
<VirtualHost *:80>
Servername {{vhost.name}}
{%endif%}
DocumentRoot {{vhost.doc}}
</VirtualHost>
{%endfor%}
[Devopes@westoslinux roles]$ tree apache/
apache/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
│ └── vhost.j2
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
8 directories, 9 files
playbook中使用
编辑YML文件
---
- hosts: westos
roles:
- role: apache
控制执行任务的顺序
---
- hosts: westos
roles:
- role: apache
pre_tasks: 任务前执行
- name: remove apache
dnf:
name: httpd
state: absent
post_tasks: 任务后执行
- name: debug
debug:
msg: "linux is good"
tasks:
- name: debug
debug:
msg: "普通任务"
3.ansible-galaxy
*Ansible Galaxy 是一个免费共享和下载 Ansible 角色的网站,可以帮助我们更好的定义和学习roles。
*ansible-galaxy命令默认与https://galaxy.ansible.com网站API通信,可以查找、下载各种社区开发的 Ansible 角色
*ansible-galaxy在 Ansible 1.4.2 就已经被包含了
*在galaxy.ansible.com网站查询roles
本地角色的下载
evopes@westoslinux .ansible]$ cd roles/
[Devopes@westoslinux roles]$ ls
apache geerlingguy.nginx
[Devopes@westoslinux roles]$ vim install_apache.yml
[Devopes@westoslinux roles]$ tar zcf /tmp/apache.tar.gz apache/
[Devopes@westoslinux roles]$ ansible-galaxy install -r install_apache.yml
- downloading role from file:///tmp/apache.tar.gz
- extracting westos to /home/Devopes/.ansible/roles/westos
- westos was installed successfully
[Devopes@westoslinux roles]$ ls
apache geerlingguy.nginx install_apache.yml westos
[Devopes@westoslinux roles]$ cat install_apache.yml
---
- src: file:///tmp/apache.tar.gz
name: westos
网络上的下载
安装选择角色
install https://galaxy.ansible.come roles
$ansible-galaxy install geerlinggue.nginx