day16.5

day16.5

playbook handlers(触发器)

# 当修改完某个服务的配置文件时,应该重启服务
- hosts: web_group
  tasks:
  - name: 推送nginx配置文件
    copy:
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
    with_items:
      - {src: "/root/code/nginx.conf",dest: "/etc/nginx/nginx.conf"}
    notify: nginx_php

  handlers:
  - name: nginx_php
    service:
      name: nginx
      state: reloaded

handler注意点

1.无论多少个task调用相同hander,只会在所有tasks执行完成后,触发handles
2.handlers只有在其所在的任务被执行时,才会被运行,如果一个任务中定义了notify调用handiers,但是由于条件判断等原因,该任务未被执行,那么handdiers同样不会被执行
3.handlers只会在每一个play的末尾运行一次,如果想在一个playbook中间运行handlers,则需要使用mera模块来使用
4.如果一个play在运行到调用handlers的语句之前失败了,那么这个handlers将不会被执行。我们可以使用meta模块的-force-handlers选项来强制执行handlers所在play中途运行失败也能运行
5.不能使用handlers代替tasks

ansible任务标签

# 对一个task打一个tag
- name: 安装rsync
  yum:
    name: rsync
    state: present
  when: ansible_hostname != 'db01'
  tags: install_rsync
# 对一个task打多个tag
- name: 安装rsync
  yum:
    name: rsync
    state: present
  when: ansible_hostname != 'db01'
  tags: 
    - install_rsync
    - send_rsync_config

# 对多个task打一个tag
- name: 安装rsync
  yum:
    name: rsync
    state: present
  when: ansible_hostname != 'db01'
  tags: install_rsync
  
  - name: 启动rsync服务
	service:
	  name: rsyncd
	  state: started
	  enabled: True
	when: ansible_hostname == 'backup'
	tags: install_rsync

使用标签

-t: 执行指定的tag标签任务
--skip-tags:执行--skip-tags之外的标签任务

ansible-playbook -i base/hosts lnmp_wp.yml -t 'install_rsync'
ansible-playbook -i base/hosts lnmp_wp.yml --skip-tags 'install_rsync'

playbook的复用

在需要重启的配置文件里加入notify并定义它的变量
- name: 推送配置文件
    copy:
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
    with_items:
      - {src: "/code/nginx.conf",dest: "/etc/nginx/nginx.conf"}
      - {src: "/code/wordpress.conf",dest: "/etc/nginx/conf.d"}
      - {src: "/code/www.conf",dest: "/etc/php-fpm.d/"}
    notify: nginx
    when: ansible_hostname is match 'web*'
在文件最后写他的配置文件
 handlers:
    - name: rsync
      service:
        name: rsync
        state: restarted
        
    - name: nginx
      service:
        name: rsync
        state: restarted

facts缓存

缓存中,记录主机自带的变量,直接调用即可

缓存加载时间:在play执行的时候,会将变量内容放到内存中

缓存清除时间:当该play执行完所有task之后,会将内存中的变量全部删除

## 主机名
ansible_hostname // 显示第一个.之前的主机名
ansible_fqdn // 显示完整的主机名
## 内存
ansible_memtotal_mb // 总内存
ansible_memfree_mb // 空闲内存
ansible_swaptotal_mb // 总虚拟内存
ansible_swapfree_mb // 空闲虚拟内存
## CPU
ansible_processor_cores // cpu核心数
## 系统
ansible_os_family 	// 系统类型 RedHat Debain
ansible_distribution 	// 系统发行版 CentOS
ansible_distribution_major_version 	// 版本号 7
ansible_distribution_version 	// 详细版本号 7.6
## IP相关
ansible_dns.nameservers // DNS
ansible_default_ipv4.address // eth0外网IP
ansible_eth0.ipv4.address // eth0外网IP
ansible_eth1.ipv4.address // eth1内网IP
## 磁盘
ansible_devices.sda.partitions.sda1.size // sda1分区的磁盘大小:/boot分区
ansible_devices.sda.partitions.sda3.size // sda3分区的磁盘大小: /分区

相关文章

----name:setpublickeyonremotehosts&setreomtehostssud...
环境准备#cat/etcedhat-releaseCentOSLinuxrelease7.9.2009(...
准备好环境,在安装之前请先了解openshift提供的ansible有大...
Ansible:运维工作:系统安装(物理机、虚拟机)-->程序包...
ansible与salt对比相同都是为了同时在多台机器上执行相同的命...
[root@node1playbook]#catnginx.yml-hosts:test\\主...