playbooks是 一个不一样于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来讲,playbook是一个很是简单的配置管理和多主机部署系统,不一样于任何已经存在的模式,可做为一个适合部署复杂应用程序的基础。Playbook能够定制配置,能够按照指定的操做步骤有序执行,支持同步和异步方式。值得注意的是playbook是经过YAML格式来进行描述定义的。linux
hosts: websrvs
hosts: 192.168.16.10 remote_user: root
tasks:
name: test connection
ping:
remote_user: root
sudo: yes
sudo_user: rootnginx
3. tasks:任务列表;格式有两种: action: module argument module: argument 建议使用 注意:shell和command模块后面跟命令,而非key=value 某任务的状态在运行后changed时,可经过‘notify’通知给相应的handlers 任务能够经过‘tags’打标签,然后可在absible-playbook命令上使用-t指定进行调用 示例: ```bash tasks: - name: disable selinux command: /sbin/setenforce 0
若是命令或者脚本的退出码不为零,可使用一下方式替代web
tasks: - name: run this command and ignore the result shell: /usr/bin/somecommand || /bin/true
或者shell
tasks: - name: run this command and ignore the result shell: /usr/bin/somecommand ignore_errors: True
YAML的语法和其余高阶语言相似而且能够简单表达清单、散列表、标量等数据结构,其结构经过空格来展现。(列表用横杆表示,键值对用冒号分割,键值对里又能够嵌套另外的键值对)
YAML文件扩展名一般为.yaml或者.yml。下面为示例
必定要对齐,只能使用空格 apache
name: wkx #冒号后有空格 age: 28 gender: male spouse: name: rlt age: 27 gender: female children: - name: dabao #横杠后有空格 age: 4 gender: femle - name: xiaobao age: 1 gender: male
ansible-playbook <filename.yaml> ... [options]
经常使用选项:
--check 只检测可能会发生的改变,但不真正执行操做
--list-hosts 列出运行任务的主机
--limit 主机列表,只针对主机列表中的主机执行
-v 显示过程 -vv -vvv 更详细
ansible-playbook file.yaml --check 只检测
Ansible-playbook file.yaml 执行
Ansible-playbook file.yaml --limit websevs 仅websevs主机执行编程
Shell脚本bash
#!/bin/bash # install apache yum -y install httpd #copy conf file cp /tmp/http.conf /etc/httpd/conf/httpd.conf #start service systemctl enable httpd systemctl start httpd
Palybook数据结构
- hosts: wensevs remote_user: root tasks: - name: install apache yum: name=httpd - name: copy conf file copy: src=/tmp/http.conf dest=/etc/httpd/conf/httpd.conf - name: start service service: name=httpd state=started enabled=yes
Handlers:是task列表,这些task与前述的task并无本质的区别,用于当关注的资源发生变化时,才会采起必定的操做
Notify:此action被用于每一个play的最后被触发,这样能够避免屡次有改变发生时每次都执行指定的操做,尽在全部变化发生后完成一次性的执行指定操做。在notify中列出的操做称为handler,也即notify中调用handler中定义的操做。
示例:
Handles和notify的使用:异步
- hosts: websrvs remote_user: root tasks: - name: add group nginx tags: user user: name=nginx state=present - name: add user nginx group: name=nginx state=present group=nginx - name: install nginx yum: name=nginx state=present - name: copy file copy: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf notify: - restart nginx - check nginx process handlers: - name: restart nginx #必须和notify中定义的名字一致 service: name=nginx status=restarted enabled=yes - check nginx process shell: killall -0 nginx > /tmp/nginx.log
Tags的使用编程语言
1. hosts: wensrvs remote_user: root tasks: 2. name: install httpd yum: name=httpd status=present 3. name: copy conf file copy: src=files/httpd.conf dest=/etc/httpd/conf/httpd.conf tags: conf 4. name: start service tags: service service: name=httpd state=started enabled=yes
变量名:仅能由字母、数字和下划线组成,且只能以字母开头
变量来源:
cat vars.yaml var1: httpd var2: nginx cat var.yaml - hosts: 192.168.16.115 remote_user: root vars_files: - vars.yaml tasks: - name: touch httpd.txt file: name={{var1}}.txt state=touch #默认建立在当前用户下 - name: touch nginx.log file: name={{var2}}.log state=touch
使用setup变量
cat var.yaml - hosts: 192.168.16.115 remote_user: root tasks: - name: create log file file: name=/root/ansible-test/{{ ansible_fqdn }} state=touch #{{ ansible_fqdn }}为setup模块中域名的一个内置变量
命令行使用变量
cat var.yaml - hosts: 192.168.16.115 remote_user: root tasks: - name: create log file file: name=/root/ansible-test/{{ pkg }} state=touch
ansible-playbook -e pkg=haha var.yaml
[websrvs:vars] #公共(组)变量 mark="-" [websrvs] #普通变量 192.168.16.115 hname=wg sname=115 192.168.16.118 hname=wg sname=118
ansible websrvs -m hostname -a 'name= {{ hname }}{{ mark }}16{{ mark }}{{sname}}'
文本文件,嵌套有脚本(使用模板编程语言编写)
Jinja2语音,使用 字面量,有下面形式
字符串:使用单引号或者双引号
数字:整数、浮点数
列表:[haha,xixi,hehe,lele]
元组:(haha,xixi,hehe,lele)
字典:{name:wkx,gender:male}
布尔型:true、false
算术运算:+、-、*、/、//、%、** 分别是加减乘除整除求余幂
比较操做:==,!=,>,<,>=,<=
逻辑运算:and,or,not
流表达式:for,if,when
cat var.yaml - hosts: 192.168.16.115 remote_user: root tasks: - name: "create file" file: name="/root/ansible-test/xixi" state=touch when: ansible_os_family == "RedHat"
- hosts: 192.168.16.115 remote_user: root tasks: - name: "create some file" file: name=/root/ansible-test/{{ item }} state=touch #建立文件,嵌套变量在下面 when: ansible_distribution_major_version == "7" #匹配条件,条件知足时执行 with_items: - haha - xixi - lele - name: install some packages yum: name={{ item }} with_items: - lrzsz - net-tools
示例2:
cat user.yaml - hosts: 192.168.16.115 remote_user: root tasks: - name: "create some group" group: name={{ item }} when: ansible_distribution_major_version == "7" with_items: - g1 - g2 - g3 - name: cerate user user: name={{ item.name }} group={{ item.group }} with_items: - { name: "user1",group: "g1"} - { name: "user2",group: "g2"} - { name: "user3",group: "g3"}
Playbook中template for if
{% for vhost in nginx_vhosts %} server{ listen {{ vhost.listen | default(‘80 default_server’)}}; {% if vhost.server_name is defined %} Server_name{{ vhost.server_name }}; {% endif %} {% if vhost.root is defined %} root {{ vhost.root }} {% endif %}
示例1:
cat temnginx.yaml --- - hosts: websrvs remote_user: root vars: nginx_hosts: - 80 - 8088 - tasks: - name: templete config template: src=/root/ansible-test/nginx.conf.j2 dest=/root/nginx.conf cat nginx.conf.j2 {% for vhost in nginx_hosts %} server { listen {{ vhost }} } {% endfor %}
生成为文件为
cat /root/nginx.conf server { listen 80 } server { listen 8088 }
示例2:
cat temnginx.yaml --- - hosts: websrvs remote_user: root vars: nginx_hosts: - web1: listen: 8080 root: "/var/www/nginx/web1/" - web2: listen: 8080 server_name: "www.wkx.com" root: "/var/www/nginx/web2/" tasks: - name: templete config template: src=/root/ansible-test/nginx.conf.j2 dest=/root/nginx.conf cat nginx.conf.j2 {% for vhost in nginx_hosts %} server { listen {{ vhost.listen }} {% if vhost.server_name is defined %} server_name {{ vhost.server_name }} {% endif %} root {{ vhost.root }} } {% endfor %}
cat zabbix-agent.yml
- hosts: agent remote_user: root tasks: - name: copy zabbix-agent copy: src=/mnt/zabbix-agent-4.4.0-1.el7.x86_64.rpm dest=/tmp - name: install zabbix-agent yum: name=/tmp/zabbix-agent-4.4.0-1.el7.x86_64.rpm - name: copy config copy: src=/etc/zabbix/zabbix_agentd.conf dest=/etc/zabbix/zabbix_agentd.conf - name: modify conf hostname lineinfile: path: /etc/zabbix/zabbix_agentd.conf regexp: "^Hostname" line: "Hostname={{ansible_hostname}}" - name: start zabbix-agent service: name=zabbix-agent state=started enabled=yes
ansible自1.2版本后引入的新特性。用于层次性、结构性的组织playbook。Roles可以根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只须要在playbook中使用include指令便可。简单来讲,roles就是经过分别将变量、文件、任务、模板以及处理器放置于单独的目录中,并能够便捷的include他们的一种机制。角色通常基于主机结构构建服务的场景中,但也能够是用于构建守护进程等场景中。
复杂场景:建议使用roles,代码复用度高
变动主机及主机组
如命名不规范维护或传承成本高
某些功能须要多个playbook,经过include实现
Ansible的roles目录结构
每一个角色,以特定的层级目录结构进行组织
Roles目录结构: playbook.yaml roles/ project/ 项目名称,有如下子目录 tasks/ 定义task、role的基本元素,至少包含一个名为main.yaml的文件;其余须要的文件在此经过include进行包含 files/ 存放由copy或者script模块调用的文件 vars/ 不经常使用,定义变量;至少包含一个名为main.yaml的文件;其余须要的文件在此经过include进行包含 default/ 不经常使用,设定默认变量时使用此目录中的main.yaml templates/ template模块查找所须要的模板问文件的目录 handlers/ meta/ 不经常使用,定义当前角色的特殊设定以及依赖关系;至少包含一个名为main.yaml的文件;其余须要的文件在此经过include进行包含