什么是playbook剧本
Ansible剧本: ⼀系列的任务按照我们期望的结果编排在⼀起 playbook组成: hosts: 定义主机⻆⾊ tasks: 具体执⾏的任务 简单理解:不同的模块去完成⼀件事
格式 两个空格
需要执⾏的主机:nfs 任务: - 任务1: 创建⽤户 动作:创建⽤户的命令 - 任务2: 创建⽬录 动作:创建⽬录的命令
playbook剧本的优势
减少重复命令的书写:ansible backup -m 简洁清晰好理解 功能强⼤,可以控制流程,⽐如:判断,循环,变量,标签 可以复⽤ 提供语法检查以及模拟执⾏
剧本的格式书写要求
YAML格式特点
严格的缩进表示层级关系 不要使⽤tab缩进 : 后⾯⼀定要有空格 - 后⾯⼀定要有空格 ⽂件后缀名需要改为yaml或yml,vim可以智能⾼亮提示
剧本的组成
hosts: 需要执⾏的主机 tasks: 需要执⾏的任务 name: 任务名称
编写rsync剧本
- 添加组
- 添加用户
- 傀儡用户
- 安装rsync
- 复制配置文件
- 创建认证
- 创建备份目录
- 启动服务
hosts: 10.0.1.7 tasks: - name: add group group: name: rsync gid: 666 - name: add user user: name: rsync shell: /sbin/nologin create_home: no group: rsync uid: 666 - name: install rsync yum: name: rsync state: latest - name: copy rsyncd copy: src: /tmp/rsyncd.conf dest: /etc/ - name: touch password file: path: /etc/rsync.passwd state: touch - name: vim password copy: content: 'rsync_backup:123456' dest: /etc/rsync.passwd mode: 0600 - name: create backup file: path: /backup state: directory owner: rsync group: rsync - name: systemctl rsync systemd: name: rsyncd state: started enabled: yes
模拟执行
ansible-playbook -C rsync_install.yaml
执行
ansible-playbook rsync_install.yaml
编写nfs剧本
- hosts: 10.0.1.8 tasks: - name: add group group: name: www gid: 666 - name: add user user: name: www uid: 666 create_home: no shell: /sbin/nologin group: www - name: install nfs yum: name: nfs-utils state: latest - name: install rpcbind yum : name: rpcbind state: latest - name: copy exports copy: src: /tmp/exports dest: /etc/ - name: create data file: path: /data state: directory owner: www group: www - name: systemd rpcbind systemd: name: rpcbind state: started enabled: yes - name: systemd nfs systemd: name: nfs state: started enabled: yes
剧本⾼级特性-循环
官方文档
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html?highlight=loop
应用场景
安装多个软件 创建多个⽬录 复制多个⽬录 复制多个⽂件到不同的⽬录 不同的⽂件权限不⼀样
循环书写风格:
单行模式:
- name: create_data file: path=/data state=directory owner=www group=www
多行模式:
name: create_data file: path: "{{ item }}" state: directory owner: www group: www loop: - /data - /backup
混合模式:
- name: create_data file: path="{{ item }}" state=directory owner=www group=www loop: - /data - /backup
多参数循环模式:
- hosts: backup tasks: - name: create_data file: path: "{{ item.path }}" state: directory owner: www group: www mode: "{{ item.mode }}" loop: - { path: '/data' , mode: '755' } - { path: '/backup', mode: '777' }
剧本⾼级特性-变量
应用场景
自定义某个变量,在任务中被多次引⽤ 从主机收集到系统信息⾥提取某个变量,⽐如IP地址,主机名
⾃定义变量并引⽤
- hosts: backup vars: user_id: '666' rsync_user: 'www' tasks: #1.创建www组和www⽤户 - name: create_group group: name: "{{ rsync_user }}" gid: "{{ user_id }}" #2.创建www⽤户 - name: create_user user: name: "{{ rsync_user }}" uid: "{{ user_id }}" group: "{{ rsync_user }}" create_home: no shell: /sbin/nologin #3.创建数据⽬录并更改授权 - name: create_data file: path: "{{ item }}" state: directory owner: "{{ rsync_user }}" group: "{{ rsync_user }}" mode: '755' loop: - /data/ - /backup/ #4.安装rsync软件 - name: install_rsync yum: name: rsync state: latest #5.复制配置⽂件和密码⽂件 - name: copy pwd&conf copy: src: "{{ item.src }}" dest: /etc/ mode: "{{ item.mode }}" loop: - { src: /root/script/rsync/rsyncd.conf, mode: '644'} - { src: /root/script/rsync/rsync.passwd, mode: '600'} #6.启动服务 - name: start systemd: name: rsyncd state: started enabled: yes
使⽤变量获取主机的eth1地址和主机名
- hosts: all tasks: - name: echo IP shell: "echo {{ ansible_default_ipv4.address }} >> /tmp/ip.txt" - name: echo hostname shell: "echo {{ ansible_hostname }} >> /tmp/hostname.txt"
在主机清单中里定义变量
[root@m01 ~]# cat /etc/ansible/hosts [web] 172.16.1.7 [web:vars] service_name=web [nfs] 172.16.1.31 service_name=nfs [backup] 172.16.1.41 service_name=rsync [all:vars] job=it
引⽤变量
- host: all tasks: - name: echo ip shell: "echo {{ service_name }} >>/tmp/service.txt" - name: echo hostname shell: "echo {{ job }} >>/tmp/hostname.txt"
内置变量
ansible_facts.eth0.ipv4.address ip地址 ansible_facts.eth1.ipv4.address ip地址 ansible_nodename 节点名字 ansible_form_factor 服务器类型 ansible_virtualization_role 虚拟机⻆⾊(宿主机或者虚拟机) ansible_virtualization_type 虚拟机类型(kvm) ansible_system_vendor 供应商(Dell) ansible_product_name 产品型号(PowerEdge R530) ansible_product_serial 序列号(sn) ansible_machine 计算机架构(x86_64) ansible_bios_version BIOS版本 ansible_system 操作系统类型(linux) ansible_os_family 操作系统家族(RedHat) ansible_distribution 操作系统发⾏版(CentOS) ansible_distribution_major_version 操作系统发⾏版主版本号(7) ansible_distribution_release 操作系统发⾏版代号(core) ansible_distribution_version 操作系统发⾏版本号(7.3.1611) ansible_architecture 体系(x86_64) ansible_kernel 操作系统内核版本号 ansible_userspace_architecture ⽤户模式体系(x86_64) ansible_userspace_bits ⽤户模式位数 ansible_pkg_mgr 软件包管理器 ansible_selinux.status selinux状态 ansible_processor CPU产品名称 ansible_processor_count CPU数量 ansible_processor_cores 单颗CPU核⼼数量 ansible_processor_threads_per_core 每个核⼼线程数量 ansible_processor_vcpus CPU核⼼总数 ansible_memtotal_mb 内存空间 ansible_swaptotal_mb 交换空间 ansible_fqdn 主机的域名 ansible_default_ipv4.interface 默认⽹卡 ansible_default_ipv4.address 默认IP地址 ansible_default_ipv4.gateway 默认⽹关 ansible_devices 硬盘设备名 ansible_devices.vendor 硬盘供应商 ansible_devices.model 硬盘整列卡型号 ansible_devices.host 硬盘整列卡控制器 ansible_devices.size 设备存储空间 ansible_interfaces ⽹卡 ansible_{interfaces}.ipv4.address ⽹卡IP地址 ansible_{interfaces}.ipv6.0.address ⽹卡IPv6地址 ansible_{interfaces}.macaddress ⽹卡mac地址
剧本⾼级特性-注册变量
应⽤场景
调试,回显命令执⾏的内容 把状态保存成变量,其他任务可以进⾏判断或引⽤
使⽤内置变量将IP地址保存到⽂本⾥,并将⽂本内容显示出来
- hosts: all tasks: - name: echo IP shell: "echo {{ ansible_default_ipv4.address }} >> /tmp/ip.txt" - name: cat IP shell: "cat /tmp/ip.txt" register: ip_txt - debug: msg: "{{ ip_txt.stdout_lines }}"
如果配置⽂件发⽣了变化**,就重启服务,**否则不重启
- host: backup tasks: - name: copy_conf copy: src: /root/rsyncd.conf dest: /etc/ register: resync_status - name: start systemd: name: rsyncd state: started enabled: yes - name: restart systemd: name: rsyncd state: restarted wehen: rsync_status.changed
注册变量和判断场景
场景: 判断所有机器/root/下有没有ip.txt的⽂件 如果有,打印出来内容并且格式为: 例如: web01 has ip.txt 内容为: 如果不存在: 输出内容:nfs is nofile
解决方案
host: all tasks: - name: test1 shell: cat /root/ip.txt; register: status - name: test2 debug: msg: '{{ ansible_hostname }} has ip.txt , content is: {{status.stdout}}' when: status is success - name: test3 debug: msg: 'ip.txt is nofile' when: status is failed
剧本⾼级特性-服务状态管理
官⽅⽂档
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#handlers-running-operations-on-change
应⽤场景
如果配置⽂件发⽣了变化,就重启服务 如果配置⽂件没发⽣变化,不重启
命令实现
- hosts: backup tasks: - name: copy_conf copy: src: /root/script/rsync/rsyncd.conf dest: /etc/ notify: - restart rsyncd - name: start systemd: name: rsyncd state: started enabled: yes handlers: - name: restart rsyncd systemd: name: rsyncd state: restarted
错误总结
handlers必须放在最后执⾏ notify⾥的服务名称必须和handlers⾥定义的⼀样
剧本⾼级特性-选择标签
添加标签
- name: enable nfs systemd: name=nfs enabled=yes tags: enable-nfs
打印出playbook⾥要执⾏的所有标签
ansible-playbook --list-tags rsync_install.yaml
指定运⾏某个标签
ansible-playbook -t '03-install nfs service' rsync_install_tag.yaml
指定运⾏多个标签
ansible-playbook -t 01-add-group,02-add-user,05-create-data-dir rsync_install_tag.yaml
指定不运⾏某个标签
ansible-playbook --skip-tags 01-add-group rsync_install_tag.yaml
指定不运⾏多个标签
ansible-playbook --skip-tags 01-add-group,02-add-user,04-copy-nfs-exports rsync_install_tag.yaml
剧本⾼级特性-选择tasks
查看task列表
ansible-playbook --list-tasks rsync_install_tag.yaml
选择从哪⼀个task开始执⾏
ansible-playbook --start-at-task '05-create data dir' rsync_install_tag.yaml
运⾏检查规范
检查剧本拼写规范
ansible-playbook --syntax-check rsync_install_tag.yaml
检查这个任务执⾏的主机对象
ansible-playbook --list-hosts rsync_install_tag.yaml
检查这个剧本需要执⾏哪些任务
ansible-playbook --list-tasks rsync_install_tag.yaml
模拟执⾏剧本
ansible-playbook -C rsync_install_tag.yaml
真正执⾏
ansible-playbook rsync_install_tag.yaml