[root@m01 ansible_playbook]#vim nfs_auto.yaml - hosts: nfs_server tasks: - name: 1:install rpcbind nsf-utils yum: name: - rpcbind - nfs-utils state: installed - name: 2:copy conf file copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/exports - name: 3:create data dir file: path=/data/ state=directory owner=nfsnobody group=nfsnobody - name: 4:boot server rcbind service: name=rpcbind state=started enabled=yes - name: 4:boot server nfs service: name=nfs state=restarted enabled=yes - hosts: nfs_client tasks: - name: 1:install nfs yum: name=nfs-utils state=installed - name: 2:mount data dir mount: src=172.16.1.31:/data/ path=/mnt fstype=nfs state=mounted
恢复环境剧本
[root@m01 ansible_playbook]#vim nfs_backup.yaml - hosts: nfs_server tasks: - name: 01:install rpcbind nfs-utils yum: name: - rpcbind - nfs-utils state: removed - name: 02:copy conf file shell: echo "" >/etc/exports - name: 03:create data dir file: path=/data/ state=absent - hosts: nfs_client tasks: - name: 01:install nfs yum: name=nfs-utils state=removed - name: 02:mount data dir mount: src=172.16.1.31:/data/ path=/mnt fstype=nfs state=unmounted
优化剧本:
[root@m01 ansible_playbook]#vim nfs_auto.yaml - hosts: nfs_server vars: conf_file: exports data_dir: /data tasks: - name: 01:install nfs rpcbind yum: name: ['nfs-utils', 'rpcbind'] state: installed - name: 02:copy conf file copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/{{ conf_file }} notify: - nfs_restart - name: 03:create data dir file: path={{ data_dir }} state=directory owner=nfsnobody group=nfsnobody - name: 04:boot server rpcbind service: name={{ item.name }} state={{ item.state }} enabled={{ item.enabled }} with_items: - {name: "rpcbind", state: "started", enabled: "yes"} - {name: "nfs", state: "started", enabled: "yes"} handlers: - name: nfs_restart service: name=nfs state=reloaded - hosts: nfs_client vars: data_dir: /data tasks: - name: 01:install nfs yum: name=nfs-utils state=installed - name: 02:mount data dir mount: src=172.16.1.31:{{ data_dir }} path=/mnt fstype=nfs state=mounted - name: 03:check mount info shell: df -h|grep mnt register: mount_info - debug: msg={{ mount_info.stdout_lines }}
1.10 ansible剧本实现sersync一键化部署
第一个历程: 按照模块方式,完成服务每个步骤部署配置hosts主机清单
[server_server] 172.16.1.31 [server_client] 172.16.1.41 #安装rsync ansible backup_server -m yum -a "name=rsync state=installed" #在批量管理主机上下载sersync,解压发送给客户端 ansible backup_server -m file -a "src=/usr/local/sersync_installdir_64bit/sersync dest=/usr/local" #在批量管理主机上写好sersync配置文件,发送给客户端 ansible backup_server -m copy -a "src=/usr/local/sersync_installdir_64bit/sersync/conf/confxml.xml dest=/usr/local/sersync/conf/" #给sersync加上执行权限 ansible backup_server -m file -a "path=/usr/local/sersync/bin/sersync mode=a+x" #给sersync创建软链接 ansible backup_server -m file -a "src=/usr/local/sersync/bin/sersync path=/usr/local/sbin/sersync state=link" #启动sersync 测试实时同步 ansible backup_server -m shell -a "sersync -dro /usr/local/sersync/conf/confxml.xml"
内容版权声明:除非注明,否则皆为本站原创文章。