playbook模块
把所有ansible命令放在文件里执行就是playbook。
playbook替代方案1:
[root@m01 ~]# cat ansible.sh
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"
ansible oldboy -m yum -a "name=nginx state=installed"
ansible oldboy -m service -a "name=crond state=started enabled=yes"
ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"
playbook替代方案2:
[root@m01 ~]# cat ~/set.sh
touch /tmp/oldboy_file
chown oldboy.oldboy /tmp/oldboy_file
yum install nginx -y
/etc/init.d/crond start
chkconfig cornd on
echo '#sync time oldboy' >>/var/spool/cron/root
echo '00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root
执行:
ansible oldboy -m script -a "~/set.sh"
ansible剧本编写格式说明
ansible剧本遵循PYyaml语法规则进行编写,ymal文件基本编写规则如下说明:
规则一:缩进
yaml使用一个固定的缩进风格表示数据层结构关系,需要每个缩进级别由两个空格组成。切记一定不能使用tab键进行缩进。
规则二:冒号
每个冒号后面一定要有一个空格(以冒号结尾不需要空格,表示文件路径的模版可以不需要空格)
规则三:短横线
想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分
- name: This command will change the working directory to somedir/ and will only run when /path/to/database doesn't exist.
command: /usr/bin/make_database.sh arg1 arg2
args:
chdir: somedir/
creates: /path/to/database
[root@m01 ~]# cat /etc/ansible/a.yml
- hosts: oldboy
tasks:
- shell: echo hello oldboy linux. >/tmp/a.log
ansible oldboy -m command -a "echo hello oldboy linux."
=========写成剧本
- hosts: oldboy
task:
- command: echo hello oldboy linux.
=========写成剧本
ansible oldboy -m command -a "pwd chdir=/etc"
- hosts: oldboy
task:
- command: echo hello oldboy linux.
用ansible完成一键部署rsync服务端。
剧本一键部署rsync服务器nfs服务器和sersync服务器
- hosts: fuwuduan
tasks:
- name: 下载rsync软件包
yum: name=rsync state=installed
- name: 服务端配置配置文件
copy: src=/data/rsyncd.conf.template dest=/etc/rsyncd.conf mode=0600 backup=yes
- name: 创建用户
user: name=rsync
- name: 建立密码文件并授权
copy: content='rsync_backup:oldboy' dest=/etc/rsync.password mode=0600
- name: 创建目录
file: path=/backup owner=rsync group=rsync state=directory
- name: 启动rsyncd服务
systemd: name=rsyncd state=restarted enabled=yes
- hosts: kehuduan
tasks:
- name: 配置客户端
shell: echo 'export RSYNC_PASSWORD=oldboy' >>/etc/bashrc #====>追加不成功
shell: source /etc/bashrc
#======================================================#
- hosts: kehuduan
tasks:
- name: 下载nfs-utils\rpcbind软件包
yum: name=nfs-utils state=installed
yum: name=rpcbind state=installed
- name: 创建共享目录/data1
file: path=/data1 state=directory owner=nfsnobody group=nfsnobody
- name: 编写/etc/exports文件
shell: echo '/data1 172.16.1.0/24(rw,sync)' >>/etc/exports
- name: 开启rpcbind\nfs服务
service: name=rpcbind state=restarted enabled=yes
service: name=nfs state=reloaded enabled=yes
- hosts: fuwuduan
tasks:
- name: 下载nfs-utils\rpcbind软件包
yum: name=nfs-utils state=installed
yum: name=rpcbind state=installed
- name: 创建data1目录
file: path=/data1 state=directory
- name: 开启rpcbind\nfs服务
service: name=rpcbind state=restarted enabled=yes
service: name=nfs state=reloaded enabled=yes
- name: 共享目录挂载服务端
shell: mount -t nfs 172.16.1.31:/data1 /mnt
#==================================================#
- hosts: shishifuzhi
tasks:
- name: 追加密码文件
copy: content='oldboy' dest=/etc/rsync.password mode=0600
- name: 重启rsync服务
systemd: name=rsyncd state=restarted enabled=yes
- name: push application.zip
copy: src=/application/ dest=/application/
- name: 加权限
shell: chmod 755 /application/sersync/bin/sersync
- name: 开启sersync服务
shell: /application/sersync/bin/sersync -d -o /application/sersync/conf/confxml.xml