学习b记 · 第二阶段
十七、Ansible_roles
前面还有一个jinja2模板先搞上在搞roles
Ansible Jinja2
1、什么是jinja2模板
jinja2是Python的全功能模板引擎
2、Jinja2与Ansible啥关系
Ansible通常会使用jinja2模板来修改被管理主机的配置文件等…在saltstack中同样会使用到jinja2
如果在100台主机上安装nginx,每台nginx的端口都不一样,如何解决?
3、Ansible如何使用Jinja2
使用Ansible的jinja2模板也就是使用template模块,该模块和copy模块一样,都是讲文件复制到远端主机上去,但是区别在于,template模块可以获取到文件中的变量,而copy则是原封不动的把文件内容复制过去。之前我们在推送rsync的backup脚本时,想把脚本中的变量名改成主机名,如果使用copy模块则推送过去的就是{{ ansible_fqdn }},不变,如果使用template,则会变成对应的主机名。
4、Ansible使用Jinja2注意事项
Ansible允许jinja2模板中使用条件判断和循环,但是不允许在playbook中使用。
注意:不是每个管理员都需要这个特性,但是有些时候jinja2模块能大大提高效率。
Ansible Jinja2模板使用
1、Jinja模板基本语法
{{ EXPR }}输出变量值,会输出自定义的变量值或facts
1)playbook文件使用template模块
2)模板文件里面变量使用{{名称}},比如{{PORT}}或使用facts
2、Jinja2模板逻辑判断
#循环表达式
{% for i in EXPR %}
{% endfor %}
#条件判断
{% if EXPR %}
{% elif EXPR %}
{% else %}
{% endif %}
#注释
{# COMMENT #}
示例
1、编辑playbook
[root@m01 ~]# vim jinja2.yml
- hosts: web_group
tasks:
- name: Copy Template File
template:
src: ./motd.j2
dest: /etc/motd
2、准备motd.j2文件
[root@m01 ~]# vim motd.j2
Welcome to {{ ansible_fqdn }}
This system total mem is : {{ ansible_memtotal_mb }} MB
This system free mem is: {{ ansible_memfree_mb }} MB
3、执行playbook
[root@m01 ~]# ansible-playbook jinja2.yml
PLAY [web_group] *****************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]
TASK [Copy Template File] ********************************************************************************************************************************************************************************************************************
changed: [web01]
changed: [web02]
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
web01 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#查看结果
[root@m01 ~]# ansible web_group -a 'cat /etc/motd'
web01 | CHANGED | rc=0 >>
Welcome to web01
This system total mem is : 1982 MB
This system free mem is: 1106 MB
web02 | CHANGED | rc=0 >>
Welcome to web02
This system total mem is : 1982 MB
This system free mem is: 1096 MB
Ansible Jinja2管理nginx
使用playbook推送文件
1、编辑playbook
[root@m01 ~]# vim lb.yml
- hosts: lb_group
vars:
http_port: 80
server_name: www.qxl.com
tasks:
- name: copy
template:
src: ./www.qxl.com.conf.j2
dest: /etc/nginx/conf.d/www.qxl.com.conf
notify: reload nginx
handlers:
- name: reload nginx
systemd:
name: nginx
state: reloaded
2、准备配置文件
[root@m01 ~]# vim www.qxl.com.conf.j2
upstream {{ server_name }} {
{% for n in range(21) %}
server 172.16.1.{{ n }}:{{ up_port }};
{% endfor %}
}
server {
listen 80;
server_name {{ server_name }};
location / {
root /code;
index index.html;
proxy_pass http://{{ server_name }};
proxy_set_header Host $http_host;
}
}
roles
1、Ansible Roles基本概述
roles不管是Ansible还是saltstack,我在写一键部署的时候,都不可能把所有的步骤全部写入到一个’剧本’文件当中,我们肯定需要把不同的工作模块,拆分开来,解耦,那么说到解耦,我们就需要用到roles官方推荐,因为roles的目录结构层次更加清晰。
例如:我们之前推荐大家写一个base.yml里面写所有基础优化的项目,其实把所有东西摞进去也是很鸡肋的,不如我们把这些功能全部拆分开,谁需要使用,就调用即可。
建议:每个roles最好只使用一个tasks这样方便我们去调用,能够很好的做到解耦。
2、Ansible Roles目录结构
1)官方推荐最佳实践目录结构定义方式
production # inventory file for production servers
staging # inventory file for staging environment
group_vars/
group1.yml # here we assign variables to particular groups
group2.yml
host_vars/
hostname1.yml # here we assign variables to particular systems
hostname2.yml
library/ # if any custom modules, put them here (optional)
module_utils/ # if any custom module_utils to support modules, put them here (optional)
filter_plugins/ # if any custom filter plugins, put them here (optional)
site.yml # master playbook
webservers.yml # playbook for webserver tier
dbservers.yml # playbook for dbserver tier
roles/
common/ # this hierarchy represents a "role"
tasks/ #
main.yml # <-- tasks file can include smaller files if warranted
handlers/ #
main.yml # <-- handlers file
templates/ # <-- files for use with the template resource
ntp.conf.j2 # <------- templates end in .j2
files/ #
bar.txt # <-- files for use with the copy resource
foo.sh # <-- script files for use with the script resource
vars/ #
main.yml # <-- variables associated with this role
defaults/ #
main.yml # <-- default lower priority variables for this role
meta/ #
main.yml # <-- role dependencies
library/ # roles can also include custom modules
module_utils/ # roles can also include custom module_utils
lookup_plugins/ # or other types of plugins, like lookup in this case
webtier/ # same kind of structure as "common" was above, done for the webtier role
monitoring/ # ""
fooapp/ # ""
2)roles目录结构使用galaxy创建
[root@m01 ~]# cd /etc/ansible/roles/
[root@m01 roles]# tree wordpress/
nfs/ #项目名称
├── defaults #低优先级变量
├── files #存放文件
├── handlers #触发器文件
├── meta #依赖关系文件
├── tasks #工作任务文件
├── templates #jinja2模板文件
├── tests #测试文件
└── vars #变量文件
3、Ansible Roles依赖关系
roles允许你再使用roles时自动引入其他的roles。role依赖关系存储在roles目录中meta/main.yml文件中。
例如:推送wordpress并解压,前提条件,必须要安装nginx和php,把服务跑起来,才能运行wordpress的页面,此时我们就可以在wordpress的roles中定义依赖nginx和php的roles
[root@m01 roles]# vim /etc/ansible/roles/wordpress/meta/main.yml
dependencies:
- { role: nginx }
- { role: php }
如果编写了meta目录下的main.yml文件,那么Ansible会自动先执行meta目录中main.yml文件中的dependencies文件,如上所示,就会先执行nginx和php的安装。
Ansible Roles最佳实践
使用roles重构rsync
1.规划目录结构如下
[root@m01 rsync]# cd /etc/ansible/roles/
[root@m01 roles]# ll
总用量 0
[root@m01 roles]# ansible-galaxy init rsync
- rsync was created successfully
[root@m01 roles]# tree
.
└── rsync
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
2.定义roles主机清单
[root@m01 roles]# cat /etc/ansible/roles/hosts
[backup]
172.16.1.41
3.指定backup主机组,执行那个roles
[root@m01 roles]# cat /etc/ansible/roles/site.yml
- hosts: backup
remote_user: root
roles:
- rsync
4.查看rsync角色的tasks任务
[root@m01 roles]# cat /etc/ansible/roles/rsync/tasks/main.yml
- name: Install Rsync Server
yum: name=rsync state=present
- name: Configure Rsync Server
copy:
src: {{ item.src }}
dest: /etc/{{ item.dest }}
mode: {{ item.mode }}
with_items:
- {src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644"}
- {src: "rsync.passwd", dest: "rsync.passwd", mode: "0600"}
notify: Restart Rsync Server
- name: Start Rsync Server
systemd:
name: rsyncd
state: started
enabled: yes
5.查看rsync角色的handlers
[root@m01 roles]# cat /etc/ansible/roles/rsync/handlers/main.yml
- name: Restart Rsync Server
service:
name: rsyncd
state: restarted
6.查看rsync角色的files目录
[root@m01 roles]# ll /etc/ansible/roles/rsync/files/
total 8
-rw-r--r-- 1 root root 322 Nov 16 18:49 rsyncd.conf
-rw------- 1 root root 20 Nov 16 18:30 rsync.passwd
7.执行roles,使用-t指定执行测试rsync角色
[root@m01 roles]# ansible-playbook -i hosts -t rsync site.yml
PLAY [backup] ********************************************************************************************
TASK [Gathering Facts] ********************************************************************************
ok: [172.16.1.41]
TASK [backup : Install Rsync Server] ***********************************************************************
ok: [172.16.1.41]
TASK [backup : Configure Rsync Server] *********************************************************************
ok: [172.16.1.41]
TASK [backup : Start Rsync Server] *************************************************************************
ok: [172.16.1.41]
PLAY RECAP ********************************************************************************************
172.16.1.41 : ok=5 changed=0 unreachable=0 failed=0