一、构建学习环境:

准备四台机器,并设置主机名

[root@serverb ~]# hostnamectl set-hostname servera
[root@serverb ~]# su -
Last login: Sat Nov 27 21:32:10 CST 2021 on pts/1
[root@servera ~]# [root@localhost ~]# hostnamectl set-hostname serverb
[root@localhost ~]# su -
Last login: Sat Nov 27 21:33:21 CST 2021 from 192.168.159.1 on pts/0
[root@serverb ~]# [root@localhost ~]#  hostnamectl set-hostname serverc
[root@localhost ~]# su -
Last login: Sat Nov 27 21:07:03 CST 2021 from 192.168.159.1 on pts/0
[root@serverc ~]# [root@localhost ~]#  hostnamectl set-hostname serverd
[root@localhost ~]# su -
Last login: Sat Nov 27 21:07:00 CST 2021 from 192.168.159.1 on pts/0
[root@serverd ~]#

点击xshell的工具中的 “发送键到所有会话” 可实现批量操作

添加标签实现 配置epel扩展源

[root@servera ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

批量操作配置hosts解析

[root@servera ~]# vim + /etc/hosts
[root@servera ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.130 servera
192.168.159.131 serverb
192.168.159.132 serverc
192.168.159.133 serverd

servera以root用户配置到所有机器免密钥互信(再次点击xshell工具中“发送键到所有会话”实现关闭)

[root@servera ~]# ssh-keygen -f ~/.ssh/id_rsa -P '' -q
[root@servera ~]# ls ~/.ssh
id_rsa  id_rsa.pub

分发到所有节点(for循环的方式)

[root@servera ~]# for i in server{a..d}
> do
> ssh-copy-id $i
> done

验证是否分发到所有节点

[root@servera ~]# for i in server{a..d}
> do
> ssh $i hostname
> done
servera
serverb
serverc
serverd

安装所需的软件包(ansible)

[root@servera ~]# yum install ansible -y

创建清单inventory文件/home/student/ansible/inventory,要求如下:

servera属于dev主机组

serverb属于test和balancers主机组

serverc和serverd属于prod主机组

prod主机组属于webservers主机组

创建ansible目录

[root@servera ansible]# cp /etc/ansible/ansible.cfg .
[root@servera ansible]# vim inventory
[root@servera ansible]# cat inventory
[dev]
servera[test]
serverb[balancers]
serverb[prod]
serverc
serverd[webservers:children]
prod

创建ansible配置文件/home/student/ansible/ansible.cfg,要求如下:

使用/home/ansible/inventory清单文件

角色存放在/home/ansible/roles/ 目录

[root@servera ansible]# touch inventory
[root@servera ansible]# mkdir roles

修改配置文件

[root@servera ansible]# vim ansible.cfg修改如下:
inventory      = /root/ansible/inventory
roles_path    = /root/ansible/roles
host_key_checking = False

测试

[root@servera ansible]# ansible-inventory --graph
@all:|--@balancers:|  |--serverb|--@dev:|  |--servera|--@test:|  |--serverb|--@ungrouped:|--@webservers:|  |--@prod:|  |  |--serverc|  |  |--serverd
[root@servera ansible]# ansible all -m ping -o
servera | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
serverb | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
serverc | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
serverd | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

二、创建一个 shell 脚本名为 adhoc.sh 用以运行 ad-hoc 命令 。为每个受控节点配罝 yum仓库。要求如下:

仓库1 :

Name: RH294_Base

Description: RH294 base software

Base url: http://content.example.com/rhel8.0/x86_64/dvd/BaseOS

需要验证钦件包 GPG 签名

GPG key 在: /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

启用此软件仓库

仓库 2:

Name: RH294_Stream

Description : RH294 stream software

Base url: http://content.example.com/rhel8.0/x86_64/dvd/AppStream

需要验证软件包 GPG 签名

GPG key 在: /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

启用此软件仓库

[root@servera ansible]# vim adhoc.sh
[root@servera ansible]# cat adhoc.sh
#!/bin/bash
ansible dev -m yum_repository -a 'name=RH294_Base description="RH249 base software" \Base url=http://content.example.com/rhel8.0/x86_64/dvd/BaseOS \gpgcheck=yes gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release \enabled=yes'ansible dev -m yum_repository -a 'name= RH294_Stream description=" RH294 stream software" \Base url=http://content.example.com/rhel8.0/x86_64/dvd/AppStream \gpgcheck=yes gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release \enabled=yes'[root@servera ansible]# chmod +x adhoc.sh
[root@servera ansible]# ./adhoc.sh

ansible 第一次练习相关推荐

  1. ansible第一次使用模块报错

    报错信息: [root@localhost ansible]# ansible -i /etc/ansible/hosts web-servers -m ping "msg": & ...

  2. ansible之安装与简单使用

    http://www.ansible.com.cn/:参照中文文档 安装方式:采用epel源安装 a安装epel源: yum install wget wget dl.fedoraproject.or ...

  3. ansible 的第一次亲密接触

    如何添加一台机器 编辑 /etc/ansible/hosts 添加本机的 public ssh key 到目标机器的 authorized_keys 添加本机的 私钥 到 ansible 运行 ans ...

  4. Ansible的安装及部署

    Ansible的安装及部署 1 实验环境 2 Ansible的安装 3 Ansible的基本信息 4 构建Ansible清单 4.1 全局清单 4.2 设定受控主机的组 4.2.1 清单查看 4.2. ...

  5. ansible 详解

    文章目录 一.ansible 简介 1.1 ansible 是什么? 1.2 ansible 特点 1.3 ansible 架构图 二.ansible 任务执行 2.1 ansible 任务执行模式 ...

  6. Ansible详解(二)

    Ansible系列命令 Ansible系列命令有如下: ansible:这个命令是日常工作中使用率非常高的命令之一,主要用于临时一次性操作: ansible-doc:是Ansible模块文档说明,针对 ...

  7. 从Docker 到Jenkins 到Ansible的部署经验

    从Docker 到Jenkins 到Ansible的部署经验 工作中,除了开发功能,还负责系统的部署工作.我从频繁的部署工作中,逐渐找到了一些偷懒的方法.从传统的Java -jar命令启动服务,到通过 ...

  8. 自动化运维工具Ansible详细部署

    一.基础介绍 ================================================================================= 1.简介 ansibl ...

  9. Python+Django+Ansible Playbook自动化运维项目实战(二)

    Python+Django+Ansible Playbook自动化运维项目实战 一.资产管理,自动化发现.扫描 1.服务端资产探测.扫描发现 1)资产管理的资产: 2)抽象与约定: 2.探测协议和模块 ...

  10. centos 7使用tar包安装ansible

    由于我的yum源没有ansible软件包,防止以后内网服务器也没有ansible的yum源,干脆一点,直接tar包安装!!!! 下载软件包 - # 1.python安装 # python2.7安装 [ ...

