79、ansible-----playbook2

news2024/9/22 23:30:34

1、作业


[root@test1 opt]# vim test2.yaml 

- name: this is mulu
  hosts: 192.168.168.22
  gather_facts: false
  vars:            ##定义变量test
    test:
      - /opt/test1    ##对变量进行赋值
      - /opt/test2
      - /opt/test3
      - /opt/test4
  tasks:
    - name: create mulu
      file:
        path: "{{item}}"
        state: directory
      with_items: "{{test}}"


- name: test2
  hosts: 192.168.168.22
  gather_facts: false
  tasks:
    - name: create mulu
      file:
        path: "{{item}}"
        state: directory
      with_items: [/opt/test1,/opt/test2,/opt/test3,/opt/test4]




[root@test1 opt]# ansible-playbook test2.yaml 

2、tags模块 可以给任务定义标签,可以根据标签来运行指定的任务

[root@test1 opt]# vim test3.yaml

#标签的类型:
#always:设定了标签名为always,除非指定跳过这个标签,否则该任务将始终运行,即使指
定了标签还会运行
#never:始终不允许的任务,指定标签名,never可以运行。
#debug:用于调试
#setup:收集主机的信息
#标签名也可以自定义:tags
- hosts: 192.168.168.22
  gather_facts: false
  tasks:
    - name: debug-test1
      debug:
        msg: "cow"
      tags:
        - debug
    - name: always-test1
      debug:
        msg: "always-run"   ##不指定也会运行
      tags:
        - always
    - name: setup-test1
      debug:
        msg: "setup"
      tags:
        - setup
    - name: never-test1  ##除非指定,否则不运行
      debug:
        msg: "never-run"
      tags:
        - never

[root@test1 opt]# ansible-playbook test3.yaml --tags=never

[root@test1 opt]# ansible-playbook test3.yaml --skip-tags=always



在这里插入图片描述

在这里插入图片描述

[root@test1 opt]# ansible-playbook test3.yaml --tags="debug","setup"

在这里插入图片描述

[root@test1 opt]# ansible-playbook test3.yaml --tags="debug","setup" --skip-tags=always

在这里插入图片描述

3、自定义标签

[root@test1 opt]# vim test4.yaml

- hosts: 192.168.168.22
  gather_facts: false
  remote_user: root
  tasks:
    - name: fuzhiwenjian
      copy: src=/etc/hosts dest=/opt/hosts
      tags:
        - zlm
    - name: touch file
      file: path=/opt/test1 state=touch
      tags:
        hpc


[root@test1 opt]# ansible-playbook test4.yaml --tags=hpc  ##指定标签执行

在这里插入图片描述

[root@test1 opt]# yum -y install httpd

[root@test1 opt]# cd /etc/httpd/conf
[root@test1 conf]# ls
httpd.conf  magic
[root@test1 conf]# cp httpd.conf /opt/httpd.conf.j2
[root@test1 conf]# 
[root@test1 conf]# cd /opt/
[root@test1 opt]# ls
123  345  httpd.conf.j2  test2.yaml  test3.yaml  test4.yaml


[root@test1 opt]# vim test5.yaml

#模板,对应用的配置文件初始化:templates模块,jinja组件,把编译过的模板文件传送给
目标文件。
- hosts: 192.168.168.22
  gather_facts: false
  remote_user: root
  vars:
    - pg: httpd
    - sv: httpd
  tasks:
    - name: install httpd
      yum: name={{pg}}
    - name: editon conf
      template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
      notify:
        - restart httpd
  handlers:
    - name: restart httpd
      service: name={{sv}} state=restarted

[root@test1 opt]# vim httpd.conf.j2 

 42 Listen {{http_port}}

 95 ServerName {{server_name}}

119 DocumentRoot "{{root_dir}}"

[root@test1 ansible]# vim hosts 


[web]
## alpha.example.org
## beta.example.org
192.168.168.22 http_port=192.168.168.22:80 server_name=www.xy.com:80 root_dir=/etc/httpd/htdocs
192.168.168.23 ansible_port=22 ansible_user=root ansible_password=123

[root@test2 httpd]# mkdir htdocs

[root@test2 httpd]# yum -y remove httpd

