一、静态文件共享
1、编写脚本
[root@m0 ~]# vim /etc/tset000.sh
#!/bin/bash mkdir /tmp/three touch /tmp/three/test echo 'I am echo at mttt' > /tmp/three/test echo 'well done'
2、将mysql_master.sh 复制到被控制的主机上并下载nfs和rpcbind包
[root@m0 ~]# ansible -s -m copy -a 'src=./mysql_master.sh dest=~'
[root@m0 ~]# ansible -s -m command -a 'yum -y install nfs-utils'[root@m0 ~]# ansible s -m command -a 'yum -y install rpcbind'
3、编写/etc/exports文件
[root@m0 ~]# vim /etc/exports
/static *(ro,sync)
4、在被控制的主机上添加static目录并创建test文件
[root@m0 ~]# ansible s -m file -a 'path=/static state=directory'
[root@m0 ~]# ansible s -m file -a 'path=/static/test state=touch'
[root@m0 ~]# ansible s -m copy -a 'src=/etc/exports dest=/etc/exports'
5、两种启动方法
(1)command模块
[root@m0 ~]# ansible s -m command -a 'systemctl start nfs'
[root@m0 ~]# ansible s -m command -a 'systemctl enable nfs'
(2)service模块
[root@m0 ~]# ansible s -m service -a 'name=rpcbind state=started enabled=yes'
6、控制机下载nfs并将nfs挂载到指定目录
[root@m0 ~]# yum -y install nfs-utils
[root@m0 ~]# mkdir /nfs
[root@m0 ~]# mount -t nfs 192.168.8.181:/static /nfs/[root@m0 ~]# ls /nfs/
test
二、playbook
playbook(剧本):是ansible用于配置,部署,和管理被控节点的剧本。用于ansible操作的编排。
1、YMAL格式
■ 以.yaml或xyml结尾
■ 文件的第一行以” --- “开始,表明YMAL文件的开始(可选的)
■ 以#号开头为注释
■ 列表中的所有成员都开始于相同的缩进级别,并且使用一个"-“作为开头(一个横杠和一个空格)
■ 一个字典是由一个简单的 键:值 的形式组成(这个冒号后面必须是一个空格)
■ 注意:写这种文件可以使用tab键,空格
--- - hosts: 组名/别名/IP/域名 remote_user: root tasks: - name: 任务说明 模块: key0=value0 service: name=vsftpd state=started enabled=yes - name: 修改配置文件 command: sed..... notify: - abcdefg handler: - name:abcdefg service: name=vsftpd state=restarted
2、Playbook常⻅语法
hosts: ⽤于指定要执⾏任务的主机,其可以是⼀个或多个由冒号分 隔主机组.
remote_user: ⽤于指定远程主机上的执⾏任务的⽤户.
tasks:任务列表,按顺序执行任务.
■ 如果一个host执行task失败,整个tasks都会回滚,修正playbook中的错误,然后重新执行即可.
3、编写yml文件(playbook剧本是保存到控制机的yml文件)必须对齐
(1)卸载安装vsftpd
[root@m0 ~]# vim test001.yml
[root@m0 ~]# ansible-playbook ./test001.yml
--- #---可以省略 - hosts: s #ansible s中的主机 remote_user: root #root用户 tasks: #任务 - name: 安装vsftpd yum: name=vsftpd state=latest
(2)启动vsftpd服务
[root@m0 ~]# vim test001.yml
[root@m0 ~]# ansible-playbook ./test001.yml--- - hosts: s remote_user: root tasks: - name: 卸载vsftpd yum: name=vsftpd state=absent - name: 安装vsftpd yum: name=vsftpd state=latest - name: 启动服务 service: name=vsftpd state=started enabled=yes
[root@m0 ~]# yum -y install lftp
[root@m0 ~]# lftp 192.168.8.181
lftp 192.168.8.181:~> ls
(3)修改配置文件
[root@m0 ~]# vim test001.yml
[root@m0 ~]# ansible-playbook ./test001.yml
- name: 修改配置文件 command: sed -i '/^anonymous_enable=YES/s/YES/NO/g' /etc/vsftpd/vsftpd.conf
(4)不允许匿名访问,重启vsftpd服务
[root@m0 ~]# vim test001.yml
[root@m0 ~]# ansible-playbook ./test001.yml
[root@m0 ~]# lftp 192.168.8.181
lftp 192.168.8.181:~> ls
中断--- - hosts: s remote_user: root tasks: - name: 卸载vsftpd yum: name=vsftpd state=absent - name: 安装vsftpd yum: name=vsftpd state=latest - name: 启动服务 service: name=vsftpd state=started enabled=yes - name: 修改配置文件 command: sed -i '/^anonymous_enable=YES/s/YES/NO/g' /etc/vsftpd/vsftpd.conf notify: - abcdefg handlers: - name: abcdefg service: name=vsftpd state=restarted
四、roles(难点)
roles介绍
roles(角色):就是通过分别将variables,tasks及handlers等放置于单独的目录中,并可以便捷地调用它们的一种机制。
假设我们要写一个playbook来安装管理lamp环境,那么这个playbook就会写很长。所以我们希望把这个很大的文件分成多个功能拆分,分成apache管理,php管理,mysql管理,然后在需要使用的时候直接调用就可以了,以免重复写。就类似编程里的模块化的概念,以达到代码复用的效果。
[root@m0 ~]# yum -y install httpd
[root@m0 ~]# systemctl start httpd
[root@m0 ~]# echo 'http html file' > /var/www/html/index.html
[root@m0 ~]# curl localhost
http html file
[root@m0 ~]# vim /etc/httpd/conf/httpd.conf[root@m0 ~]# systemctl restart httpd
[root@m0 ~]# curl localhost:8080
http html file
1、编写剧本httpd服务
[root@m0 ~]# vim test002.yml
[root@m0 ~]# ansible-playbook test002.yml
--- - hosts: s remote_user: root tasks: - name: 将管理机的rope文件复制到被控制主机 copy: src=/etc/yum.repos.d dest=/etc/ - name: 安装httpd yum: name=httpd state=present - name: 修改配置文件 command: sed -i '/^Listen/s/80/8080/g' /etc/httpd/conf/httpd.conf - name: 修改默认资源文件 command: echo "xxxxx" > /var/www/html/index.html - name: 启动httpd服务 service: name=httpd state=started - name: 重启httpd服务 service: name=httpd state=restarted
2、创建文件(编排多个hosts任务)
[root@m0 ~]# vim test003.yml
[root@m0 ~]# ansible-playbook ./test003.yml--- - hosts: s remote_user: root tasks: - name: 创建一个文件 file: path=/tmp/xxxx.txt state=touch - hosts: s remote_user: root tasks: - name: 创建一个文件 file: path=/tmp/yyyy.txt state=touch ...
[root@s0 ~]# ls /tmp/
xxxx.txt
yyyy.txt
3、nfs任务
[root@m0 ~]# vim test004.yml
[root@m0 ~]# ansible-playbook ./test004.yml--- - hosts: 192.168.8.181 remote_user: root tasks: - name: 安装nfs-utils yum: name=nfs-utils state=present - name: 安装rpcbind yum: name=rpcbind state=present - name: 创建共享目录 file: path=/static state=directory - name: 配置文件 shell: echo '/static *(ro,sync)' > /etc/exports - name: 启动nfs service: name=nfs state=started enabled=yes - hosts: 192.168.8.182 remote_user: root tasks: - name: 安装nfs-utils yum: name=nfs-utils state=latest - name: 创建挂载目录 file: path=/nfs state=directory - name: 挂载nfs文件 command: mount -t nfs 192.168.8.181:/static /nfs
[root@s1 ~]# ls /nfs/
test