Playbooks与Ad-Hoc相比,是一种完全不同的运用Ansible的方式,而且是非常之强大的;也是系统ansible命令的集合,其利用yaml语言编写,运行过程,ansbile-playbook命令根据自上而下的顺序依次执行。Playbook遵循yaml规范
简单来说,Playbooks 是一种简单的配置管理系统与多机器部署系统的基础。与现有的其他系统有不同之处,且非常适合于复杂应用的部署。
同时,Playbooks开创了很多特性,它可以允许你传输某个命令的状态到后面的指令,如你可以从一台机器的文件中抓取内容并附为变量,然后在另一台机器中使用,这使得你可以实现一些复杂的部署机制,这是ansible命令无法实现的。
Playbooks可用于声明配置,更强大的地方在于,在Playbooks中可以编排有序的执行过程,甚至于做到在多组机器间,来回有序的执行特别指定的步骤。并且可以同步或异步的发起任务。
我们使用Ad-Hoc时,主要是使用 /usr/bin/ansible 程序执行任务.而使用Playbooks时,更多是将之放入源码控制之中,用之推送你的配置或是用于确认你的远程系统的配置是否符合配置规范。
palybook命令格式
Usage: ansible-playbook [options] playbook.yml [playbook2 ...] -C, --check # 检查但是不会真的执行 -f FORKS, --forks=FORKS # 并发,默认是5个 --list-hosts #列出匹配的主机 --syntax-check # 检查语法
第一个playbook文件
单个任务
[root@node1 playbook]# vim p1.yml - hosts: web #指定要执行的主机 remote_user: root #指定用户,使用root用户可以不写 tasks: #要执行的任务 - name: copyfile #任务的名字 copy: src=/etc/fstab dest=/tmp/fs #要执行的模块
检查语法
[root@node1 playbook]# ansible-playbook --syntax-check p1.yml
执行文件
[root@node1 playbook]# ansible-playbook p1.yml
执行多个任务
[root@node1 playbook]# cat p1.yml - hosts: web remote_user: root tasks: - name: copyfile copy: src=/etc/fstab dest=/tmp/fs - name: createuser user: name=wl
多个任务是,按照顺序执行,第一个任务在所有机器多行执行完成后,才会执行第二个任务
ansible-palybook的传参
第一种方式
[root@onde1 playbook]# cat p1.yml - hosts: web remote_user: root tasks: - name: create{{user}} user: name={{user}}
执行
[root@node1 playbook]# ansible-playbook -e user=wangl p1.yml
第二种方式
修改ansible的hosts文件
[root@node1 playbook]# vim /etc/ansible/hosts
在要执行的组IP后面添加参数
[web] 10.0.0.22 user=wl2 10.0.0.23 user=wl3 [db] 10.0.0.23 10.0.0.24
执行
[root@node1 playbook]# ansible-playbook p1.yml
第三种方式
同样修改hosts文件
[root@node1 playbook]# cat /etc/ansible/hosts [web] 10.0.0.22 10.0.0.23 [web:vars] #添加[web:vars]分组 user=wl3
执行
[root@node1 playbook]# ansible-playbook p1.yml
第四种方式
在yml文件中添加vars
[root@node1 playbook]# cat p1.yml - hosts: web vars: - user: wl5 remote_user: root tasks: - name: create{{user}} user: name={{user}}
第五种方式
[root@node1 playbook]# cat p1.yml - hosts: web tasks: - name: yumbc yum: name=bc - name: sum shell: echo 8+9|bc #计算相加的值 register: user # 将返回的值注册为user,赋值操作 - name: echo shell: echo {{user.stdout}} >/tmp/sum.txt #user.stdout=17 - name: createuser{{user.stdout}} user: name=wl{{user.stdout}}
user
{stderr_lines: [], uchanged: True, uend: u2019-04-09 23:17:43.525072, failed: False, ustdout: u17, ucmd: uecho 8+9|bc,
urc: 0, ustart: u2019-04-09 23:17:43.518708, ustderr: u, udelta: u0:00:00.006364, stdout_lines: [u17]} #u表示编码:unicode
优先级
-e > playbook的vars > hosts文件
查看收集到的信息
setup
TASK [Gathering Facts] ******************************************************* ok: [10.0.0.23] ok: [10.0.0.22]
查看10.0.0.22的信息
[root@node1 playbook]# ansible 10.0.0.22 -m setup
ansible_all_ipv4_addresses # 所有的ipv4地址 ansible_all_ipv6_addresses # 所有的ipv6的地址 ansible_bios_version # 主板bios的版本 ansible_architecture # 架构信息 ansible_date_time # 系统的时间 ansible_default_ipv4 # IPv4默认地址 address #ip地址 alias #网卡名称 broadcast #广播地址 gateway # 网关 macaddress #mac地址 netmask #子网掩码 network #网段 ansible_distribution #系统的版本 ansible_distribution_file_variety# 系统的基于对象 ansible_distribution_major_version# 系统的主版本 ansible_distribution_version #系统的版本 ansible_domain #系统的域 ansible_dns #系统的dns ansible_env #系统的环境变量 ansible_hostname #系统的主机名 ansible_fqdn #系统的完整主机名 ansible_machine #系统的架构 ansible_memory_mb #系统的内存信息 ansible_os_family #系统的家族 ansible_pkg_mgr #系统的包管理工具 ansible_processor_cores #cpu的核数 ansible_processor_count #每颗cpu上的颗数 ansible_processor_vcpus #cpu的总核数=cpu的颗数*每颗cpu上的核数 ansible_python #系统的python版本
查看cpu的信息
[root@node1 playbook]# ansible 10.0.0.22 -m setup -a "filter=*processor*" #想要查看本机可以改为127.0.0.1或localhosts主机名
tags
只执行某个配置
- hosts: web tasks: - name: install yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/etc/redis.conf tags: copyfile #为copy模块添加标签 - name: start service: name=redis state=started
执行,只会执行copyfile
ansible-playbook -t copyfile p7.yml
handlers
更改redis的配置文件并重启
- hosts: web tasks: - name: install yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/etc/redis.conf tags: copy notify: restart #触发restart执行 - name: start service: name=redis state=started handlers: #不会自动执行,需要notify触发 - name: restart service: name=redis state=restarted
执行
ansible-playbook -t copyfile p7.yml
template
使用绝对路劲
例:将copy过去的redis的配置文件中的ip地址改为远程机器的IP地址
- hosts: web tasks: - name: install yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=/etc/redis.conf tags: copyfile notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted
redis.conf文件配置
# bind 127.0.0.1 bind {{ansible_default_ipv4.address}}
使用相对路径
在本地的目录下创建一个templates目录,就可以用相对路径
[root@node1 playbook]# mkdir templates [root@node1 playbook]# cp /etc/redis.conf templates/
- hosts: web tasks: - name: install yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=redis.conf tags: copy notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted
when
判断条件是否成立
- hosts: web tasks: - name: file copy: content="大弦嘈嘈如急雨" dest=/opt/file when: ansible_distribution_major_version=="7" #如果系统版本是7 - name: file copy: content="小弦切切如私语" dest=/opt/file when: ansible_distribution_major_version=="6" #如果系统版本是6
也可以传值判断
- hosts: web tasks: - name: file copy: content="大弦嘈嘈如急雨\n" dest=/opt/file when: sum=="7" - name: file copy: content="小弦切切如私语\n" dest=/opt/file when: sum=="6"
执行
ansible-playbook -e sum=7 p11.yml
循环
- hosts: web tasks: - name: file user: name={{item}} with_items: - wl20 - wl21 - hosts: web tasks: - name: creategroup group: name={{item}} with_items: - wl20 - wl21 - name: file user: name={{item}} with_items: - zs22 - zs23
嵌套循环
- hosts: web tasks: - name: creategroup group: name={{item}} with_items: - wl22 - wl23 - name: file user: name={{item.name}} group={{item.group}} with_items: - {"name":zs24,"group":wl22} - {"name":zs25,"group":wl23}