hualinux 进阶 2-1.12:ansible 剧本Playbooks(九) 模板template

 目录

 一、说明

1.1 template模板格式

1.2 template模板语法

二、例子

2.1 编写模板

2.2 添加变量名

2.3 修改task相关内容

2.3.1 修改siteFiles.yml

2.3.2 修改配置文件conf.yml 

2.4 执行playbook看一下效果

附录一、修改后新的目录结构及配置文件

1.目录结构

2. 相关任务tasks

3 涉及的变量vars

4.涉及的handler

5. 涉及的配置模板templates

6. 涉及的文件

7. playbook配置文件


Ansible 是借由 Jinja2 来写作 template 系统,所以请使用 *.j2 格式 在模板中我们就可以使用前篇讲的Jinja2的东西了。

 一、说明

1.1 template模板格式

Ansible 是借由 Jinja2 来写作 template 系统,所以请使用 *.j2 格式

在模板中我们就可以使用上面讲的Jinja2的东西了。

在playbook用“template”来定义模板,具体用法可以使用文档说明帮助

ansible-doc template

1.2 template模板语法

一般格式为:

template:src=/Jinja2模板路径/xxx.j2 dest=/目录路径/名字 [options]

下面是一般选项说明

选项

必要

说明

backup

no

建立个包括timestamp在内的文件备份,以备不时之需.

dest

yes

远程节点上的绝对路径,用于放置template文件。

group

no

设置远程节点上的的template文件的所属用户组

mode

no

设置远程节点上的template文件权限。类似Linux中chmod的用法

owner

no

设置远程节点上的template文件所属用户

src

yes

本地Jinjia2模版的template文件位置。

Ansible 很容易的传输文件到远端系统上面,但是它经常需要替换一些变量在其它的文件里面。变量可以来自 清单文件,Host Vars Group Vars,或者 FactsTemplates 使用 Jinja2 模板引擎同样可以包含逻辑控制像循环和 if 语句。

我们只需事先定义变数和模板 (Templates),即可用它动态产生远端的 Shell Scripts、设定档 (Configure) 等。换句话说,我们可以用一份 template 来产生开发 (Development)、测试 (Test) 和正式环境 (Production) 等不同的环境设定。

 

二、例子

我在《ansible 剧本Playbooks(六)角色roles》文章中,以nginx为角色,配置了nginx自动化安装,但是模板部分并没有使用,这章在此基础上补上。

其实nginx配置修改的就是那几个地方,域名、网站目录路径、日志名,暂时就修改三处吧

为了看到效果我把hua主机组和vm821安装的nginx卸载,网站目录删除,再从0弄一下,我就直接基于《ansible 剧本Playbooks(六)角色roles》例子的nginx角色基础上进行修改即可

2.1 编写模板

把这前nginx角色的配置文件移到模板中,并修改后缀为.j2

cd /etc/ansible/
mv roles/nginx/files/default.conf roles/nginx/templates/default.conf.j2

对 roles/nginx/templates/default.conf.j2 模板文件进行修改如下:我只修改粉色部分

