playbook介绍
ansible playbook是一系列ansible命令的集合,利用yaml(Yet Another Markup Language)语言编写。playbook命令根据自上而下的顺序依次执行。
playbook基本语法
vim users.yml
---
- name: create user
hosts: webservers, prod
tasks:
- name: create admin1
user:
name: admin1
state: present
- name: create it group
group:
name: it
state: present
ansible-playbook --syntax-check users.yml
ansible-playbook -C users.yml
ansible-playbook users.yml
# ---:顶格写,作为语法开头。
# - name:顶格写,描述信息,可为空。
# hosts:主机组
# tasks:定义要做的任务,一个任务由一个-开始,如果name前不加-,则user前面必须加-
# user:使用user模块
# state:定义是存在(present)还是不存在(absent)
# -C:测试playbook,不会真的执行。有的时候后面的操作需要依赖前面的操作,所以不一定测试失败,真的执行的时候也会失败
playbook装包
---
- name: Install package and upgrade software
hosts: prod
tasks:
- name: install httpd and php
yum:
name:
- httpd
- php
state: present
- name: copy index.html to /var/www/html
copy:
src: /home/admin/index.html
dest: /var/www/html/index.html
owner: root
group: root
mode: 0644
- name: modify httpd.conf
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen 80'
line: Listen 8080
- name: started httpd service
service:
name: httpd
state: restarted
enabled: yes
- name: Install Deveploment Tools
hosts: webservers
tasks:
- name: install tools
yum:
name: "@Development Tools"
state: present
- name: upgrade all software and all hosts
hosts: all
tasks:
- name: upgrade software at all hosts
yum:
name: '*'
state: latest