网站风格趋势,做网站闵行,福利窝又一个wordpress,电脑培训网playbook的组成部分
1、 tasks#xff1a;任务
在目标主机上需要执行的操作。使用模块定义这些操作。每个任务都是一个模块的调用。
2、 variables#xff1a;变量
用于存储和传递数据。类似于shell脚本中的变量。变量可以自定义。可以在playbook当中定义为全局变量…playbook的组成部分
1、 tasks任务
在目标主机上需要执行的操作。使用模块定义这些操作。每个任务都是一个模块的调用。
2、 variables变量
用于存储和传递数据。类似于shell脚本中的变量。变量可以自定义。可以在playbook当中定义为全局变量也可以外部传参。类似于shell脚本中的位置变量
3、 Templates模板
用于生成配置文件。模板是包含占位符的文件。占位符由ansible在执行时转化为变量值。
4、 handlers处理器
当需要有变更的时候可以执行触发器。
5、 Roles角色
类似于dockercompose。是一种组织和封装playbook的。允许把相关的任务、变量、模板和处理器组织成一个可复用的单元。
实例模板1
vim test1.yml
#this is our first playbook
- name: first play
#相当于任务描述。一个name就是一个任务名。可以为空可以不写。gather_facts: false
#是否收集主机的相关信息。如果不写默认收集
#false:表示不检查。可以加快playbook的执行速度hosts: 20.0.0.20
#声明目标主机是谁。可以使用IP或者组名remote_user: root
#在目标主机执行的用户tasks:
#声明需要执行的任务。可以理解为大任务中的小任务- name: ping testping:- name: close selinuxcommand: /sbin/setenforce 0ignore_errors: True
#如果出现错误则忽略- name: close firewalldservice: namefirewalld statestopped- name: install httpdyum: namehttpd
#statelatest:声明服务的版本。可以不加- name: start httpdservice: enabledtrue namehttpd statestarted- name: editon index.html
#修改httpd服务的默认的访问页面shell: echo this is httpd /var/www/html/index.htmlnotify: restart httpd
#notify:表示需要变更。交给handlers处理重启httpd服务handlers:- name: restart httpdservice: namehttpd staterestarted 检查yml文件的语法是否正确
ansible-playbook test1.yml --syntax-check
#检查yml文件的语法是否正确
检查有多少任务
ansible-playbook test.yaml --list-task
检查有多少主机生效
ansible-playbook test1.yml --list-hosts
#检查生效的目标主机
指定剧本
ansible-playbook test1.yml --start-at-taskinstall httpd
#指定剧本从哪个任务开始执行
切换用户
#this is our first playbook
- name: first play
#相当于任务描述。一个name就是一个任务名。可以为空可以不写。gather_facts: false
#是否收集主机的相关信息。如果不写默认收集
#false:表示不检查。可以加快playbook的执行速度hosts: 20.0.0.20
#声明目标主机是谁。可以使用IP或者组名remote_user: zyg
#在目标主机执行的用户become:yesbecome_user:root#表示需要切换用户切换用户的名称tasks:
#声明需要执行的任务。可以理解为大任务中的小任务- name: ping testping:- name: close selinuxcommand: /sbin/setenforce 0ignore_errors: True
#如果出现错误则忽略- name: close firewalldservice: namefirewalld statestopped- name: install httpdyum: namehttpd
#statelatest:声明服务的版本。可以不加- name: start httpdservice: enabledtrue namehttpd statestarted- name: editon index.html
#修改httpd服务的默认的访问页面shell: echo this is httpd /var/www/html/index.htmlnotify: restart httpd
#notify:表示需要变更。交给handlers处理重启httpd服务handlers:- name: restart httpdservice: namehttpd staterestartedbecome:yes
become_user:root
vim /etc/ansible/ansible.cfg
取消17行的注释
ansible-play test1.yml -K
#表示输入密码
#-K内部声明更改用户使用-K
指定用户操作
ansible-playbook test1.yml -u root -k
#如果没有声明更改用户可以在外部指定用户
#-u指定用户
#-k手动输入密码
实例模板2
声明和引用变量以及外部传参变量
#this is second playbook!
#声明和引用变量以及外部传参变量
- hosts: 20.0.0.20remote_user: rootvars:groupname: zygusername: hmbbtasks:- name: create groupgroup:name: {{ groupname }}
#引用前面设定好的groupnamesystem: yesgid: 111- name: create useruser:name: {{ username }}uid: 1011group: {{ groupname }}shell: /sbin/nologin- name: copy filecopy:content: {{ hostvars[inventory_hostname][ansible_default_ipv4][adress]}}dest: /opt/bqb.txt
字典方式 vars:groupname: zygusername: hmbb
使用的是key-value的方式
使用符号-开头表示这是一个列表
表示包含所有主机变量的字典 {{ hostvars[inventory_hostname][ansible_default_ipv4][adress] }}#表示包含所有主机变量的字典#inventory_hostname目标主机的主机名#[ansible_default_ipv4][adress]获取目标主机名#表示获取目标主机的IP地址复制到目标文件里
外部传参
ansible-playbook test2.yml -e usernameyst groupnameymr
#外部传参
playbook的条件判断
when比较常见的应用场景实现满足条件即执行不满足条件即跳过的任务
#this is when test
- hosts: all
#可以用主机的IP地址也可以使用组名remote_user: roottasks:- name: test whendebug:msg: 位置判断
#相当于shell脚本中的echo。满足条件就会打印。不满足则不打印
#msg:表示输出的内容when: ansible_default_ipv4.address 20.0.0.30playbook当中的循环
ansible有多种循环格式。with_items循环遍历
- hosts: 20.0.0.30remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_items:- [a,b,c,d]- [1,2,3,4]
#声明变量是item。playbook的内置变量with_items,会把itme的值。遍历列表当中的a,b,c,d.
#虽然声明的列表是两个但是with_items还是把两个列表当成整体进行遍历。 分组打印列表
- hosts: 20.0.0.30remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_list:- [a,b,c,d]- [1,2,3,4]
#with_list分组打印 遍历循环在主机上创建目录
- hosts: 20.0.0.30remote_user: rootgather_facts: falsetasks:- name: create filefile:path: {{ item }}state: touchwith_items:- [/opt/a,/opt/b,/opt/c,/opt/d]- [/opt/1,/opt/2,/opt/3,/opt/4]方法2
- hosts: 20.0.0.30remote_user: rootgather_facts: falsetasks:- name: create filefile:path: {{ item }}state: touchwith_list:- /opt/a- /opt/b- /opt/c- /opt/d- /opt/1- /opt/2- /opt/3- /opt/4
同一列的数据组合输出
- hosts: 20.0.0.30remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_together:- [a,b,c,d]- [1,2,3,4]
#with_together组循环列表当中的值一一对应打印出来。
#适用于组合搭配
根据列表数据循环匹配
- hosts: 20.0.0.30remote_user: rootgather_facts: falsetasks:- debug:msg: {{ item }}with_nested:- [a,b,c,d]- [1,2,3,4]#第一层列表里面的元素定义了循环的次数
#第二层相当于内循环
四种循环方式
with_items
#最常用
with_list
#列表分组循环
with_together
#列表对应的列数据结合的方式循环
with_nested
#相当于双重循环第一层定义了循环次数第二层表示第一层的每个元素会循环几次#这些都是单循环
Tenplates模块
jinja模板架构通过模板可以实现向模块文件传参(python转义)把占位符参数传到配置文件中去。
jinja的作用生成一个目标文本文件传递变量到需要配置文件当中。
实验架构
vim /etc/httpd/conf/httpd.conf
#配置占位符。可以理解为声明的变量
vim /etc/ansible/hosts
#配置主机的占位符名称和j2文件中的占位符一致。可以理解为定义参数占位符的参数声明好
vim /opt/httpd.yml
#在playbook当中使用template模块来把参数传给目标主机的配置文件。
- hosts: allremote_user: rootvars:- package: httpd- service: httpdtasks:- name: install httpdyum: name{{package}}- name: install configure filetemplate: src/opt/httpd.conf.j2 dest/etc/httpd/conf
#使用template的模板notify:- restart httpd- name: create root_dirfile:path: /etc/httpd/htdocsstate: directory- name: start httpdservice: name{{service}} enabledtrue statestartedhandlers:- name: restart httpdservice: name{{service}} starterestarted
#通过占位符和python的转义复制到目标文件中
1、 先配置占位符。可以理解为声明的变量 2、 再配置主机的占位符名称和j2文件中的占位符一致。可以理解为定义参数占位符的参数声明好 3、 最后在playbook当中使用template模块来把参数传给目标主机的配置文件。
外部传参远程修改nginx的配置文件
- hosts: allremote_user: rootvars:- package: nginx- service: nginxtasks:- name: install nginxyum: name{{package}}- name: install configure filetemplate: src/opt/nginx.conf.j2 dest/etc/nginx/nginx.confnotify:- restart nginx- name: create root_dirfile:path: /opt/nginx/html/state: directory- name: start nginxservice: name{{service}} enabledtrue statestartedhandlers:- name: restart nginxservice: name{{service}} staterestarted
tags模块
标签模块可以在playbook当中为任务设定标签。我们在运行playbook时可以通过指定任务标签来实现只允许设定的标签任务。
-nametags:debug:--tags debug--start-at-taskwdf
任务标签的种类
always不管你是否制定了允许标签任务都会执行。
never不管是否运行了指定标签该任务也不会执行。
debug调试任务
setup手机主机信息
还可以自定义标签
per_tasks指定标签之前的任务
post_tasks运行指定标签之后的任务
- hosts: allremote_user: roottasks:- name: tag debugdebug:msg: this is demo1tags:- debug
#声明项目的名称- name: tag setupsetup:tags:- setup- name: tag alwaysdebug:msg: runtags:- always- name: tag neverdebug:msg: never runtags:- neveransible-playbook demo1.yml --tagsdebug
#运行指定标签debug
#如果指定标签never也可以运行
#只有always是一直在运行
Roles模块
角色模块每个主题就是一个角色
在ansible当中层次化结构化的组织playbook。使用了Rolse(角色)。可以更具层次结构自动装载变量文件task以及handlers等等
roles分辨把变量文件任务模块以及处理器放在单独的目录当中使用rolse模块来一键调用这些文件。类似于Dockercompose
roles的结构图
----web-----总目录角色
子目录
files存放copy和script模块调用的文件
templates存放j2的模板文件
tasks包含任务的目录---子目录下---main.yml。定义角色运行的任务
handlers包含处理器的目录--子目录下--main.yml。
vars存放角色需要引用变量的目录--子目录下--main.yml。
defaults包含默认变量的目录---子目录下---main.yml。
meta包含元信息的目录---子目录下--main.yml。
site.yml统筹调用所有的配置文件。
实验搭建
三个服务 http mysql php
cd /etc/ansible/roles/
mkdir httpd mysql phpvim httpd/tasks/main.yml
#配置httpd
- name: install httpdyum: name{{pkg}}
- name: start httpdservice: enabledtrue name{{svc}} statestartedvim httpd/vars/main.yml
#配置http服务的名称进行外部传参
pkg: httpd
svc: httpdvim mysql/tasks/main.yml
- name: install mysqlyum: name{{pkg}}
- name: start mysqlservice: enabledtrue name{{svc}} statestartedvim mysql/vars/main.yml
pkg:- mariadb- mariadb-server
svc: mariadbvim php/tasks/main.yml
- name: install phpyum: name{{pkg}}
- name: start php-fpmservice: enabledtrue name{{svc}} statestartedvim php/vars/main.yml
pkg:- php- php-fpm
svc: php-fpmvim sit.yml
#定义总控制
- hosts: 20.0.0.20remote_user: rootroles:- httpd- mysql- php