Playbooks与Ad-Hoc相比,是一种彻底不一样的运用Ansible的方式,并且是很是之强大的;也是系统ansible命令的集合,其利用yaml语言编写,运行过程,ansbile-playbook命令根据自上而下的顺序依次执行。Playbook遵循yaml规范
简单来讲,Playbooks 是一种简单的配置管理系统与多机器部署系统的基础。与现有的其余系统有不一样之处,且很是适合于复杂应用的部署。node
同时,Playbooks开创了不少特性,它能够容许你传输某个命令的状态到后面的指令,如你能够从一台机器的文件中抓取内容并附为变量,而后在另外一台机器中使用,这使得你能够实现一些复杂的部署机制,这是ansible命令没法实现的。python
Playbooks可用于声明配置,更强大的地方在于,在Playbooks中能够编排有序的执行过程,甚至于作到在多组机器间,来回有序的执行特别指定的步骤。而且能够同步或异步的发起任务。ios
咱们使用Ad-Hoc时,主要是使用 /usr/bin/ansible 程序执行任务.而使用Playbooks时,更可能是将之放入源码控制之中,用之推送你的配置或是用于确认你的远程系统的配置是否符合配置规范。web
Usage: ansible-playbook [options] playbook.yml [playbook2 ...] -C, --check # 检查可是不会真的执行 -f FORKS, --forks=FORKS # 并发,默认是5个 --list-hosts #列出匹配的主机 --syntax-check # 检查语法
[root@node1 playbook]# vim p1.yml - hosts: web #指定要执行的主机 remote_user: root #指定用户,使用root用户能够不写 tasks: #要执行的任务 - name: copyfile #任务的名字 copy: src=/etc/fstab dest=/tmp/fs #要执行的模块
检查语法redis
[root@node1 playbook]# ansible-playbook --syntax-check p1.yml
执行文件shell
[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
多个任务是,按照顺序执行,第一个任务在全部机器多行执行完成后,才会执行第二个任务vim
[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后面添加参数dom
[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文件
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主机名
只执行某个配置
- 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
更改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
例:将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
判断条件是否成立
- 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}