一、playbooks的组成
playbooks 本身由以下各部分组成
1.Tasks:任务,即通过task 调用 ansible 的模板将多个操作组织在一个 playbook中运行
2.Variables:变量
3.Templates:模板
4.Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
5.Roles:角色
二、示例
vim test1.yaml --- #yaml文件以---开头,以表明这是一个yaml文件,可省略
- name: first play #定义一个play的名称,可省略 gather_facts: false #设置不进行facts信息收集,这可以加快执行速度,可省略 hosts: webservers #指定要执行任务的被管理主机组,如多个主机组用冒号分隔 remote_user: root #指定被管理主机上执行任务的用户 tasks: #定义任务列表,任务列表中的各任务按次序逐个在hosts中指定的主机上执行
- name: test connection #自定义任务名称 ping: #使用 module: [options] 格式来定义一个任务
- name: disable selinux command: '/sbin/setenforce 0' #command模块和shell模块无需使用key=value格式 ignore_errors: True #如执行命令的返回值不为0,就会报错,tasks停止,可使用ignore_errors忽略失败的任务
- name: disable firewalld service: name=firewalld state=stopped #使用 module: options 格式来定义任务,option使用key=value格式
- name: install httpd yum: name=httpd state=latest
- name: install configuration file for httpd copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf #这里需要一个事先准备好的/opt/httpd.conf文件 notify: "restart httpd" #如以上操作后为changed的状态时,会通过notify指定的名称触发对应名称的handlers操作
- name: start httpd service service: enabled=true name=httpd state=started handlers: #handlers中定义的就是任务,此处handlers中的任务使用的是service模块
- name: restart httpd #notify和handlers中任务的名称必须一致 service: name=httpd state=restarted ##Ansible在执行完某个任务之后并不会立即去执行对应的handler,而是在当前play中所有普通任务都执行完后再去执行handler,这样的好处是可以多次触发notify,但最后只执行一次对应的handler,从而避免多次重启。
三、运行playbook
ansible-playbook test1.yaml
//补充参数:
-k(–ask-pass):用来交互输入ssh密码
-K(-ask-become-pass):用来交互输入sudo密码
-u:指定用户
ansible-playbook test1.yaml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook test1.yaml --list-task #检查tasks任务
ansible-playbook test1.yaml --list-hosts #检查生效的主机
ansible-playbook test1.yaml --start-at-task='install httpd' #指定从某个task开始运行
3.1 实验用playbook完成lnmp
1.准备实验环境
systemctl stop firewalld
setenforce 0
2.安装Ansible
//管理端安装 ansible
yum install -y epel-release //先安装 epel 源
yum install -y ansible
//配置主机清单
cd /etc/ansible
vim hosts
[webservers] #配置组名
192.168.100.20 #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)
[dbservers]
192.168.100.30
//配置密钥对验证
ssh-keygen -t rsa #一路回车,使用免密登录
sshpass -p '123456' ssh-copy-id root@192.168.100.20
sshpass -p '123456' ssh-copy-id root@192.168.100.30
3.用playbook安装nginx
---
- name: nginx
gather_facts: false
hosts: webservers
remote_user: root
tasks:
- name: test connection
ping:
- name: disable firewalld
service: name=firewalld state=stopped enabled=no
- name: disable selinux
command: '/usr/sbin/setenforce 0'
ignore_errors: true
- name: install nginx
yum: name=nginx state=latest
- name: start nginx
service: name=nginx state=started enabled=yes
4.用playbook安装mysql
- name: mysql
hosts: webservers
remote_user: root
gather_facts: false
tasks:
- name: wget
shell: wget https://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm
- name: install mysql
shell: rpm -ivh mysql57-community-release-el7-11.noarch.rpm
- name: mysql-communtiy
shell: sed -i 's/gpgcheck=1/gpgcheck=0/' /etc/yum.repos.d/mysql-community.repo
- name: mysql-server
yum: name=mysql-server state=latest
- name: start mysqld
service: name=mysqld state=started enabled=yes
- name: passwd
script: /test/1.sh
5.用playbook安装php
---
- name: php
gather_facts: false
hosts: webservers
remote_user: root
tasks:
- include: init.yaml
- name: yum php
shell: yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache
- name: start php
service: name=php-fpm state=started enabled=yes
- name: nginx.conf
copy: src=/test/default.conf dest=/etc/nginx/conf.d/default.conf
notify:
- restart nginx
- name: index.php
copy: src=/test/index.php dest=/usr/share/nginx/html/index.php
handlers:
- name: restart nginx
service: name=nginx state=restarted
3.2 定义、引用变量名
- name: second play
hosts: dbservers
remote_user: root
vars: #定义变量
- groupname: mysql #格式为 key: value
- username: nginx
tasks:
- name: create group
group: name={{groupname}} system=yes gid=306 #使用 {{key}} 引用变量的值
- name: create user
user: name={{username}} uid=306 group={{groupname}}
- name: copy file
copy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt #在setup模块中可以获取facts变量信息
ansible-playbook test1.yaml -e "username=nginx" #在命令行里定义变量
- name: fourth play
hosts: dbservers
remote_user: root
vars:
- filename: abc.txt
tasks:
- name: create file
file: path=/opt/"{{filename}}" state=touch
将本地的文件copy给那个组的主机
- name: fourth play
hosts: dbservers
remote_user: root
vars:
- filename: abc.txt
tasks:
- name: create file
file: path=/opt/"{{filename}}" state=touch
- name: ansible_default_ipv4
copy: content={{ansible_default_ipv4}} dest=/opt/ipv4.txt