07.ansible实现template管理nginx实战

1.在playbook中去定义变量

变量的优先级 -e > playbook > 主机清单
普通变量比分组里面的公共变量优先级高

1.1使用变量查看主机名

[root@ansible ansible]# ansible all -m setup -a 'filter="ansible_fqdn"'
10.0.0.50 | SUCCESS => {
    "ansible_facts": {
        "ansible_fqdn": "c7-50.shared",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
10.0.0.49 | SUCCESS => {
    "ansible_facts": {
        "ansible_fqdn": "web82.example.com",
        "discovered_interpreter_python": "/usr/bin/python"
    },
---
    "changed": false
}
10.0.0.48 | SUCCESS => {
    "ansible_facts": {
        "ansible_fqdn": "web81.example.com",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}

1.2新建playbook


[root@ansible ansible]# cat var.yml
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: create log file
      file: name=/data/{{ ansible_fqdn }}.log state=touch  #直接只是setup的变量

1.3启动

[root@ansible ansible]# ansible-playbook var.yml

1.4验证


[root@ansible ~]# ansible all -a 'ls /data -l'
10.0.0.49 | CHANGED | rc=0 >>
总用量 0
-rw-r--r-- 1 root root 0 8月  20 05:41 web82.example.com.log
10.0.0.48 | CHANGED | rc=0 >>
总用量 0
-rw-r--r-- 1 root root 0 8月  20 05:41 web81.example.com.log
10.0.0.50 | CHANGED | rc=0 >>
总用量 0

2.在独立的变量YAML文件中定义

2.1新建变量文件

[root@ansible ansible]# cat vars.yml
var1: httpd
var2: vsftpd

2.2新建playbook剧本

[root@ansible ansible]# cat testvar.yml
---
- hosts: webserver
  remote_user: root
  vars_files:   #定义变量文件
    - vars.yml   #文件名
  tasks:
    - name: install package
      yum: name={{ var1 }}
    - name: cretate file
      file: name=/data/{{ var2 }}.log state=touch

2.3执行


[root@ansible ansible]# ansible-playbook testvar.yml

2.4验证

#验证
[root@ansible ansible]# ansible webserver -m shell -a 'rpm -q httpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running
'rpm'.  If you need to use command because yum, dnf or zypper is insufficient
you can add 'warn: false' to this command task or set 'command_warnings=False'
in ansible.cfg to get rid of this message.
10.0.0.48 | CHANGED | rc=0 >>
httpd-2.4.6-93.el7.centos.x86_64
10.0.0.49 | CHANGED | rc=0 >>
httpd-2.4.6-93.el7.centos.x86_64
[root@ansible ansible]# ansible webserver -m shell -a 'ls /data'
10.0.0.48 | CHANGED | rc=0 >>
vsftpd.log
10.0.0.49 | CHANGED | rc=0 >>
vsftpd.log

3.模版templates

  • 只用于playbook
[root@ansible ansible]# ansible-doc -s template
- name: Template a file out to a remote server
#拿一个文件当模板传到远程主机上去
  • 文本文件,嵌套有脚本(使用模板编程语言编写)
  • Jinja2语言,使用字面量,有下面格式
    字符串:使用单引号或双引号
    数字: 整数,浮点数
    列表:[iterm1,iterm2,…]
    元组:(iterm1,iterm2,…)
    字典:{key1:value1,key2:value2,…}
    布尔型:true/false
  • 算术匀运算:+,-,/,//,%,**
  • 比较操作:==,!=,>,>=,<,<=
  • 逻辑运算:and,or,not
  • 流表达式:For If When

3.1安装nginx

[root@ansible ~]# yum -y install nginx

3.2查看配置文件

root@ansible ~]# cat /etc/nginx/nginx.conf
worker_processes auto;  #根据CPU核数自动生成work进程数

3.3启动nginx

[root@ansible ~]# systemctl start nginx

3.4查看进程数

[root@ansible ~]# ps aux | grep nginx
root     22038  0.0  0.2 120900  2096 ?        Ss   21:21   0:00 nginx: master process /usr/sbin/nginx
nginx    22039  0.0  0.3 121296  3052 ?        S    21:21   0:00 nginx: worker process   #只有一个进程表示一个CPU
root     22041  0.0  0.0 112824   980 pts/2    S+   21:21   0:00 grep --color=auto nginx

3.5查看CPU

[root@ansible ~]# lscpu  #查看CPU 只有一个
CPU(s):                1

ansible_processor_vcpus": 1

3.6定义进程数

[root@ansible templates]# cat nginx.conf.j2
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes {{ansible_processor_vcpus*4}};  #使用变量*4

3.7编辑playbook

[root@ansible templates]# cat templat.yml
---
- hosts: webserver
  remote_user: root
  tasks:
    - name: install package
      yum: name=nginx
    - name: copy template
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf #使用template模块 且只能在playbook中使用
      notify: restart service
    - name: start service
      service: name=nginx state=started enabled=yes
  handlers:
    - name: restart service
      service: name=nginx state=restarted

3.8执行

[root@ansible templates]# ansible-playbook templat.yml

3.9查看进程数

#因为是1核所以*4是4个进程
[root@ansible templates]# ansible webserver -m shell -a 'ps aux| grep nginx'
10.0.0.49 | CHANGED | rc=0 >>
root      7061  0.0  0.2 120900  2244 ?        Ss   01:10   0:00 nginx: master process /usr/sbin/nginx
nginx     7063  0.0  0.3 121296  3256 ?        S    01:10   0:00 nginx: worker process
nginx     7064  0.0  0.3 121296  3256 ?        S    01:10   0:00 nginx: worker process
nginx     7065  0.0  0.3 121296  3256 ?        S    01:10   0:00 nginx: worker process
nginx     7066  0.0  0.3 121296  3052 ?        S    01:10   0:00 nginx: worker process
root      7215  0.0  0.1 113280  1204 pts/1    S+   01:10   0:00 /bin/sh -c ps aux| grep nginx
root      7217  0.0  0.0 112828   960 pts/1    S+   01:10   0:00 grep nginx
10.0.0.48 | CHANGED | rc=0 >>
root      7445  0.0  0.2 120900  2244 ?        Ss   01:10   0:00 nginx: master process /usr/sbin/nginx
nginx     7447  0.0  0.3 121296  3052 ?        S    01:10   0:00 nginx: worker process
nginx     7448  0.0  0.3 121296  3052 ?        S    01:10   0:00 nginx: worker process
nginx     7449  0.0  0.3 121296  3052 ?        S    01:10   0:00 nginx: worker process
nginx     7450  0.0  0.3 121296  3052 ?        S    01:10   0:00 nginx: worker process
root      7580  0.0  0.1 113280  1204 pts/1    S+   01:10   0:00 /bin/sh -c ps aux| grep nginx
root      7582  0.0  0.0 113280   188 pts/1    R+   01:10   0:00 /bin/sh -c ps aux| grep nginx

4.0更改端口

4.0.1使用主机清单的变量
[root@ansible ~]# cat /etc/ansible/hosts
[webserver]
10.0.0.48 http_port=81
10.0.0.49 http_port=82
4.0.2修改配置文件
[root@ansible ansible]# cd templates/
[root@ansible templates]# cat nginx.conf.j2
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes {{ ansible_processor_vcpus*4 }};  #将auto改成变量

    server {
        listen       {{ http_port }} default_server;  #将80改成变量
4.0.3playbook
[root@ansible templates]# cat template.yml
---
- hosts: webserver
  remote_user: root
  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
4.0.4验证
#更改为4个进程
[root@ansible templates]# ansible webserver -m shell -a 'ps aux| grep nginx'
10.0.0.49 | CHANGED | rc=0 >>
root      9665  0.0  0.2 120900  2244 ?        Ss   01:48   0:00 nginx: master process /usr/sbin/nginx
nginx     9667  0.0  0.3 121296  3256 ?        S    01:48   0:00 nginx: worker process
nginx     9668  0.0  0.3 121296  3256 ?        S    01:48   0:00 nginx: worker process
nginx     9669  0.0  0.3 121296  3256 ?        S    01:48   0:00 nginx: worker process
nginx     9670  0.0  0.3 121296  3048 ?        S    01:48   0:00 nginx: worker process
root      9800  0.0  0.1 113280  1204 pts/1    S+   01:48   0:00 /bin/sh -c ps aux| grep nginx
root      9802  0.0  0.0 112824   960 pts/1    R+   01:48   0:00 grep nginx
10.0.0.48 | CHANGED | rc=0 >>
root     10378  0.0  0.2 120900  2248 ?        Ss   01:48   0:00 nginx: master process /usr/sbin/nginx
nginx    10380  0.0  0.3 121296  3260 ?        S    01:48   0:00 nginx: worker process
nginx    10381  0.0  0.3 121296  3260 ?        S    01:48   0:00 nginx: worker process
nginx    10382  0.0  0.3 121296  3260 ?        S    01:48   0:00 nginx: worker process
nginx    10383  0.0  0.3 121296  3056 ?        S    01:48   0:00 nginx: worker process
root     10513  0.0  0.1 113280  1204 pts/1    S+   01:48   0:00 /bin/sh -c ps aux| grep nginx
root     10515  0.0  0.0 112824   960 pts/1    S+   01:48   0:00 grep nginx
[root@ansible templates]# ansible webserver -m shell -a 'ss -ntlp | grep nginx'
10.0.0.49 | CHANGED | rc=0 >>
LISTEN     0      128          *:82#已更改                       *:*                   users:(("nginx",pid=9670,fd=6),("nginx",pid=9669,fd=6),("nginx",pid=9668,fd=6),("nginx",pid=9667,fd=6),("nginx",pid=9665,fd=6))
LISTEN     0      128       [::]:80                    [::]:*                   users:(("nginx",pid=9670,fd=7),("nginx",pid=9669,fd=7),("nginx",pid=9668,fd=7),("nginx",pid=9667,fd=7),("nginx",pid=9665,fd=7))
10.0.0.48 | CHANGED | rc=0 >>
LISTEN     0      128          *:81  #已更改                     *:*                   users:(("nginx",pid=10383,fd=6),("nginx",pid=10382,fd=6),("nginx",pid=10381,fd=6),("nginx",pid=10380,fd=6),("nginx",pid=10378,fd=6))
LISTEN     0      128       [::]:80                    [::]:*                   users:(("nginx",pid=10383,fd=7),("nginx",pid=10382,fd=7),("nginx",pid=10381,fd=7),("nginx",pid=10380,fd=7),("nginx",pid=10378,fd=7))
[root@ansible templates]#

when

  • 条件测试:如果需要根据变量、facts或之前任务的执行结果来做为某task执行与否的前提时要用到条件测试,通过when语句实现,在task中使用,Jinja2的语法格式
  • when语句
  • 在task后添加when子句即可使用条件测试;when语句支持Jinja2表达式语法
  • 示例:
  • tasks:
    • name: “shutdown RedHat flavored systems”
      command: /sbin/shutdown -h now
      when: ansible_os_family == “RedHat”

根据版本号 来推送不同的模板文件

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云原生解决方案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值