当前位置: 首页 > news >正文

网站建设视频教程网北京建设工程信息网上报名基础信息

网站建设视频教程网,北京建设工程信息网上报名基础信息,时代定制,做彩平的材质网站Ansible playbook自动化运维工具详解 一、playbook的相关知识1.1、playbook 的简介1.2、playbook的 各部分组成 二、基础的playbook剧本编写实例三、 playbook的定义、引用变量3.1、基础变量的定义与引用3.2、引用fact信息中的变量 四、playbook中的when条件判断和变量循环使用… Ansible playbook自动化运维工具详解 一、playbook的相关知识1.1、playbook 的简介1.2、playbook的 各部分组成 二、基础的playbook剧本编写实例三、 playbook的定义、引用变量3.1、基础变量的定义与引用3.2、引用fact信息中的变量 四、playbook中的when条件判断和变量循环使用4.1、when条件判断4.2、变量循环 五、Templates 模块cd /etc/ansible #在ansible的所在目录中创建该项目的目录 mkdir apache vim apache.yamlvim nginx.yamlvim test1.yamlvim test2.yamlvim test3.yamlvim test3.yaml4.2 变量循环 1with_item 单循环输出 vim test4.yamlvim test4.yaml(2) with_list 每组列表一起循环的输出(3) with_together 同一列表位置数据组合输出的循环4 with_nested 列表数据循环匹配的循环根据列表个数定义有多少层的循环 五、Templates 模块六、Tags6.1 单标签的使用6.2、多标签的运用6.3、通用标签always的运用 一、playbook的相关知识 1.1、playbook 的简介 playbook是 一个不同于使用Ansible命令行执行方式的模式其功能更强大灵活。简单来说playbook是一个非常简单的配置管理和多主机部署系统不同于任何已经存在的模式可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置可以按照指定的操作步骤有序执行支持同步和异步方式。我们完成一个任务例如安装部署一个httpd服务我们需要多个模块一个模块也可以称之为task提供功能来完成。而playbook就是组织多个task的容器他的实质就是一个文件有着特定的组织格式它采用的语法格式是YAMLYet Another Markup Language。 1.2、playbook的 各部分组成 Tasks任务即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行Variables变量Templates模板Handlers处理器当changed状态条件满足时notify触发执行的操作Roles角色 二、基础的playbook剧本编写实例 playbook中运用的模块就是ansible中的模块就像docker-compose一样将docker操作容器的指令归纳为一个yaml文件开启运行yaml中的指令模块就能按照预设计的方向去完成。实例1playbook编写 apache的yum安装部署剧本 剧本编写实现的需求对Ansible管理的所有的webservers组的成员yum安装最新版本的apache服务软件并进行相应环境的调整确保webservers的apache服务能够正常运行并设置开机自启 cd /etc/ansible #在ansible的所在目录中创建该项目的目录 mkdir apache vim apache.yaml --- - name: apache yum applygather_facts: falsehosts: webserversremote_user: roottasks:- name: test connectionping:- name: stop firewalldservice: namefirewalld statestopped- name: stop selinuxcommand: /usr/sbin/setenforce 0ignore_errors: true- name: yum install apache serviceyum: namehttpd statelatest- name: start apache serviceservice: namehttpd statestarted enabledyes运行剧本的方法 //运行playbook ansible-playbook apache.yaml//补充参数 -k–ask-pass用来交互输入ssh密码 -K-ask-become-pass用来交互输入sudo密码 -u指定用户 ansible-playbook apache.yaml --syntax-check #检查yaml文件的语法是否正确 ansible-playbook apache.yaml --list-task #检查tasks任务 ansible-playbook apache.yaml --list-hosts #检查生效的主机 ansible-playbook apache.yaml --start-at-taskinstall httpd #指定从某个task开始运行实例2playbook编写nginx 的yum安装并且能修改其监听端口的剧本 需求通过yum安装nginx服务并且能够控制被管理的主机的服务的开启按照预设的配置在运行时的端口。在编写剧本前需要准备相应的两个文件一个为nginx的yum源。一个为相对应的主配置文件在主配置文件中修改其端口在将该配置移至被管理主机中作为运行启动时的默认配置 剧本编写 mkdir /etc/ansible/nginxvim nginx.yaml --- - name: nginx scriptgather_facts: falsehosts: webserversremote_user: roottasks:- name: test connectionping:- name: stop firewalldservice: namefirewalld statestopped enabledno- name: stop selinuxcommand: /usr/sbin/setenforce 0ignore_errors: true- name: prepare nginx repocopy: src/etc/ansible/nginx/nginx.repo dest/etc/yum.repos.d/nginx.repo- name: install nginxyum: namenginx statelatest- name: change portcopy: src/opt/default.conf dest/etc/nginx/conf.d/default.confnotify: restart nginx- name: start nginxservice: namenginx statestarted enabledyeshandlers:- name: restart nginxservice: namenginx staterestarted 运行结果 三、 playbook的定义、引用变量 3.1、基础变量的定义与引用 在yaml文件中我们可以在初始配置的模块中用var去定义变量的存在变量的格式为key:value,以此来确定该变量在剧本中的存在vim test1.yaml --- - name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: touch a test filefile: path/opt/{{filename}} statetouchansible-playbook test1.yaml3.2、引用fact信息中的变量 首先我们知道 使用 ansible 组 -m setup 可以收集该组中所有的节点信息 所以setup中fact信息有时候会剧本编写中需要而fact的信息也是可以通过变量的方式进行调用剧本编写 vim test2.yaml --- - name: this is a playbook for quote variatehosts: dbserversremote_user: roottasks:- name: reading setup fact variatedebug: msg{{ansible_date_time.weekday}} ~ 运行的结果 四、playbook中的when条件判断和变量循环使用 4.1、when条件判断 #选用filteransible_default_ipv4中的address作为when条件进行测试 ansible all -m setup -a filteransible_default_ipv4测试剧本编写 vim test3.yaml --- - name: this is when test playbookhosts: allremote_user: roottasks:- name: test whendebug: msg判断位置when: ansible_default_ipv4.address 192.168.73.107ansible-playbook test3.yaml4.2、变量循环 with_item 单循环输出vim test4.yaml --- - name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: {{item}}with_items: [a, b, c, d]ansible-playbook test4.yamlwith_list 每组列表一起循环的输出--- - name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: {{item}}with_list:- [a, b, c, d]- [1 ,2, 3, 4] ~ ~ with_together 同一列表位置数据组合输出的循环--- - name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: {{item}}with_together:- [a, b, c, d]- [1 ,2, 3, 4] ~ with_nested 列表数据循环匹配的循环根据列表个数定义有多少层的循环--- - name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: {{item}}with_nested:- [a, b, c, d]- [1 ,2, 3, 4] ~ 四种迭代循环方式的总结whith_items: {{item}}会把所有的列表展开进行遍历输出with_flattened也可以替代with_items with_list: {{item}}会把每个列表当作一个整体输出。如果每个列表中只有一个值则效果与with items一致。loop也可以替代ith with_together: {{item}}引用时会把每个列表相同位置的值对齐合并后输出 with nested:{ {item}}引用时会把每个列表的值两两组合循环输出 五、Templates 模块 linux自动化运维 同时被 2 个专栏收录 8 篇文章1 订阅 订阅专栏 Linux软件使用 10 篇文章1 订阅 订阅专栏 内容预知 目录 内容预知 1.playbook的相关知识 1.1 playbook 的简介 1.2 playbook的 各部分组成 基础的playbook剧本编写实例 实例1playbook编写 apache的yum安装部署剧本 实例2playbook编写nginx 的yum安装并且能修改其监听端口的剧本 playbook的定义、引用变量 3.1 基础变量的定义与引用 3.2 引用fact信息中的变量 playbook中的when条件判断和变量循环使用 4.1 when条件判断 4.2 变量循环 1with_item 单循环输出 (2) with_list 每组列表一起循环的输出 (3) with_together 同一列表位置数据组合输出的循环 4 with_nested 列表数据循环匹配的循环根据列表个数定义有多少层的循环 四种迭代循环方式的总结 Templates 模块 1先准备一个以 .j2 为后缀的 template 模板文件设置引用的变量 2 修改主机清单文件使用主机变量定义一个变量名相同而值不同的变量 (3) 编写 playbook Tags 6.1 单标签的使用 6.2 多标签的运用 6.3 通用标签always的运用 roles 1.playbook的相关知识 1.1 playbook 的简介 playbook是 一个不同于使用Ansible命令行执行方式的模式其功能更强大灵活。简单来说playbook是一个非常简单的配置管理和多主机部署系统不同于任何已经存在的模式可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置可以按照指定的操作步骤有序执行支持同步和异步方式。我们完成一个任务例如安装部署一个httpd服务我们需要多个模块一个模块也可以称之为task提供功能来完成。而playbook就是组织多个task的容器他的实质就是一个文件有着特定的组织格式它采用的语法格式是YAMLYet Another Markup Language。 对于YAML文件格式的编写我在之前的博客就有所介绍 【云原生】Docker-compose单机容器集群编排_站在这别动我去给你买橘子的博客-CSDN博客 https://blog.csdn.net/qq_62462797/article/details/128052363?spm1001.2014.3001.5501 1.2 playbook的 各部分组成 1Tasks任务即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行 2Variables变量 3Templates模板 4Handlers处理器当changed状态条件满足时notify触发执行的操作 5Roles角色 基础的playbook剧本编写实例 playbook中运用的模块就是ansible中的模块就像docker-compose一样将docker操作容器的指令归纳为一个yaml文件开启运行yaml中的指令模块就能按照预设计的方向去完成。 实例1playbook编写 apache的yum安装部署剧本 剧本编写实现的需求对Ansible管理的所有的webservers组的成员yum安装最新版本的apache服务软件并进行相应环境的调整确保webservers的apache服务能够正常运行并设置开机自启 cd /etc/ansible #在ansible的所在目录中创建该项目的目录 mkdir apache vim apache.yaml name: apache yum apply gather_facts: false hosts: webservers remote_user: root tasks: name: test connection ping: name: stop firewalld service: namefirewalld statestopped name: stop selinux command: ‘/usr/sbin/setenforce 0’ ignore_errors: true name: yum install apache service yum: namehttpd statelatest name: start apache service service: namehttpd statestarted enabledyes 运行剧本的方法 //运行playbook ansible-playbook apache.yaml //补充参数 -k–ask-pass用来交互输入ssh密码 -K-ask-become-pass用来交互输入sudo密码 -u指定用户 ansible-playbook apache.yaml --syntax-check #检查yaml文件的语法是否正确 ansible-playbook apache.yaml --list-task #检查tasks任务 ansible-playbook apache.yaml --list-hosts #检查生效的主机 ansible-playbook apache.yaml --start-at-task‘install httpd’ #指定从某个task开始运行 实例2playbook编写nginx 的yum安装并且能修改其监听端口的剧本 需求通过yum安装nginx服务并且能够控制被管理的主机的服务的开启按照预设的配置在运行时的端口。 在编写剧本前需要准备相应的两个文件一个为nginx的yum源。一个为相对应的主配置文件在主配置文件中修改其端口在将该配置移至被管理主机中作为运行启动时的默认配置 剧本编写 mkdir /etc/ansible/nginx vim nginx.yaml name: nginx script gather_facts: false hosts: webservers remote_user: root tasks: name: test connection ping: name: stop firewalld service: namefirewalld statestopped enabledno name: stop selinux command: ‘/usr/sbin/setenforce 0’ ignore_errors: true name: prepare nginx repo copy: src/etc/ansible/nginx/nginx.repo dest/etc/yum.repos.d/nginx.repo name: install nginx yum: namenginx statelatest name: change port copy: src/opt/default.conf dest/etc/nginx/conf.d/default.conf notify: “restart nginx” name: start nginx service: namenginx statestarted enabledyes handlers: name: restart nginx service: namenginx staterestarted 运行结果 playbook的定义、引用变量 3.1 基础变量的定义与引用 在yaml文件中我们可以在初始配置的模块中用var去定义变量的存在变量的格式为key:value,以此来确定该变量在剧本中的存在 vim test1.yaml name: this is a play for testing variables hosts: dbservers remote_user: root vars: filename: abc.txt tasks: name: touch a test file file: path/opt/{{filename}} statetouch ansible-playbook test1.yaml 3.2 引用fact信息中的变量 首先我们知道 使用 ansible 组 -m setup 可以收集该组中所有的节点信息 所以setup中fact’信息有时候会剧本编写中需要而fact的信息也是可以通过变量的方式进行调用 剧本编写 vim test2.yaml name: this is a playbook for quote variate hosts: dbservers remote_user: root tasks: name: reading setup fact variate debug: msg{{ansible_date_time.weekday}} ~ 运行的结果 playbook中的when条件判断和变量循环使用 4.1 when条件判断 #选用filteransible_default_ipv4中的address作为when条件进行测试 ansible all -m setup -a ‘filteransible_default_ipv4’ 测试剧本编写 vim test3.yaml name: this is when test playbook hosts: all remote_user: root tasks: name: test when debug: msg‘判断位置’ when: ansible_default_ipv4.address “192.168.73.107” ansible-playbook test3.yaml 除此之外 when条件还可以通过 不等于条件来进行判断 vim test3.yaml name: this is when test playbook hosts: all remote_user: root tasks: name: test when debug: msg‘判断位置’ when: ansible_default_ipv4.address ! “192.168.73.107” ansible-playbook test3.yaml 4.2 变量循环 1with_item 单循环输出 vim test4.yaml name: item test hosts: dbservers remote_user: root gather_facts: no tasks: debug: msg: “{{item}}” with_items: [a, b, c, d] ansible-playbook test4.yaml 当列表为两个时。with_item的输出方式 vim test4.yaml name: item test hosts: dbservers remote_user: root gather_facts: no tasks: debug: msg: “{{item}}” with_items: [a, b, c, d][1 ,2, 3, 4] ansible-playbook test4.yaml (2) with_list 每组列表一起循环的输出 name: item test hosts: dbservers remote_user: root gather_facts: no tasks: debug: msg: “{{item}}” with_list: [a, b, c, d][1 ,2, 3, 4] ~ ~ (3) with_together 同一列表位置数据组合输出的循环 name: item test hosts: dbservers remote_user: root gather_facts: no tasks: debug: msg: “{{item}}” with_together: [a, b, c, d][1 ,2, 3, 4] ~ name: item test hosts: dbservers remote_user: root gather_facts: no tasks: debug: msg: “{{item}}” with_together: [a, b, c, d][1 ,2, 3, 4][A, B, C] 4 with_nested 列表数据循环匹配的循环根据列表个数定义有多少层的循环 name: item test hosts: dbservers remote_user: root gather_facts: no tasks: debug: msg: “{{item}}” with_nested: [a, b, c, d][1 ,2, 3, 4] ~ 四种迭代循环方式的总结 whith_items: {{item}}会把所有的列表展开进行遍历输出with_flattened也可以替代with_items with_list: {{item}}会把每个列表当作一个整体输出。如果每个列表中只有一个值则效果与with items一致。loop也可以替代ith with_together: {{item}}引用时会把每个列表相同位置的值对齐合并后输出 with nested:{ {item}}引用时会把每个列表的值两两组合循环输出 五、Templates 模块 Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件可以看作是一个编译过的模板文件用来产生目标文本传递Python的变量给模板去替换模板中的标记。 本次我们以改变apche的配置文件为例来展现Templates模块的运用 先准备一个以 .j2 为后缀的 template 模板文件设置引用的变量 #如果没有相关的httpd的配置文件可以先yum按住一个httpd的服务取其主配置文件 cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2vim /opt/httpd.conf.j2 Listen {{http_port}} #42行修改 ServerName {{server_name}} #95行修改 DocumentRoot {{root_dir}} #119行修改修改主机清单文件使用主机变量定义一个变量名相同而值不同的变量vim /etc/ansible/hosts [webservers] 192.168.73.106 http_port192.168.73.106:80 server_namewww.test1.com:80 root_dir/etc/httpd/htdocs[dbservers] 192.168.73.107 http_port192.168.73.107:80 server_namewww.test2.com:80 root_dir/etc/httpd/htdocs此外如果没有做DNS解析域名还需要对主机名进行映射 vim /etc/hosts192.168.73.106 www.test1.com 192.168.73.107 www.test2.com编写 playbook mkdir /etc/ansible/templates vim apache.yaml --- - hosts: allremote_user: rootvars:- package: httpd- service: httpdtasks:- name: install httpd packageyum: name{{package}} statelatest- name: install configure filetemplate: src/opt/httpd.conf.j2 dest/etc/httpd/conf/httpd.confnotify:- restart httpd- name: create root dirfile: path/etc/httpd/htdocs statedirectory- name: start httpd serverservice: name{{service}} enabledtrue statestartedhandlers:- name: restart httpdservice: name{{service}} staterestartedansiable-playbook apache.yaml六、Tags 可以在一个playbook中为某个或某些任务定义“标签”在执行此playbook时通过ansible-playbook命令使用–tags选项能实现仅运行指定的tasks。playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时无论执行哪一个tags时定义有always的tags都会执行。 6.1 单标签的使用 vim test1.yaml --- - name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: position 1debug:msg: ls /opttags:- only- name: position 2debug:msg: ls /mntansible-playbook test1.yaml --tagsonly6.2、多标签的运用 --- - name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: position 1debug:msg: 测试标签1tags:- one- name: position 2debug:msg: 测试标签2tags:- two- name: position 3debug:msg: 测试标签3tags:- one执行结果 6.3、通用标签always的运用 --- - name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: position 1debug:msg: 测试标签1tags:- one- name: position 2debug:msg: 测试通用标签alwaystags:- always- name: position 3debug:msg: 测试标签3tags:- one 执行结果
http://www.hkea.cn/news/14475823/

