Playbook
Playbook 翻译过来就是“剧本”, 那 playbook 组成如下
play: 定义的是主机的角色
task: 定义的是具体执行的任务
playbook: 由一个或多个 play 组成,一个 play 可以包含多个 task 任务
简单理解为: 使用不同的模块完成一件事情
#Playbook 的优势
功能比ansible命令更强大
能很好的控制先后执行顺序, 以及依赖关系
语法展现更加的直观
ansible命令无法持久使用, playbook 可以持久使用
cat /etc/ansible/hosts
[a]
172.19.79.192 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
[b]
172.19.79.193 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
172.19.79.194 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
检查剧本语法
ansible-playbook --syntax-check file-playbook.yaml
模拟执行剧本
ansible-playbook -C file-playbook.yaml
执行剧本
ansible-playbook file-playbook.yaml
playbook-rsync创建
vim rsync_install.yaml
- hosts: a
tasks:
- name: 01-add group
group: name=www gid=666
- name: 02-add user
user: name=www createhome=no shell=/sbin/nologin group=www uid=666
- name: 03-install rsync
yum: name=rsync state=installed
- name: 04-copy rsync conf
copy: src=/root/rsyncd.conf dest=/etc/
- name: 05-create passwd conf
copy: content='rsyc_backup:123456' dest=/etc/rsync.passwd mode=600
- name: 06-ceate backup dir
file: path=/backup state=directory owner=www group=www
- name: 07-create data dir
file: path=/data state=directory owner=www group=www
- name: 08-start rsyncd service
service: name=rsyncd state=started
- name: 09-enabled rsyncd service
systemd: name=rsyncd enabled=yes
#################################
vim /root/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsyc_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to backup!
path = /backup
[data]
comment = welcome to data!
path = /data
Playbook-nfs
playbook-nfs服务端:
vim nfs_server_install.yaml
- hosts: a
tasks:
- name: 01-add group
group: name=www gid=666
- name: 02-add user
user: name=www createhome=no shell=/sbin/nologin group=www uid=666
- name: 03-install nfs service
yum: name=nfs-utils state=latest
- name: 04-copy nfs exports
copy: src=/root/exports dest=/etc/
- name: 05-create data dir
file: path=/data state=directory owner=www group=www
- name: 06-start rpcbind
service: name=rpcbind state=started
- name: 07-start nfs
service: name=nfs state=started
- name: 08-enable rpcbind
systemd: name=rpcbind enabled=yes
- name: 09-enable nfs
systemd: name=nfs enabled=yes
###########################################
playbook-nfs客户端:
vim nfs_client_install
- hosts: b
tasks:
- name: 01-add group
group: name=www gid=666
- name: 02-add user
user: name=www createhome=no shell=/sbin/nologin group=www uid=666
- name: 03-install nfs service
yum: name=nfs-utils state=installed
- name: 04-create data dir
file: path=/data state=directory owner=www group=www
- name: 05-start rpcbind
service: name=rpcbind state=started
- name: 06-enabled rpcbind
systemd: name=rpcbind enabled=yes
- name: 07-mount data
mount: path=/data src=172.19.79.192:/data fstype=nfs opts=defaults state=mounted
##################################################
vim /root/exports
/data 172.19.79.*(rw,sync,all_squash)
Playbook进阶
使用情景1:
1.需要创建多个目录
2.需要启动多个服务
vim dir.yaml
- hosts: a
tasks:
- name: 01-create data and backups
file:
path: "{{ item }}"
state: directory
with_items:
- /data
- /backup
同时启动2个服务
- hosts: a
tasks:
- name: 01-create data and backups
service:
name: "{{ item }}"
state: started
with_items:
- rpcbind
- nfs
使用情景2:
使用情景2:
如果配置文件发生了变化,就重启服务,否则什么都不操作
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html?highlight=handlers#handlers-running-operations-on-change
- hosts: a
tasks:
- name: 01-if nfs conf changed,then restart nfs service
copy:
src: /root/rsyncd.conf
dest: /etc/
notify: Restart_Nfs_Server
handlers:
- name: Restart_Nfs_Server
service:
name: nfs
state: restarted
使用场景3:
从我们指定的任务开始执行,而不是从头到尾执行一遍
- hosts: a
tasks:
- name: 01-add group
group: name=www gid=666
tags: 01-add-group
- name: 02-add user
user: name=www createhome=no shell=/sbin/nologin group=www uid=666
tags: 02-add-user
- name: 03-install rsync
yum: name=rsync state=installed
tags: 03-install-rsync
- name: 04-copy rsync conf
copy: src=/root/rsyncd.conf dest=/etc/
tags: 04-copy-rsync-conf
- name: 05-create passwd conf
copy: content='rsyc_backup:123456' dest=/etc/rsync.passwd mode=600
tags: 05-create-passwd-conf
- name: 06-ceate backup dir
file: path=/backup state=directory owner=www group=www
tags: 06-ceate-backup-dir
- name: 07-create data dir
file: path=/data state=directory owner=www group=www
tags: 07-create-data-dir
- name: 08-start rsyncd service
service: name=rsyncd state=started
tags: 08-start-rsyncd-service
- name: 09-enabled rsyncd service
systemd: name=rsyncd enabled=yes
tags: 09-enable
打印出playbook里要执行的所有标签
ansible-playbook --list-tags rsync_install.yaml
指定运行某个标签
ansible-playbook -t 05-create-passwd rsync_install.yaml
指定运行多个标签,使用逗号隔开
ansible-playbook -t 05-create-passwd,06-create-backup rsync_install.yaml
指定不运行某个标签
ansible-playbook --skip-tags=05-create-passwd rsync_install.yaml
指定不运行多个标签
ansible-playbook --skip-tags=05-create-passwd,06-create-backup rsync_install.yaml
使用场景4
从网上下载可执行文件,并赋予执行权限
- hosts: a
tasks:
- name: download ctop
get_url:
url: http://172.19.79.9:88/shared/888888/ctop-0.7.1-linux-amd64
dest: /usr/local/bin/ctop
force: yes
- name: chmod +x
file:
path: /usr/local/bin/ctop
mode: 0755
使用场景5
安装jdk8
- hosts: a
tasks:
- name: downlocad jdk8
get_url:
url: http://172.19.79.9/soft/java/jdk-8u144-linux-x64.tar.gz
dest: /tmp
force: yes
- name: unzip jdk8
unarchive: src=/tmp/jdk-8u144-linux-x64.tar.gz dest=/usr/local copy=no
或者:
##########################
- name: 安装解压工具
package:
name: "{{ item }}"
state: present
with_items:
- tar
- name: tar_xf_jdk8
shell: tar -xf /tmp/jdk-8u144-linux-x64.tar.gz -C /usr/local
###########################
- name: set ENV
lineinfile:
dest: /etc/profile
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
with_items:
- { regexp: '^JAVA_HOME=',line: 'export JAVA_HOME=/usr/local/jdk1.8.0_144' }
- { regexp: '^JRE_HOME=',line: 'export JRE_HOME=$JAVA_HOME/jre' }
- { regexp: '^CLASSPATH=',line: 'export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib' }
- name: Updata profiles
shell: source /etc/profile
args:
executable: /bin/bash
- name: Link java
file:
src: "/usr/local/jdk1.8.0_144/bin/java"
参数说明:
ansible unarchive:
功能:解压缩
排坑:
copy:
将远程主机上的某个压缩包解压缩到指定路径下。这种情况下,需要设置copy=no,远程主机上面的操作,不涉及ansible服务端
copy:默认为yes,当copy=yes,那么拷贝的文件是从ansible主机复制到远程主机上的,如果设置为copy=no,那么会在远程主机上寻找src源文件
src:源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no
dest:远程主机上的目标路径
mode:设置解压缩后的文件权限
lineinfile:
功能:文件中行的添加,替换
path参数 :必须参数,指定要操作的文件
line参数 : 使用此参数指定文本内容
regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。
作用:
lineinfile:
dest: /etc/profile
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
with_items:
- { regexp: '^JAVA_HOME=',line: 'export JAVA_HOME=/usr/local/jdk1.8.0_144' }
- { regexp: '^JRE_HOME=',line: 'export JRE_HOME=$JAVA_HOME/jre' }
- { regexp: '^CLASSPATH=',line: 'export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib' }
大意指,匹配以 ^JAVA_HOME=开头的行,如果有,则替换成line指定的内容,如果没匹配到,则在文本末尾添加line指定的内容,如果匹配到多行,则最后一行替换成line指定的内容