ansible-playbook采用的语法格式是YAML(Yet Another Markup Language)。YAML语法能够简单的表示散列表,字典等数据结构。简单来说, playbook是由一个或多个模块组成的,使用多个不同的模块,完成一件事情。
YAML格式三板斧
1 缩进:yaml使用一个固定的缩进格式表示层级结构,不能Tab进行缩进,缩进由两个空格代替。
2 冒号:所有冒号后面必须有空格,除了以冒号结尾
3 短横线:表示列表项,使用一个短横线加一个空格作为一个列表项,多个项使用同样的缩进级别作为同一列表
实机——httpd服务(使用处理器)
以下是一段远程安装httpd服务并配置启动的剧本
[root@xyy ~]#cat httpd_install.yaml
- hosts: C
remote_user: root
gather_facts: no
tasks:
- name: install httpd fuwu
yum: name=httpd,httpd-tools state=installed
- name: configure httpd fuwu
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: start httpd fuwu
service: name=httpd state=started enabled=yes
host:填写的是/etc/ansible/hosts中创建的组,也就是期望在哪些服务器上运行
remote_user:指定远程主机的使用用户
gather_facts:执行playbook时候,默认会采集目标主机的信息,禁用会提高效率
tasks:任务列表
执行命令使用ansible-playbook yaml文件名
[root@xyy /]#ansible-playbook /xy/httpd_install.yaml
处理器(notify、handlers)
Ansible提供了notify指令和handlers功能。如果在某个task中定义了notify指令,当Task(任务)状态为changed时,处理器就会执行你写好的handlers(处理器)操作。所谓handler,其实就是task,无论在写法上还是作用上它和task都没有区别,唯一的区别在于hander是被触发而被动执行的,不像普通task一样会按流程正常执行。
httpd_insert.yaml
- hosts: C
remote_user: root
gather_facts: no
tasks:
- name: install httpd fuwu
yum: name=httpd,httpd-tools state=installed
- name: configure httpd fuwu
copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart http fuwu
- name: start httpd fuwu
service: name=httpd state=started enabled=yes
handlers:
- name: restart httpd fuwu
service: name=httpd state=restarted
[root@xyy ~]#cat /xy/httpd_remove.yaml
- hosts: C
remote_user: root
gather_facts: yes
tasks:
- name: remove httpd
yum: name=httpd,httpd-tools state=removed
- name: remove apache user
user: name=apache state=absent
- name: remove httpd file
file: name=/etc/httpd state=absent
实机——nfs远程挂载文件
安装nfs-utils
yum install -y nfs-utils
修改/etc/exports配置文件,添加
/data *(rw,no_root_squash)
远程挂载nfs文件夹
[root@xyy xy]#cat /xy/web_mount.yaml
- hosts: C
remote_user: root
gather_facts: no
tasks:
- name: mount nfs server share data
mount: src=10.0.0.5:/data path=/data fstype=nfs opts=defaults state=mounted
src:ansible服务端本地需要挂载到用户端的文件夹
path:挂载到用户端的什么位置
state=absent为卸载
实机——配置文件变动时触发重启服务
准备好rsync配置文件
[root@xyy xy]#cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
list = false
auth users = rsync_backup
secrets file = /etc/rsyncd.conf.password
log file = /var/log/rsyncd.log
[data]
path=/data
编写yaml文件
[root@xyy xy]#cat rsync_install.yaml
- hosts: C
remote_user: root
gather_facts: no
tasks:
- name: install rsyncd.service
yum: name=rsync state=installed
- name: configure rsync
copy: src=/etc/rsyncd.conf dest=/etc/rsyncd.conf
notify: restart rsyncd server
#创建虚拟用户和密码文件,用户名:rsync_backup 密码:123456
- name: create virt uesr
copy: content='rsync_backup:123456' dest=/etc/rsyncd.password mode=600
- name: create yonghu www
group: name=www gid=666
#远程创建用户:create_home=no不创建家目录 指定shell不能登陆
- name: create yonghu www
user: name=www uid=666 group=www create_home=no shell=/sbin/nologin
#创建/data/为共享目录
- name: create data mulu
file: path=/data state=directory recurse=yes owner=www group=www mode=755
- name: start rsync server
service: name=rsyncd state=started enabled=yes
handlers:
- name: restart rsyncd server
service: name=rsyncd state=restarted