[root@vm82 ansible]# cat roles/nginx/templates/default.conf.j2
server {
    listen       80;
    server_name  {{ server_name }};

    #charset koi8-r;
    access_log  /var/log/nginx/{{ log_name }}.access.log  main;

    location / {
        root   {{ site_dir }};
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

2.2 添加变量名

#在角色中添加2个变量名 server_name  site_dir log_name
[root@vm82 ansible]# cat roles/nginx/vars/main.yml 
install_dir: /disk1/tools
# 网站配置名
nginx_conf_name: hualinux
# 域名
server_name: "hualinux.com www.hualinux.com"
# 日志名
log_name: hualinux.com
# 网站目录
site_dir: /disk1/www/hualinux.com

2.3 修改task相关内容

2.3.1 修改siteFiles.yml

因为我这里使用变量了,所以也要采用变量的方式

[root@vm82 ansible]# cat roles/nginx/tasks/siteFiles.yml 
# 创建目录并把权限修改为nginx,yum安装默认用户是nginx
- name: create site dir
  file: 
    path: "{{ site_dir }}"
    state: directory
    recurse: yes
    owner: nginx
    group: nginx
- copy: 
    src: /etc/ansible/roles/nginx/files/index.html
    dest: "{{ site_dir }}"

2.3.2 修改配置文件conf.yml 

之前是使用复制的方式,我这里不使用复制,使用模板了

[root@vm82 ansible]# cat roles/nginx/tasks/conf.yml 
# 使用模板,以域名为配置名
- name: nginx configure file
  template: 
    src: /etc/ansible/roles/nginx/templates/default.conf.j2
    dest: /etc/nginx/conf.d/{{ nginx_conf_name }}.conf 
  notify: restart nginx

2.4 执行playbook看一下效果

所有的都修改完成了,修改完之后的目录结构和配置文件内容,可以看附录一、修改后新的目录结构及配置文件,接下来执行一下playbooks就行了

[root@vm82 ansible]# ansible-playbook yaml-conf/nginx_roles.yml

PLAY [hua,h1] *******************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [192.168.3.76]
ok: [192.168.3.21]

TASK [nginx : create /disk1/tools] **********************************************************************************
ok: [192.168.3.76]
ok: [192.168.3.21]

TASK [nginx : copy] *************************************************************************************************
skipping: [192.168.3.21]
changed: [192.168.3.76]

TASK [nginx : copy] *************************************************************************************************
skipping: [192.168.3.76]
ok: [192.168.3.21]

TASK [yum install nginx on centos7] *********************************************************************************
skipping: [192.168.3.21]
changed: [192.168.3.76]

TASK [yum install nginx on centos8] *********************************************************************************
skipping: [192.168.3.76]
changed: [192.168.3.21]

TASK [nginx : create site dir] **************************************************************************************
changed: [192.168.3.76]
changed: [192.168.3.21]

TASK [nginx : copy] *************************************************************************************************
changed: [192.168.3.76]
changed: [192.168.3.21]

TASK [nginx configure file] *****************************************************************************************
changed: [192.168.3.76]
changed: [192.168.3.21]

TASK [restart nginx] ************************************************************************************************
changed: [192.168.3.76]
changed: [192.168.3.21]

RUNNING HANDLER [start nginx] ***************************************************************************************
changed: [192.168.3.76]
changed: [192.168.3.21]

PLAY RECAP **********************************************************************************************************
192.168.3.21               : ok=9    changed=6    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
192.168.3.76               : ok=9    changed=7    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0 

我在本的的win上绑定打开hosts文件,目录位置 C:\Windows\System32\drivers\etc\hosts 做如下绑定

#文件路径 C:\Windows\System32\drivers\etc\hosts
#添加如下条目并保存生效
192.168.3.21 www.hualinux.com hualinux.com

打开浏览器,输入 www.hualinux.com看一下效果

 

附录一、修改后新的目录结构及配置文件

1.目录结构

[root@vm82 ansible]# pwd
/etc/ansible
[root@vm82 ansible]# tree roles/nginx
roles/nginx
├── default
├── files
│   ├── index.html
│   ├── nginx-1.18.0-1.el7.ngx.x86_64.rpm
│   └── nginx-1.18.0-1.el8.ngx.x86_64.rpm
├── handlers
│   └── main.yml
├── meta
├── tasks
│   ├── conf.yml
│   ├── install.yml
│   ├── main.yml
│   └── siteFiles.yml
├── templates
│   └── default.conf.j2
└── vars
    └── main.yml

2. 相关任务tasks

[root@vm82 ansible]# cat roles/nginx/tasks/main.yml 
# 复制文件文件,如果对应的目录不存在此ansible版本会自动创建
# 如果不自动创建的请手工创建一下/disk1/www/hualinux.com
#
# 1.复制对应的rpm并使用yum安装rpm
- include: install.yml
# 2.复制网站文件
- include: siteFiles.yml
# 3.复制配置文件并重启
- include: conf.yml
[root@vm82 ansible]# cat roles/nginx/tasks/install.yml 
# 创建放软件的目录
- name: create {{ install_dir }}
  file:
    path: "{{ install_dir }}"
    state: directory
    recurse: yes
    owner: root
    group: root

# when和模块是同级的,判断系统版本及主版本
# 判断是否来centos7
- copy:
    src: /etc/ansible/roles/nginx/files/nginx-1.18.0-1.el7.ngx.x86_64.rpm
    dest: "{{ install_dir }}"
  when: (ansible_distribution=="CentOS") and (ansible_distribution_major_version=="7")
# 判断是否为centos8
- copy:
    src: /etc/ansible/roles/nginx/files/nginx-1.18.0-1.el8.ngx.x86_64.rpm
    dest: "{{ install_dir }}"
  when: (ansible_distribution=="CentOS") and (ansible_distribution_major_version=="8")

# yum安装
- name: yum install nginx on centos7
  yum: 
    name: /disk1/tools/nginx-1.18.0-1.el7.ngx.x86_64.rpm
    state: present
  notify: start nginx  
  when: (ansible_distribution=="CentOS") and (ansible_distribution_major_version=="7")

- name: yum install nginx on centos8
  yum:
    name: /disk1/tools/nginx-1.18.0-1.el8.ngx.x86_64.rpm
    state: present
  notify: start nginx
  when: (ansible_distribution=="CentOS") and (ansible_distribution_major_version=="8")
[root@vm82 ansible]# cat roles/nginx/tasks/siteFiles.yml 
# 创建目录并把权限修改为nginx,yum安装默认用户是nginx
- name: create site dir
  file: 
    path: "{{ site_dir }}"
    state: directory
    recurse: yes
    owner: nginx
    group: nginx
- copy: 
    src: /etc/ansible/roles/nginx/files/index.html
    dest: "{{ site_dir }}"
[root@vm82 ansible]# cat roles/nginx/tasks/conf.yml 
# 使用模板,以域名为配置名
- name: nginx configure file
  template: 
    src: /etc/ansible/roles/nginx/templates/default.conf.j2
    dest: /etc/nginx/conf.d/{{ nginx_conf_name }}.conf
- name: restart nginx
  service: 
    name: nginx
    state: restarted 

3 涉及的变量vars

[root@vm82 ansible]# cat roles/nginx/vars/main.yml 
install_dir: /disk1/tools
# 网站配置名
nginx_conf_name: hualinux
# 域名
server_name: "hualinux.com www.hualinux.com"
# 日志名
log_name: hualinux.com
# 网站目录
site_dir: /disk1/www/hualinux.com

4.涉及的handler

[root@vm82 ansible]# cat roles/nginx/handlers/main.yml 
# 暂时没有调用到这个重启服务
- name: restart nginx
  service: 
    name: nginx
    state: restarted

- name: start nginx
  service:
    name: nginx
    state: started
    enabled: yes

5. 涉及的配置模板templates

[root@vm82 ansible]# cat roles/nginx/templates/default.conf.j2 
server {
    listen       80;
    server_name  {{ server_name }};

    #charset koi8-r;
    access_log  /var/log/nginx/{{ log_name }}.access.log  main;

    location / {
        root   {{ site_dir }};
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

6. 涉及的文件

[root@vm82 ansible]# ll roles/nginx/files/
总用量 1584
-rw-r--r-- 1 root root     29 9月  11 21:20 index.html
-rw-r--r-- 1 root root 790284 4月  21 23:19 nginx-1.18.0-1.el7.ngx.x86_64.rpm
-rw-r--r-- 1 root root 825436 9月  11 21:12 nginx-1.18.0-1.el8.ngx.x86_64.rpm

#index文件里的内容
[root@vm82 ansible]# cat roles/nginx/files/index.html 
<h2>Welcome to hualinux</h2>

7. playbook配置文件

#playbooks配置文件内容
[root@vm82 ansible]# cat yaml-conf/nginx_roles.yml 
---
- hosts:
  - hua
  - h1
  roles:
  - nginx

#ansible hosts配置文件内容
[root@vm82 ansible]# egrep -v '^$|^#' hosts
[hua]
192.168.3.21  http_port=80
[h1]
192.168.3.76

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值