最新文章

  1. 面试定心丸:AI知识点备忘录(包括ML、DL、Python、Pandas等)
  2. iphone分辨率_目前最值得入手的三款安卓机!流畅度堪比iPhone,用三五年不过时...
  3. hdu 5586(最大连续子序列和)
  4. 四种π型RC滤波电路
  5. python123平台作业答案第十一周_马哥2016全新Linux+Python高端运维班第十次作业
  6. 《ArcGIS Runtime SDK for .NET开发笔记》--三维功能
  7. C语言 · 猜算式
  8. Aspose.Word
  9. Docker系列教程27-在生产环境中使用Docker Compose
  10. Centos6.4升级glibc_2.14
  11. mysql查询数据1168_mysql merge报错ERROR 1168
  12. 2021-07-17
  13. Airbnb民宿预订
  14. 李宏毅学习笔记35.GAN.06.Tips for Improving GAN
  15. idea新建maven工程没有artifacts
  16. 基于Hi3516DV300的嵌入式入门演练(上)
  17. 小谈网易云音乐----API
  18. (六)图数据neo4j之cypher(一)
  19. kubernetes 【调度和驱逐】【1】污点和容忍度
  20. 搜索引擎优化,SEO关键词快速排名方法有哪些?

热门文章

  1. 深度探索C++对象模型
  2. 1.7 非平衡数据的处理方法大全
  3. Scikit-learn:主要模块和基本使用方法
  4. Effective Use of Word Order for Text Categorization with Convolutional Neural Networks
  5. TF-IDF算法的介绍
  6. Eclipse连接hdfs
  7. json序列化_JSON 序列化和反序列化 In Go
  8. 华为root_传Mate40系列12月率先升级鸿蒙OS 华为官方回应:真相原来是这样
  9. 排序算法java 简书_常用的排序算法(Java版)
  10. 蓝桥杯2019年第十届C/C++省赛C组第一题-求和