[root@test1 opt]# ansible-playbook test5.yaml
[root@test2 httpd]# netstat -antp | grep httpd
[root@test2 httpd]# netstat -antp | grep 80
[root@test2 httpd]# curl 192.168.168.22
[root@test2 htdocs]# cd /var/www/html/
[root@test2 html]# ls
index.html
[root@test2 html]# ls
index.html
[root@test2 html]# cat index.html 
this is httpd
[root@test2 html]# find / -type f -name index.html
/usr/share/httpd/noindex/index.html
[root@test2 html]# cd /usr/share/httpd/noindex/
[root@test2 noindex]# ls
css  images  index.html
[root@test2 noindex]# cat index.html 
[root@test2 noindex]# echo 123 > index.html 
[root@test2 noindex]# curl 192.168.168.22
123

nginx 传参的方式,端口8080 servername:www.xy.com

4、nsible为了层次化,结构化的组织playbook,使用roles(角色)通过层次化自动装载变量,任务和处理器等等。

roles把变量,任务和模块的文件单独放在各个不同的目录中,通过rolse一键的编排。

mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p

[root@test1 ansible]# yum -y install tree
[root@test1 ansible]# cd roles/
[root@test1 roles]# tree



roles:
├── httpd            ##角色名称,自定义
│   ├── defaults     ##存放配置文件的目录,可以不写
│   ├── files        ##存放copy模块或者script
│   ├── handlers     ##存放处理器文件的目录
│   ├── meta         ##保存角色源信息的文件
│   ├── tasks        ##保存任务的文件
│   ├── templates    ##保存模板的文件
│   └── vars         ##保存变量的文件
就是把原来写一个yaml的配置,分开--------》不同的目录----------》保存在一个名字的yaml里面。
执行的时候调用不同目录的同一个yaml的文件
main.yaml

[root@test1 roles]# touch /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta}/main.yaml
[root@test1 roles]# touch /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta}/main.yaml
[root@test1 roles]# touch /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta}/main.yaml
[root@test1 roles]# tree

总结:
ansible:14个模块必须掌握,熟练。

playbook:剧本能够定义一般的远程部署相关的配置即可

​ 了解条件判断,循环

​ tags的作用 标签的 系统标签:always never 自定义

​ templates:了解即可

​ roles:了解即可。

作业、

配置主机清单,实现免密钥对登录。声明ip地址列表

1、在目标主机批量创建目录:/opt/test1 /opt/test2 /opt/test3

2、从主机批量复制文件,123 456 789,分别输出到指定的123–>test1 456---->test2 789---->test3
指定主机为192.168.233.20.

3、创建一个nginx.conf文件,改配置文件可以实现upstream反向代理 复制到nginx1

4、分别在nginx2和nginx3上配置页面: test1 test2

5、在主机访问目标主机nginx1,实现负载均衡。

以上步骤全部用ansible远程完成!

[root@test1 opt]# vim test6.yaml

#配置主机清单,实现免密钥对登录。声明ip地址列表
#1、在目标主机批量创建目录:/opt/test1 /opt/test2 /opt/test3
#2、从主机批量复制文件,123 456 789,分别输出到指定的
123-->test1 456---->test2 789---->test3
#指定主机为192.168.168.22.
#3、创建一个nginx.conf文件,改配置文件可以实现upstream反向代理 复制到nginx1
#4、分别在nginx2和nginx3上配置页面: test1 test2
#5、在主机访问目标主机nginx1,实现负载均衡。
#以上步骤全部用ansible远程完成!
- name: this is if
  hosts: all
  remote_user: root
  tasks:
    - name: create mulu
      file:
        path: "{{item}}"
        state: directory
      with_items: [/opt/test1,/opt/test2,/opt/test3]
      when: ansible_default_ipv4.address == "192.168.168.22"
    - name: copy wenjian
      copy:
        src: "{{ item.src }}"
        dest: "/opt/{{ item.dest }}"
      with_items:
        - { src: '/opt/test/123', dest: 'test1' }
        - { src: '/opt/test/456', dest: 'test2' }
        - { src: '/opt/test/789', dest: 'test3' }
      when: ansible_default_ipv4.address == "192.168.168.22"
    - name: nginx1
      yum: name=nginx state=latest
    - name: nginx.conf
      copy: 'src=/opt/nginx.conf dest=/etc/nginx/nginx.conf'
      when: ansible_default_ipv4.address == "192.168.168.22"
      notify: start nginx
    - name: test2 connection
      ping:
    - name: close selinux
      command: '/sbin/setenforce 0'
      ignore_errors: true
    - name: close firewalld
      service: name=firewalld state=stopped
    - name: install nginx
      yum: name=nginx state=latest
    - name: interview
      shell: echo "this is test1" > /usr/share/nginx/html/index.html
      when: ansible_default_ipv4.address == "192.168.168.23"
      notify: start nginx
    - name: test3 connection
      ping:
    - name: close2 selinux
      command: '/sbin/setenforce 0'
      ignore_errors: true
    - name: close2 firewalld
      service: name=firewalld state=stopped
    - name: install nginx2
      yum: name=nginx state=latest
    - name: interview2
      shell: echo "this is test2" > /usr/share/nginx/html/index.html
      when: ansible_default_ipv4.address == "192.168.168.24"
      notify: start nginx
  handlers:
    - name: start nginx
      service: name=nginx state=started