相关文章:

  • seo网站打开慢郫都区网站建设
  • qq头像网站源码怎么查网站备案号
  • 上海建筑建材业网站迁移led 网站模板
  • 有一套源码做网站还差什么江苏网站建设方案
  • 手机网站自助建网站开发流程有哪些
  • 西安制作手机网站神农架网页设计
  • 做外贸网站维护费是多少开网店卖什么适合新手
  • 网站维护费怎么做分录设计网站的关键点
  • 做芯片哪个网站推广湖南企业建网站
  • 个人微信号做网站行吗襄阳建设网站
  • 网站下载免费软件安装南京高端网站建设公司
  • 凡科免费个人做网站有弊吗做夜夜做网站
  • 自己建设手机网站首页网络热词大全
  • 公司没有自己的网站网站维护中一般要多长时间
  • 廉政建设网站动画制作设计
  • 广西 网站建设wordpress短消息
  • 公司网站百度搜索的描述怎么做怎么做网站赚钱软件
  • 自建网站平台哪个好莱芜信息港房产网
  • 网站建设管理系统免费网站o2o网站开发方案
  • 网站开发维护的好处上海网络营销有限公司
  • 怎样自己建立一个网站烟台网站建设加企汇互联专业
  • 企业建站原因saas系统是什么意思
  • 合肥专业网站制网站开发引发的官司
  • 建设心理网站的背景汕头seo排名收费
  • 龙岗网站建设公司效果青岛市建设工程管理信息网
  • 高仿做的好点的网站职业生涯规划
  • 湖北勘察设计协会网站福建 专业网站建设公司
  • wordpress做一个视频网站泰州建设信息网站
  • 外贸品牌网站设计公司要网站开发费用短信
  • 一家只做性价比的网站wordpress分类置顶