Ansible之Playbook详解
1.Playbook详解
playbook是一个非常简单的配置管理和多主机部署系统,可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式.
核心元素
Hosts:主机
Tasks:任务,由模板定义的操作列表
Variables:变量
Templates:模板,即使用模板语法的文件
Handlers:处理器,当某条件满足时,触发执行的操作
Roles:角色
cat test.yaml - hosts: all remote_user: root tasks: - name: install redis yum: name=redis state=latest - name: copy config file copy: src=/root/playbook/redis.conf dest=/etc/redis.conf owner=redis notify: restart redis tags: ChangeConfigFile - name: start redis service: name=redis state=started handlers: - name: restart redis service: name=redis state=restarted # 检查yaml文件的语法是否正确 ansible-playbook test.yaml --syntax-check # 检查tasks任务 ansible-playbook test.yaml --list-task # 检查生效的主机 ansible-playbook test.yaml --list-hosts # 干跑一遍 ansible-playbook -C test.yaml # 指定从某个task开始运行 ansible-playbook test.yaml --start-at-task='Copy Nginx.conf' ansible-playbook test.yaml -t ChangeConfigFile
Handlers:如果触发了指定条件,则notify就会通知handlers执行对应操作.
2.引入变量
# 引用变量,收集主机facts变量 ansible-doc -s setup ansible 10.0.0.51 -m setup 直接引用Ansible变量 - hosts: all remote_user: root tasks: - name: copy file copy: content={{ ansible_env }} dest=/opt/ansibel_env.txt 自定义变量 - hosts: all remote_user: root tasks: - name: install package {{ pkgname }} yum: name={{ pkgname }} state=latest ansible-playbook -e pkgname=memcache -C forth.yaml 引用主机变量,在组的主机后面添加变量 [webservers] 10.0.0.51 ansibel_ssh_port=9122 ansibel_ssh_user=lixiang ansibel_ssh_pass=lixiang 10.0.0.52 第二种方式 [webservers:vars] http_port=8080 三种调用方式示例 - hosts: webservers remote_user: root vars: - pbvar: playbook Var test tasks: - name: command line var copy: content={{ cmdvar }} dest=/tmp/cmd.var - name: playbook var copy: content={{ pbvar }} dest=/tmp/pb.var - name: host iventory var copy: content={{ http_port }} dest=/tmp/host.var # cmdvar传值,中间有空格,会不识别 ansible-playbook -e cmdvar="command line var" vars.yaml
3.Templates介绍
# redis.conf.j2这个文件是从一个redis文件拷贝而来,修改了bind这一行 head /root/playbook/redis.conf.j2 bind {{ ansibel_eth0.ipv4.address4}} cat templete.yaml - hosts: webservers remote_user: root tasks: - name: templete config file templete: src=/root/playbook/redis.conf.j2 dest=/tmp/redis.conf
4.条件判断
条件测试-when语句: tasks: - name: install conf file to centos7 templete: src=/path/nginx.conf.c7.j2 dest=/etc/nginx/conf.d/nginx.conf when: ansible_distribution_major_version == "7" - name: install conf file to centos6 templete: src=/path/nginx.conf.c6.j2 dest=/etc/nginx/conf.d/nginx.conf when: ansible_distribution_major_version == "6" 多条件判断 tasks: - name: install conf file to centos7 templete: src=/path/nginx.conf.c7.j2 dest=/etc/nginx/conf.d/nginx.conf when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "7" 组合条件判断 tasks: - name: install conf file to centos7 templete: src=/path/nginx.conf.c7.j2 dest=/etc/nginx/conf.d/nginx.conf when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "7" ) or (ansible_distribution == "RedHat" and ansible_distribution_major_version == "7" ) 迭代:有需要循环执行任务时,可以使用迭代机制 cat iter.yaml - hosts: webservers remote_user: root tasks: - name: install {{ item }} package yum: name={{ item }} state=latest with_item: - nginx - tomcat - mariadb-server - redis
5.角色(roles)
每个角色,以特定的层级目录结构进行组织.
mysql/
files/:存放由copy或script等模块调用的文件;
templetes/:templete模块查找所需要模板文件的目录;
tasks/:至少应该包含一个名为main.yml的文件,其它的文件需要在此文件中通过include进行包含;
handlers/:至少应该包含一个名为main.yml的文件,其它的文件需要在此文件中通过include进行包含;
vars/:至少应该包含一个名为main.yml的文件,其它的文件需要在此文件中通过include进行包含;
meta/:至少应该包含一个名为main.yml的文件,定义当前角色的特殊设定及其依赖关系,其它的文件需要在此文件中通过include进行包含;
default/:设定默认变量时使用此目录中的main.yaml文件.
# 配置文件中指定了roles路径 roles_path=/etc/ansible/roles mkdir -pv /etc/ansible/roles/nginx/{files,templetes,tasks,vars,handlers,meta,default} cat /etc/ansible/roles/nginx/tasks/main.yml - name: install nginx yum: name=nginx state=latest when: ansible_os_family == "RedHat" - name: install conf templete: src=vhost1.conf.j2 dest=/etc/nginx/conf.d/vhost1.conf tags: conf notify: restart nginx - name: create site home-directory file: path={{ ngxhomedir }} state=directory - name: create index page copy: src=index.html dest={{ ngxhomedir }}/ - name: start nginx service: name=nginx state=started # 定义变量时,不用加横线 cat /etc/ansible/roles/nginx/vars/main.yml ngxhomedir: /ngxdata/vhost1 cat /etc/ansible/roles/nginx/files/index.yml <h1>Vhost1</h1> cat /etc/ansible/roles/nginx/handlers/main.yml - name: restart nginx service: name=nginx state=restarted cat nginx.yml - hosts: webservers remote_user: root roles: - nginx cat /etc/ansible/roles/nginx/templetes/vhost1.conf.j2 server{ listen 8080; server_name {{ ansibel_fqdn}}; location / { root "/ngxdata/vhost1"; } }
参考博客:https://blog.51cto.com/13630803/2154192