剧本的组成部分
演员信息:男一号 hosts
干的事情:吃饭 tasks
演员信息:男二号
干的事情:看着
剧本编写的规范:三点要求
1.合理的信息缩进(一般都是两个空格)
标题一
标题二
标题三
ps:在ansible中一定不能使用tab进行缩进
2.冒号的使用方法
hosts: 10.0.0.11
tasks:(这里不需要空格)
yum: name=xxx
ps: 使用冒号时后面要有空格信息(空一格)
以冒号结尾的,冒号信息出现在注释说明中,后面都不需要加空格
3.短横线应用 -(列表功能)
男
- 打游戏
- 运动
北京
- 李四
女
学习
上海
- 王五
男
运动
深圳
ps:使用短横线构成列表信息,短横线后面需要有空格
检查剧本的语法
ansible-playbook --syntax-check rsync_server.yaml
模拟执行剧本
ansible-playbook -C rsync-server.yaml
直接执行剧本
ansible-playbook rsync_server.yaml
简单的剧本
创建目录
mkdir /etc/ansible/ansible-playbook
cd /etc/ansible/ansible-playbook
vim rsync_server.yaml
- hosts: 10.0.0.11
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-push conf file
copy: src=/tmp/rsyncd.conf dest=/etc/
#- hosts: 10.0.0.12
# tasks:
#简单模拟执行脚本
ansible-playbook -C rsync_server.yaml
PLAY [10.0.0.11] ********************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [10.0.0.11]
TASK [01-install rsync] *************************************************************************************************************************
ok: [10.0.0.11]
TASK [02-push conf file] ************************************************************************************************************************
changed: [10.0.0.11]
PLAY RECAP **************************************************************************************************************************************
10.0.0.11 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
真正的一键化部署rsync
创建目录
mkdir /etc/ansible/ansible-playbook
mkdir /etc/ansible/server_file/rsync_server -p
- hosts: 10.0.0.10
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-push conf file
copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/
- name: 03-create user
user: name=rsync create_home=no shell=/sbin/nologin
- name: 04-create backup dir
file: path=/backup state=directory owner=rsync group=rsync
- name: 05-create password file
copy: content=rsync_backup:sky123 dest=/etc/rsync.password mode=600
- name: 06-start rsync server
service: name=rsyncd state=started enabled=yes
- hosts: 10.0.0.11
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-create password file
copy: content=sky123 dest=/etc/rsync.password mode=600
- name: 03-check test file
file: dest=/tmp/test.txt state=touch
- name: 04-check test
shell: rsync -avz /tmp/test.txt rsync_backup@10.0.0.10::backup --password-file=/etc/rsync.password
ansible-playbook --syntax-check rsync_server.yaml
ansible-playbook -C rsync_server.yaml
ansible-playbook rsync_server.yaml
PLAY [10.0.0.10] ********************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [10.0.0.10]
TASK [01-install rsync] *************************************************************************************************************************
ok: [10.0.0.10]
TASK [02-push conf file] ************************************************************************************************************************
changed: [10.0.0.10]
TASK [03-create user] ***************************************************************************************************************************
changed: [10.0.0.10]
TASK [04-create backup dir] *********************************************************************************************************************
changed: [10.0.0.10]
TASK [05-create password file] ******************************************************************************************************************
changed: [10.0.0.10]
TASK [06-start rsync server] ********************************************************************************************************************
changed: [10.0.0.10]
PLAY [10.0.0.11] ********************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [10.0.0.11]
TASK [01-install rsync] *************************************************************************************************************************
ok: [10.0.0.11]
TASK [02-create password file] ******************************************************************************************************************
changed: [10.0.0.11]
TASK [03-check test file] ***********************************************************************************************************************
changed: [10.0.0.11]
TASK [04-check test] ****************************************************************************************************************************
changed: [10.0.0.11]
PLAY RECAP **************************************************************************************************************************************
10.0.0.10 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.11 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
查看一下
[root@ansible01 ansible]# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 30775 root 3u IPv4 163084 0t0 TCP *:rsync (LISTEN)
rsync 30775 root 5u IPv6 163085 0t0 TCP *:rsync (LISTEN)
剧本编写常见错误
1.剧本语法是否符合(空格 冒号 短横线)
2.剧本中模块使用是否正确
3.剧本中一个name标识下面只能写一个模块任务信息
4.剧本中有时不能拿反复执行
5.剧本中尽量不要大量使用shell模块
ansible主机清单
/etc/ansible/hosts
1.分组配置主机信息
[data]
10.0.0.10
10.0.0.11
[web]
10.0.0.12
10.0.0.13
显示data组的hostname
ansible data -a "hostname"
2.主机名符号匹配配置12-15
[web]
10.0.0.[12:15]
或者通过主机名(需要在/etc/hosts进行解析)
[web]
web[01:03]
3端口变化
[web]
web01:52113
4主机的特殊变量
[web]
10.0.0.123 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
5主机名嵌入配置
[rsync:children] ---嵌入子组信息
rsync_server
rsync_client
[rsync_server]
10.0.0.10
[rsync_client]
10.0.0.11
剧本扩展应用
1.在剧本中设置变量信息
2.在剧本中设置注册信息
3.在剧本中设置判断信息
4.在剧本中设置循环信息
5.在剧本中设置错误忽略
6.在剧本中设置标签信息
7.在剧本中设置触发信息
8.在剧本中进行剧本整合
优化上面的rsync剧本
vars:
变量名: 值
调用{{ 变量名 }}
变量信息的使用
- hosts: 10.0.0.10
vars:
backupdir: /backup
passfile: rsync.password
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-push conf file
copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/
- name: 03-create user
user: name=rsync create_home=no shell=/sbin/nologin
- name: 04-create backup dir
file: path={{ backupdir }} state=directory owner=rsync group=rsync
- name: 05-create password file
copy: content=rsync_backup:sky123 dest=/etc/{{ passfile }} mode=600
- name: 06-start rsync server
service: name=rsyncd state=started enabled=yes
- hosts: 10.0.0.11
vars:
backupdir: /backup
passfile: rsync.password
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-create password file
copy: content=sky123 dest=/etc/{{ passfile }} mode=600
- name: 03-check test file
file: dest=/tmp/test.txt state=touch
- name: 04-check test
shell: rsync -avz /tmp/test.txt rsync_backup@10.0.0.10::backup --password-file=/etc/{{ passfile }}
在剧本中设置注册信息
- hosts: 10.0.0.10
tasks:- name:check server port
shell: netstat -lntup ---端口信息
register: get_server_port <--端口信息 - name: display port info
debug: msg={{ get_server_port.stout_lines }}
添加返回端口
- name:check server port
- hosts: 10.0.0.10
vars:
backupdir: /backup
passfile: rsync.password
tasks:
- name: 01-install rsync
yum: name=rsync state=installed
- name: 02-push conf file
copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/
- name: 03-create user
user: name=rsync create_home=no shell=/sbin/nologin
- name: 04-create backup dir
file: path={{ backupdir }} state=directory owner=rsync group=rsync
- name: 05-create password file
copy: content=rsync_backup:sky123 dest=/etc/{{ passfile }} mode=600
- name: 06-start rsync server
service: name=rsyncd state=started enabled=yes
- name: 07-check server port info
shell: netstat -lntup|grep 873
register: get_server_port
- name: display port info
debug: msg={{ get_server_port.stdout_lines }}
设置判断
(ansible_hostname == "nfs")
setup模块中显示的信息中有的变量
- hosts: 10.0.0.11
remote_user: root
tasks:name: check file
file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
when: (ansible_hostname=="nfs") or (ansible_hostname=="backup")name: install httpd
yum: name=httpd state=installed
when: (系统情况 == 'centos')name: install httpd2
yum: name=httpd2 state=installed
when: (系统情况=='ubuntu')
循环
vim test.yaml
- hosts: all
remote_user: root
tasks:
- name: add users
user: name={{ item.name }} groups={{ item.groups }} state=present
with_items:
- { name: 'testuser1', groups: 'bin' }
- { name: 'testuser2', groups: 'root' }
vim test2.yaml
- hosts: all
remote_user: root
tasks:
-name: Installed pkg
yum: name={{ item }} state=installed
with_items:
- wget
- tree
- lrszsz
方法二
- hosts: all
remote_user: root
tasks:
-name: Installed pkg
yum:
name: ['rsync','tree','wget']
state: installed
设置错误忽略
加入ignore_errors: yes 忽略错误
vim test3.yaml
- hosts: all
remote_user: root
tasks:
- name: Ignore False
command: /bin/false
ignore_errors: yes
- name: touch new file
file: path=/tmp/oldboy_ignore state=touch
设置标签功能
设置标签,可以跳过
- hosts: all
ignore_errors: yes
remote_user: root
tasks:
- name: check file
file: path=/tmp/this_is{{ ansible_hostname }}_file state=touch
when: (ansible_hostname=="nfs01") or (ansible_hostname=='bakcup')
tags: t1
....
tags: t2
ansible-playbook --tags=t1 test5.yaml 指定执行那个标签
ansible-playbook --skip-tags=t2 test5.yaml 跳过执行的标签
触发器notify
- hosts: backup
remote_user: root
tasks:
- name: 01 install rsync
yum: name=resync state=installed
- name: 02 push config file
copy: src=./file/{{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }}
with_items:
- {src: "rsyncd.conf",dest: "rsyncd.conf",mode: "0644"}
- {src: "rsync.password",dest: "rsync.password",mode: '0600'}
notify:restart rsync server
handlers:
- name:restart resync server
service:name=resyncd state=restarted
编写nfs剧本
cd /etc/ansible/ansible-ploybook
mkdir nfs-file/{nfs-server,nfs-client} -p
echo '/data 10.0.0.0/24(rw,sync)'>nfs-file/nfs-server/exports
编写主机清单
vim /etc/ansible/hosts
[nfs:children]
nfs_server
nfs_client
[nfs_server]
10.0.0.10
[nfs_client]
10.0.0.11
vim /etc/ansible/ansible-playbook/nfs-server.yaml
- hosts: nfs
tasks:
- name: 01-install nfs software
yum:
name: ['nfs-utils','rpcbind']
state: installed
- hosts: nfs_server
vars:
Data_dir: /data
tasks:
- name: 01-copy conf file
copy: src=/etc/ansible/ansible-playbook/nfs-file/nfs-server/exports dest=/etc
notify: restart nfs server
- name: 02-create data dir
file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody
#file:
#path: ['data01','data02','data03']
#state: directory
#owner: nfsnobody
#group: nfsnobody
- name: 03-boot server
#service: name=rpcbind state=started enabled=yes
#service: name=nfs state=started enabled=yes
service: name={{ item }} state=started enabled=yes
with_items:
- rpcbind
- nfs
handlers:
- name: restart nfs server
service: name=nfs state=restarted
- hosts: nfs_client
vars:
Data_dir: /data
tasks:
- name: 01-mount
mount: src=10.0.0.10:{{ Data_dir }} path=/mnt fstype=nfs state=mounted
- name: 02-check mount info
shell: df -h|grep /data
register: mount_info
- name: display mount
debug: msg={{ mount_info.stdout_lines }}
ansible-playbook nfs-server.yaml
PLAY [nfs] **************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [10.0.0.11]
ok: [10.0.0.10]
TASK [01-install nfs software] ******************************************************************************************************************
ok: [10.0.0.11]
ok: [10.0.0.10]
PLAY [nfs_server] *******************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [10.0.0.10]
TASK [01-copy conf file] ************************************************************************************************************************
changed: [10.0.0.10]
TASK [02-create data dir] ***********************************************************************************************************************
ok: [10.0.0.10]
TASK [03-boot server] ***************************************************************************************************************************
ok: [10.0.0.10] => (item=rpcbind)
ok: [10.0.0.10] => (item=nfs)
RUNNING HANDLER [restart nfs server] ************************************************************************************************************
changed: [10.0.0.10]
PLAY [nfs_client] *******************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [10.0.0.11]
TASK [01-mount] *********************************************************************************************************************************
changed: [10.0.0.11]
TASK [02-check mount info] **********************************************************************************************************************
changed: [10.0.0.11]
TASK [display mount] ****************************************************************************************************************************
ok: [10.0.0.11] => {
"msg": [
"10.0.0.10:/data 19G 1.8G 18G 10% /mnt"
]
}
PLAY RECAP **************************************************************************************************************************************
10.0.0.10 : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.11 : ok=6 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
查看
cat /etc/fstab
10.0.0.10:/data /mnt nfs defaults 0 0
将多个剧本进行整合
import_playbook: rsync_server.yaml
import_playbook: nfs-server.yaml