[root@test1 ansible]# yum -y install nginx
[root@test1 ansible]# cp /etc/nginx/nginx.conf /opt/nginx.conf
[root@test1 ansible]# cd /opt/
[root@test1 opt]# ls
123  345  httpd.conf.j2  nginx.conf  test  test2.yaml  test3.yaml  test4.yaml  test5.yaml  test6.yaml
[root@test1 opt]# vim nginx.conf 


 upstream xy102 {
        server 192.168.168.23;
        server 192.168.168.24;
        }
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        location / {
            root   html;
            index  index.html index.htm;
        proxy_pass http://xy102;
        }

[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test2
[root@test1 opt]# curl 192.168.168.22
this is test2
[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test2
[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test1

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2074720.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

K8s之自动扩缩容

Kubernetes (K8s) 的动态扩缩容(自动伸缩)功能是集群管理中非常关键的一部分,能够根据工作负载的变化自动调整应用程序的副本数,以确保资源的高效利用和服务的稳定性。 K8s介绍文章 容器之k8s(Kubernetes)-CSDN博客 1. 动态扩缩容…

文件包含漏洞(1)

目录 PHP伪协议 php://input Example 1&#xff1a; 造成任意代码执行 Example 2&#xff1a; 文件内容绕过 php://filer zip:// PHP伪协议 php://input Example 1&#xff1a; 造成任意代码执行 搭建环境 <meta charset"utf8"> <?php error_repo…

代码随想录算法训练营第51天|卡码网99. 岛屿数量、100. 岛屿的最大面积

1.卡码网99. 岛屿数量 题目链接&#xff1a;https://kamacoder.com/problempage.php?pid1171 文章链接&#xff1a;https://www.programmercarl.com/kamacoder/0099.岛屿的数量深搜.html#_99-岛屿数量 本题思路: 遇到一个没有遍历过的节点陆地&#xff0c;计数器就加一&#x…

银河麒麟桌面操作系统V10:如何设置应用开机自启动?

银河麒麟桌面操作系统V10&#xff1a;如何设置应用开机自启动&#xff1f; 1、图形界面设置2、命令行设置3、注意 &#x1f496;The Begin&#x1f496;点点关注&#xff0c;收藏不迷路&#x1f496; 1、图形界面设置 打开“设置”->“系统”->“开机启动”。点击“添加…

秋招突击——笔试整理——8/25——拼多多笔试整理

文章目录 引言正文第一题——多多删树个人解答 第二题、多多的奇数序列个人解答 第三题&#xff1a;多多换礼物个人解答参考实现 第四题、多多的字符反转个人实现 总结 引言 今天的拼多多笔试挺难的&#xff0c;感觉自己在技巧性还差很多&#xff0c;很多东西需要看很久&#…

第3章-02-Python库Selenium安装与讲解

🏆作者简介,黑夜开发者,CSDN领军人物,全栈领域优质创作者✌,CSDN博客专家,阿里云社区专家博主,2023年CSDN全站百大博主。 🏆数年电商行业从业经验,历任核心研发工程师,项目技术负责人。 🏆本文已收录于专栏:Web爬虫入门与实战精讲,后续完整更新内容如下。 文章…

windows javascript 打开、关闭摄像头

1. 效果 打开摄像头 关闭摄像头&#xff08;包括指示灯也关了的&#xff09; 2. 代码 open_close_camera.html // open_close_camera.html <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>use camera</title>…

CPP中lamada表达式作用一览[more cpp-6]

一般语法 CPP中的lambda 表达式的本质就是匿名函数&#xff0c;它可以在代码中定义一个临时的、局部的函数.为什么需要lamada表达式&#xff1f; 因为命名是个大问题 想名字以及避免命名冲突是很劳神费力的事&#xff0c;这就是lamada表达式的优点(lamada优点表现为简洁性)总…

7.Linux_GCC与GDB

GCC 1、GCC编译过程 首先使用编辑器对.c文件进行编辑&#xff0c;即&#xff1a;敲代码。之后GCC编译器会对.c文件进行预处理、编译、汇编、链接&#xff0c;最终输出可执行文件。具体流程如下&#xff1a; 四个阶段的含义及指令 1、预处理 指令&#xff1a;gcc - E <.…

专利服务系统小程序的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;用户管理&#xff0c;专利申请管理&#xff0c;分类号管理&#xff0c;专利管理&#xff0c;增值服务管理&#xff0c;业务指导信息管理&#xff0c;系统管理 微信端账号功能包括&#xff1a;系统首页…

免费插件集-illustrator插件-Ai插件-路径节点分割路径

文章目录 1.介绍2.安装3.通过窗口>扩展>知了插件4.功能解释5.总结 1.介绍 本文介绍一款免费插件&#xff0c;加强illustrator使用人员工作效率&#xff0c;路径处理功能&#xff0c;功能是路径节点分割路径。首先从下载网址下载这款插件 https://download.csdn.net/down…

kubenetes--资源调度

前言&#xff1a;本博客仅作记录学习使用&#xff0c;部分图片出自网络&#xff0c;如有侵犯您的权益&#xff0c;请联系删除 出自B站博主教程笔记&#xff1a; 完整版Kubernetes&#xff08;K8S&#xff09;全套入门微服务实战项目&#xff0c;带你一站式深入掌握K8S核心能…

C语言 之 自定义类型:结构体

结构体类型的声明 结构体的声明 struct tag {member-list; //结构体中的成员&#xff0c;可以有多个 }variable-list; //这里是直接创建结构体的变量&#xff0c;但是不一定要在这里声明变量 //不能把后面这个 ; 省略了例如结构体用于描述一个学生&#xff1a; struct Stu…

48.给定一个 n × n 的二维矩阵表示一个图像,实现一个算法将图像原地顺时针旋转 90 度

48. Rotate Image 题目 你得到了一个 n x n 的二维矩阵,表示一张图像。 将图像顺时针旋转 90 度。 注意: 你必须 就地 旋转图像,这意味着你需要直接修改输入的二维矩阵。不能分配另一个二维矩阵来进行旋转。 示例 1: 输入: [ [1,2,3], [4,5,6], [7,8,9] ] 输出: [ [7,…

数据中台架构设计

由于当前项目需要对接多个不同的数据源&#xff0c;同时涉及到多端处理&#xff0c;而且需要考虑海量数据处理&#xff0c;还有总部与分部架构部署问题&#xff0c;因而整体技术栈倾向于大数据和分表分库式处理数据层接入问题。 简单讲&#xff0c;项目分为数据中台和业务中台…

介绍下线程池的七个参数

corePoolSize&#xff1a;核心线程数&#xff0c;池中要保留的线程数&#xff0c;即使它们处于空闲状态&#xff0c;除非设置了allowCoreThreadTimeOutmaximumPoolSize&#xff1a;最大线程数量keepAliveTime&#xff1a;线程存活时间unit&#xff1a;时间单位workQueue&#x…

【C++离线查询】2250. 统计包含每个点的矩形数目

本文涉及的基础知识点 离线查询 LeetCode2250. 统计包含每个点的矩形数目 给你一个二维整数数组 rectangles &#xff0c;其中 rectangles[i] [li, hi] 表示第 i 个矩形长为 li 高为 hi 。给你一个二维整数数组 points &#xff0c;其中 points[j] [xj, yj] 是坐标为 (xj,…

Linux——文件系统层次结构,绝对路径

一、文件系统层次结构 linux中所有文件存储在文件系统中&#xff0c;文件系统层次结构可以类比为一颗到立的树&#xff0c;最顶层为根目录/&#xff0c;根目录/底又分了/etc,/bin,/boot,/dev等子目录&#xff0c;而这些子目录底下又可以生成子目录分支。 以下为linux中较为重要…

亿图设备采集

这个数控系统的英文名是HUST,在台湾知名度还可以,但大陆这边我做这么多年项目也只接触过屈指可数的几次,可见市场占有率并不大。下面是一些介绍: 台灣億圖 HUST CNC 是一個充滿活力的公司,我們經營的目標是提供能滿足客戶之優良產品,以及優質的服務。我們的期望是使 HUS…

【STM32】驱动LCD

没买LCD屏&#xff0c;没有上机实践&#xff0c;只是学习了理论。 大部分图片来源&#xff1a;正点原子HAL库课程 专栏目录&#xff1a;记录自己的嵌入式学习之路-CSDN博客 目录 1 屏幕接口 2 屏幕驱动的基本步骤 3 8080时序的各信号线 4 8080的读和写 5 屏…