模板--templates

学习目标:

提示:这里可以添加学习目标

学习内容:

模板templates

在这里插入图片描述
在这里插入图片描述
templates功能
在这里插入图片描述
利用template模板安装nginx样例

[root@zabbix_server ansible]# tail -9 /etc/ansible/hosts
[LYP]
#172.28.102.130 ansible_user=root ansible_ssh_pass=Cm146688!
#172.28.102.131 ansible_user=root ansible_ssh_pass=Cm146688!
172.28.102.130 http_port=81  #变量的优先级:命令行>playbook>普通变量>公共组变量
172.28.102.131 http_port=82

[root@zabbix_server ansible]# grep -E  '^worker_processes' templates/nginx.conf.j2 
worker_processes {{ ansible_processor_vcpus*2 }}

[root@zabbix_server ansible]# grep listen templates/nginx.conf.j2 
        listen       {{ http_port }};
        listen       [::]:{{ http_port }};

[root@zabbix_server ansible]# cat templatenginx.yml 
---
- hosts: LYP
  remote_user: root
  vars:
   - http_port: 9090

  tasks:
   - name: install package
     yum: name=nginx
   - name: copy template
     template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
     notify: restart service
   - name: start service
     service: name=nginx state=started enabled=yes

  handlers:
   - name: restart service
     service: name=nginx state=restarted

[root@zabbix_server ansible]# ansible-playbook templatenginx.yml 

PLAY [LYP] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [172.28.102.130]
ok: [172.28.102.131]

TASK [install package] **************************************************************************************************************************************************************************************
ok: [172.28.102.130]
ok: [172.28.102.131]

TASK [copy template] ****************************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]

TASK [start service] ****************************************************************************************************************************************************************************************
ok: [172.28.102.130]
ok: [172.28.102.131]

RUNNING HANDLER [restart service] ***************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]

PLAY RECAP **************************************************************************************************************************************************************************************************
172.28.102.130             : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.28.102.131             : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@zabbix_server ansible]# ansible LYP -m shell -a 'netstat -luntp|grep nginx'
172.28.102.131 | CHANGED | rc=0 >>
tcp        0      0 0.0.0.0:9090            0.0.0.0:*               LISTEN      22445/nginx: master 
tcp6       0      0 :::9090                 :::*                    LISTEN      22445/nginx: master 
172.28.102.130 | CHANGED | rc=0 >>
tcp        0      0 0.0.0.0:9090            0.0.0.0:*               LISTEN      29522/nginx: master 
tcp6       0      0 :::9090                 :::*                    LISTEN      29522/nginx: master

[root@zabbix_server ansible]# ansible LYP -m shell -a 'ps -ef |grep nginx'
172.28.102.130 | CHANGED | rc=0 >>
root     29522     1  0 17:30 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx    29525 29522  0 17:30 ?        00:00:00 nginx: worker process
nginx    29526 29522  0 17:30 ?        00:00:00 nginx: worker process
root     29697 29696  0 17:38 pts/1    00:00:00 /bin/sh -c ps -ef |grep nginx
root     29699 29697  0 17:38 pts/1    00:00:00 /bin/sh -c ps -ef |grep nginx
172.28.102.131 | CHANGED | rc=0 >>
root     22445     1  0 17:30 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx    22446 22445  0 17:30 ?        00:00:00 nginx: worker process
nginx    22447 22445  0 17:30 ?        00:00:00 nginx: worker process
root     22973 22972  0 17:38 pts/1    00:00:00 /bin/sh -c ps -ef |grep nginx
root     22975 22973  0 17:38 pts/1    00:00:00 grep nginx

when

  • 条件测试,如果需要根据变量,facts或此前任务的执行结果来做为莫task执行与否的前提时要用到条件测试,通过when语句实现,在task使用,jinja2的语法格式:
  • when语句
  • 在task后添加when子句即可实现条件测试,when语句支持jinja2表达式语法
  • 示例:
    在这里插入图片描述
    样例
[root@zabbix_server ansible]# cat templatenginx.yml 
---
- hosts: LYP
  remote_user: root
  vars:
#   - http_port: 9090

  tasks:
   - name: install package
     yum: name=nginx
   - name: copy template6
     template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf
     when: ansible_hostname == "zabbix_agent2"
     notify: restart service
   - name: copy template7
     template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf
     when: ansible_hostname == "zabbix_proxy"
     notify: restart service
   - name: start service
     service: name=nginx state=started enabled=yes

  handlers:
   - name: restart service
     service: name=nginx state=restarted

[root@zabbix_server ansible]# ll templates/
total 8
-rw-r--r-- 1 root root 2389 Apr 30 17:16 nginx.conf6.j2
-rw-r--r-- 1 root root 2389 Apr 30 18:09 nginx.conf7.j2

[root@zabbix_server ansible]# ansible LYP -m shell -a 'netstat -luntp |grep nginx'
172.28.102.130 | CHANGED | rc=0 >>
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      30869/nginx: master 
tcp6       0      0 :::81                   :::*                    LISTEN      30869/nginx: master 
172.28.102.131 | CHANGED | rc=0 >>
tcp        0      0 0.0.0.0:82              0.0.0.0:*               LISTEN      25965/nginx: master 
tcp6       0      0 :::82                   :::*                    LISTEN      25965/nginx: master


学习产出:

提示:这里统计学习计划的总量

例如:

  • 技术笔记 2 遍
  • CSDN 技术博客 3 篇
  • 习的 vlog 视频 1 个
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值