playbook
一系列模块任务的集合
---- hosts: webserremove_user: roottasks:- name: hellocommand: echo "hello"- name: create filefile: name=/data/newfile state=touch- name: create useruser: name=nobody shell=/sbin/nologin- name: install pakcage httpdyum: name=httpd state=laste# 同样的模块 不能写在一起- name: copy index.htmlcopy: src=/data/index.html dest=/var/html- name: copy main.htmlcopy: src=/data/main.html dest=/var/html
playbook 执行的相对路径是 文件所在的路径
ansible-playbook hello.yaml

handlers和notify
当notify配置的任务发生改变时 会执行notify配置的任务(可以触发多个任务 列表形式)
如: copy模块改动了配置文件 service就会被重启
---- hosts: webserremove_user: roottasks:- name: install httpdyum: name=httpd- name: copy conf filecopy: src=files/httpd.conf dest=/etc/httpd/conf backup=yesnotify: restart service- name: start serviceservice: name=httpd state=started enabled=yeshandlers:- name: restart serviceservice: name=httpd state=restarted
tags
标签 可以通过tags关键字调用任务
---- hosts: webserremove_user: roottasks:- name: install httpdyum: name=httpdtags: installhttpd- name: copy conf filecopy: src=files/httpd.conf dest=/etc/httpd/conf backup=yesnotify: restart service- name: start serviceservice: name=httpd state=started enabled=yestags: starthttpdhandlers:- name: restart serviceservice: name=httpd state=restarted
ansible-playbook -t starthttpd httpd.yaml //只执行 starthttpd 这一个标签的类容
ansible-playboot —list-tags httpd.yaml
多个任务可以共用一个标签
playbook中的变量
变量仅能由字母 数字 下划线组成 只能以字母开头
变量来源:
ansible setup facts // 系统状态信息
ansible webser -m setup -a “filter=address“ //查询带address的变量名称
palay脚本中定义变量
- hosts: appserremote_user: rootvars:- pk_name: httpdtasks:- name: install packageyum: name={{ pk_name }}- name: start serviceservice: name={{ pk_name }} state=started enabled=yes
ansible-playbook -e “pk_name= httpd” app.yml // 对playbook变量 赋值运行
playbook中 也可以对变量赋值
在/etc/ansible/hosts中 也可以定义变量
[webser]192.168.200.147 pk_name=httpd192.168.200.148 pk_name=vsftpd[webser:vars]hostname=webserver
可以针对单台主机 或者主机组 进行变量设置
主机变量优先级 高于 主机组组变量
命令行变量优先级 高于 playbook中的变量
playbook中的变量 高于 配置文件中变量
剧本调用vars文件
vars文件内容一下:
---pk_name: httpd
---- hosts: webserremote_user: rootvar_files:- vars.yamltasks:- name: install packageyum: name={{pk_name}}
templates
模板用于配置文件
- hosts: nginxserremote_user: roottasks:- name: install packageyum: name=nginx- name: copy templatetemplate: src=nginx.conf dest=/etc/nginx/nginx.confnotiyf: restart nginx- name: start nginxservice: name=nginx state=started enable=yeshandlers:- name: restart nginxservice: name=nginx state=restart
#nginx config#path templates/nginx.confuser nginx;worker_processes {{ ansible_processor_vcpus * 2 }}error_log /var/log/nginx/error.log;http{server{listen {{ http_port }}}}
when
条件判断
---- hosts: webserremote_user: roottasks:- name: install packageyum: name=nginx- name: copy template for centos7template: src=ngin7.conf dest=/etc/nginx/conf/nginx.confwhen: ansible_distribution_major_version == "7"- name: copy template for centos6template: src=nginx6.conf dest=/etc/nginx/conf/nginx.confwhen: ansible_distribution_major_version == "6"- name: start nginxservice: name=nginx state=started enable=yes
with_items循环
---- hosts: webserremote_user: roottasks:- name: create some filesfile: name=/data/{{ item }} state= touchwith_items:- file1- file2- file3- name: install some packagesyum: name={{ item }}with_items:- httpd- vsftpd- hping3
