首先下载ansible
yum install ansible -y
然后配置/etc/ansible/hosts文件
[root@m01 /server/scripts]# cat /etc/ansible/hosts
[oldboy]
172.16.1.66
[data]
172.16.1.67
172.16.1.68
172.16.1.69
[oldgirl]
172.16.1.68
172.16.1.69
在把 /etc/ansible/ansible.cfg 71的注释删除
[root@m01 /server/scripts]# vim /etc/ansible/ansible.cfg +71
host_key_checking = False
[root@m01 /server/scripts]# vim /etc/ansible/yaml/dajuben.yml
- hosts: oldboy
tasks:
- name: rsync服务段操作 复制rsync配置文件
copy: src=/etc/rsyncd.conf dest=/etc/rsyncd.conf backup=yes
- name: 重启rsync服务
systemd: name=rsyncd state=restarted
- name: 创建rsync用户
user: name=rsync
- name: 创建backup目录和设置他的属组和属主
file: path=/backup state=directory owner=rsync group=rsync
- name: 创建密码文件
shell: echo "rsync_backup:oldboy" > /etc/rsync.password
- name: 设置他的权限
file: path=/etc/rsync.password mode=0600
- name: 免密码
shell: echo "export RSYNC_PASSWORD=rsync_backup:oldboy" >>/etc/bashrc
- name: 生效免密码
shell: source /etc/bashrc
- hosts: data
tasks:
- name: rssync客户端操作创建密码文件
shell: echo "oldboy" > /etc/rsync.password
- name: 设置他的权限
file: path=/etc/rsync.password mode=0600
- name: 免密码
shell: echo 'export RSYNC_PASSWORD=oldboy' >>/etc/bashrc
- name: 生效免密码
shell: source /etc/bashrc
- hosts: 172.16.1.67
tasks:
- name: nfs服务端操作 安装 nfs和rpcbind
shell: yum install nfs-utils rpcbind -y
- name: 启动rpcbind和开机自启动
systemd: name=rpcbind enabled=yes
- name: 启动nfs和开机自启动
systemd: name=nfs enabled=yes
- name: 复制/etc/exports文件
copy: src=/etc/exports dest=/etc/exports backup=yes
- name: 平滑重启nfs
systemd: name=nfs state=reloaded
- name: 创建lvsha用户
user: name=lvsha uid=3344
- name: 创建用户和设置属组和属主
file: path=/data state=directory owner=lvsha group=lvsha
- hosts: oldgirl
tasks:
- name: nfs客户端操作 安装nfs和rpcbind
shell: yum install nfs-utils rpcbind -y
- name: 启动rpcbind和开机自启动
systemd: name=rpcbind enabled=yes
- name: 启动nfs和开机自启动
systemd: name=nfs enabled=yes
- name: 创建lvsha用户
user: name=lvsha uid=3344
- name: 永久挂载
mount: src=172.16.1.67:/data path=/mnt fstype=nfs opts=defaults state=mounted
- hosts: data
tasks:
- name: syrsync客户端操作 复制文件
copy: src=/application/ dest=/application/
- name: 加权限
file: path=/application/ recurse=on mode=+x
- name: 启动服务
shell: /application/sersync/bin/sersync -d -o /application/sersync/conf/confxml.xml &>/dev/null
写个脚本
[root@m01 /server/scripts]# cat dajuben.sh
#!/bin/sh
#删除公钥
[ ~/.ssh/id_rsa ]&& rm -fr ~/.ssh
#创建密钥对
ssh-keygen -f ~/.ssh/id_rsa -P '' -q &&\
#分发公钥和优化ssh
for n in 172.16.1.{66..69}
do
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $n
ansible $n -m copy -a "src=/etc/ssh/sshd_config dest=/etc/ssh/sshd_config backup=yes"
done &&\
#执行剧本
ansible-playbook /etc/ansible/yaml/dajuben.yml