ansible第一天

news2024/10/6 16:24:10

ansible

第一天

以上主机使用rhel-8.2-x86_64-dvd.iso镜像,配置ip、yum源,关闭防火墙和selinux规则

安装中文包,重启生效

 

[root@control ~]# yum -y install langpacks-zh_CN.noarch && reboot

配置名称解析
[root@control ~]# echo -e "192.168.88.253\tcontrol">>/etc/hosts
[root@control ~]# for i in {1..5}
do
echo -e "192.168.88.1$i\tnode$i">>/etc/hosts
done
配置ssh到所有节点免密登陆
[root@control ~]# ssh-keygen
root@control ~]# echo node{1..5}
node1 node2 node3 node4 node5
[root@control ~]# for i in node{1..5}
> do
> ssh-copy-id root@$i
> done
装包

软件包链接:链接:百度网盘 请输入提取码 提取码:bb2o --来自百度网盘超级会员V5的分享

[root@control ~]# ls
anaconda-ks.cfg  ansible_soft.tar.gz
[root@control ~]# tar zxvf ansible_soft.tar.gz 
[root@control ~]# ls
anaconda-ks.cfg  ansible_soft  ansible_soft.tar.gz
[root@control ~]# ls ansible_soft
ansible-2.8.5-2.el8.noarch.rpm           python3-paramiko-2.4.3-1.el8.noarch.rpm
libsodium-1.0.18-2.el8.x86_64.rpm        python3-pynacl-1.3.0-5.el8.x86_64.rpm
python3-bcrypt-3.1.6-2.el8.1.x86_64.rpm  sshpass-1.06-9.el8.x86_64.rpm
[root@control ~]# yum -y install /root/ansible_soft/*.rpm
创建ansible工作目录
创建ansible工作目录,目录名自己定义,不是固定的
[root@control ~]# mkdir ansible
[root@control ~]# cd ansible
创建配置文件。默认的配置文件是/etc/ansible/ansible.cfg,一般不用,而是在工作目录下创建自己的配置文件
[root@control ansible]# vim ansible.cfg 文件名必须是ansible.cfg
[root@control ansible]# cat ansible.cfg 
[defaults]
inventory = hosts  管理的主机,配置在当前目录的hosts文件中,hosts是自己定义的。=号俩边空格可有可无
[root@control ansible]# touch hosts
[root@control ansible]# vim hosts
[root@control ansible]# cat hosts
[test]
node1
[proxy]
node2
[webservers]
node[3:4]
[database]
node5
[cluster:children] cluster是组名,自定义的;children是固定写法,表示下面的组名是cluster的子组
webservers
database
[root@control ansible]# ansible all --list
  hosts (5):
    node1
    node2
    node3
    node4
    node5
[root@control ansible]# ansible webservers --list
  hosts (2):
    node3
    node4
[root@control ansible]# ansible proxy --list
  hosts (1):
    node2
简单演示
用ansible创建/tmp/abcd目录
[root@control ansible]# ansible all -a "mkdir /tmp/abcd"
 [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.
If you need to use command because file is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
​
node2 | CHANGED | rc=0 >>
​
​
node1 | CHANGED | rc=0 >>
​
​
node5 | CHANGED | rc=0 >>
​
​
node3 | CHANGED | rc=0 >>
​
​
node4 | CHANGED | rc=0 >>
ansible管理
ansible进行远程管理的俩个办法

adhoc临时命令。就是在命令行上执行管理命令

playbook剧本。把管理任务用特定格式写到文件中

无论哪种方式,都是通过模块加参数进行管理

adhoc临时命令
语法:

ansible 主机或者组列表 -m 模块 -a 参数 
测试ansible与被控主机的连通性
[root@control ansible]# ansible all -m ping
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
node5 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
node4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
command模块
ansible默认模块,用于在远程主机上执行任意命令
command不支持shell特性。如管道、重定向
在所有被管主机上创建目录aaa
[root@control ansible]# ansible all -a "mkdir aaa"
 [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.
If you need to use command because file is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
​
node5 | CHANGED | rc=0 >>
​
​
node3 | CHANGED | rc=0 >>
​
​
node1 | CHANGED | rc=0 >>
​
​
node2 | CHANGED | rc=0 >>
​
​
node4 | CHANGED | rc=0 >>
查看node节点的ip地址,不支持管道、重定向命令
[root@control ansible]# ansible all -a "ip a|head -2"
node3 | FAILED | rc=1 >>
Object "a|head" is unknown, try "ip help".non-zero return code
​
node2 | FAILED | rc=1 >>
Object "a|head" is unknown, try "ip help".non-zero return code
​
node1 | FAILED | rc=1 >>
Object "a|head" is unknown, try "ip help".non-zero return code
​
node4 | FAILED | rc=1 >>
Object "a|head" is unknown, try "ip help".non-zero return code
​
node5 | FAILED | rc=1 >>
Object "a|head" is unknown, try "ip help".non-zero return code
shell模块
与command模块类似,但是支持shell特性,如管道、重定向
[root@control ansible]# ansible node1 -m shell -a "ip a| head"
node1 | CHANGED | rc=0 >>
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:44:4e:3b brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.11/24 brd 192.168.88.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
script模块
用于在远程主机上执行脚本
在控制端创建脚本即可
[root@control ansible]# vim http.sh 
#!/bin/bash
yum -y install httpd
systemctl start httpd
在test组的主机上执行脚本
[root@control ansible]# ansible test -m script -a "http.sh"
查看test组的主机httpd服务是否开启
[root@control ansible]# ansible test  -a "systemctl status httpd"
node1 | CHANGED | rc=0 >>
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-11-07 19:04:56 EST; 44s ago
     Docs: man:httpd.service(8)
 Main PID: 3226 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 5298)
   Memory: 27.8M
   CGroup: /system.slice/httpd.service
           ├─3226 /usr/sbin/httpd -DFOREGROUND
           ├─3227 /usr/sbin/httpd -DFOREGROUND
           ├─3230 /usr/sbin/httpd -DFOREGROUND
           ├─3231 /usr/sbin/httpd -DFOREGROUND
           └─3233 /usr/sbin/httpd -DFOREGROUND
​
11月 07 19:04:56 node1 systemd[1]: Starting The Apache HTTP Server...
11月 07 19:04:56 node1 httpd[3226]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::dde1:3eea:5077:d08f. Set the 'ServerName' directive globally to suppress this message
11月 07 19:04:56 node1 systemd[1]: Started The Apache HTTP Server.
11月 07 19:04:56 node1 httpd[3226]: Server configured, listening on: port 80

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

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

相关文章

拓展企业客户群:如何使用企业联系方式查询API帮助在社交媒体上寻找潜在客户

前言 在当今竞争激烈的商业环境中&#xff0c;拓展企业客户群已经成为许多企业的首要任务之一。在这种情况下&#xff0c;使用企业联系方式查询API可以帮助企业在社交媒体上寻找潜在客户。本文将探讨如何使用企业联系方式查询API拓展企业客户群。 企业联系方式查询API简介 首…

linux rsyslog介绍

Rsyslog网址&#xff1a;https://www.rsyslog.com/ Rsyslog is the rocket-fast system for log processing. It offers high-performance, great security features and a modular design. While it started as a regular syslogd, rsyslog has evolved into a kind of swis…

【物联网】继续深入探索ADC模拟转数字的原理——Flash ADC流水线ADC逐次逼近型SAR ADC

这篇文章主要弥补上一篇关于ADC的不足&#xff0c;更加深入了解ADC数模转换器的工作原理&#xff0c;举例常见的三种ADC&#xff0c;分别为Flash ADC&流水线ADC&逐次逼近型SAR ADC。 【物联网】深入了解AD/DA转换技术&#xff1a;模数转换和数模转换 文章目录 一、模拟…

休眠和睡眠有哪些区别?如何让电脑一键休眠?

电脑中有休眠和睡眠&#xff0c;那么它们有什么区别呢&#xff1f;下面我们就通过本文来了解一下。 休眠和睡眠的区别 电脑在睡眠状态时&#xff0c;会切断内存之外的设备电源&#xff0c;电脑会进入睡眠状态&#xff0c;当再次唤醒电脑后&#xff0c;不会影响睡眠前保存好的工…

Git中的 fork, clone,branch

一、是什么 fork fork&#xff0c;英语翻译过来就是叉子&#xff0c;动词形式则是分叉&#xff0c;如下图&#xff0c;从左到右&#xff0c;一条直线变成多条直线 转到git仓库中&#xff0c;fork则可以代表分叉、克隆 出一个&#xff08;仓库的&#xff09;新拷贝 包含了原来…

Swing 程序设计

概述 String包的层次结构和继承关系如下 常用的Swing组件如下表 Swing常用窗体 JFrame 窗体 JFrame 类的常用构造方法包括以下两种形式&#xff1a; public JFrame&#xff08;&#xff09;&#xff1a;创建一个初始不可见、没有标题的窗体。public JFrame(String title)…

Java并发编程第11讲——AQS设计思想及核心源码分析

Java并发包&#xff08;JUC&#xff09;中提供了很多并发工具&#xff0c;比如前面介绍过的ReentrantLock、ReentrantReadWriteLock、CountDownLatch、Semaphore、FutureTask等锁或者同步部件&#xff0c;它们的实现都用到了一个共同的基类——AbstractQueuedSynchronizer&…

element分页

获取数据信息&#xff0c;这是表格和分页内容 <el-col :span"24"><div class"grid-content bg-purple-dark"><el-table :data"tableData" stripe style"width: 100%"><el-table-column prop"xuhao" l…

WorkPlus:企业数字化底座,统一数字化办公入口

在企业数字化转型的潮流下&#xff0c;统一入口的移动数字化底座成为了企业提高工作效率和迈向数字化时代的关键要素。在这个领域&#xff0c;WorkPlus凭借其独特的定位和功能&#xff0c;成为了企业微信、钉钉、飞书等类似产品中的完美选择&#xff0c;为企业提供了统一入口的…

旋转矩阵-数学理论

目录 概述 一、固定旋转&#xff08;Fix Angles&#xff09; 二、欧拉旋转&#xff08;Euler Angle&#xff09; 三、旋转矩阵小结 四、参考 概述 旋转矩阵是姿态的一种数学表达方式&#xff0c;或者笼统说变换矩阵是一种抽象的数学变量。其抽象在于当你看到…

【深度学习】卷积层填充和步幅以及其大小关系

参考链接 【深度学习】&#xff1a;《PyTorch入门到项目实战》卷积神经网络2-2&#xff1a;填充(padding)和步幅(stride) 一、卷积 卷积是在深度学习中的一种重要操作&#xff0c;但实际上它是一种互相关操作&#xff0c;&#xff0c;首先我们来了解一下二维互相关&#xff…

Redis主从配置和哨兵模式

主从简介 1、主从 – 用法 像MySQL一样&#xff0c;redis是支持主从同步的&#xff0c;而且也支持一主多从以及多级从结构。 主从结构&#xff0c;一是为了纯粹的冗余备份&#xff0c;二是为了提升读性能&#xff0c;比如很消耗性能的SORT就可以由从服务器来承担。 redis的主…

oracle-sql语句解析类型

语句执行过程&#xff1a;1. 解析(将sql解析成执行计划) 2.执行 3.获取数据(fetch) 1. shared pool的组成。 share pool是一块内存池。 主要分成3块空间。free&#xff0c; library(库缓存&#xff0c;缓存sql以及执行计划)&#xff0c;row cache(字典缓存) select * from v…

云贝教育 |【PostgreSQL PGCA】pg15安装pg_hint_plan扩展包

pg15安装pg_hint_plan扩展包 pg当前是支持HINT固定执行计划&#xff0c;需要通过扩展包pg_hint_plan来实现 一、扩展包下载&#xff1a; Releases ossc-db/pg_hint_plan GitHub 二、选择v15版本 pg_hint_plan15 1.5.1 is released pg_hint_plan15 1.5.1 is released. This…

SLAM从入门到精通(安全避障)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 在工业生产中&#xff0c;安全是底线。没有安全性的技术&#xff0c;一般也不会在工业生产中进行部署、使用。对于slam来说&#xff0c;同样也是这…

机器人制作开源方案 | 行星探测车实现云端控制

1. 功能描述 本文示例所实现的功能为&#xff1a;手机端控制R261样机行星探测车的显示屏显示心形图。 2. 电子硬件 在这个示例中&#xff0c;我们采用了以下硬件&#xff0c;请大家参考&#xff1a; 3. 功能实现 编程环境&#xff1a;Milxy 0.999及以上版本 下面提供一个手机…

TerraNoise for 3dMax插件教程

TerraNoise for 3dMax插件教程 创建地形&#xff1a; - 从列表中拖动生成器并将其放到画布上 - 将导出器拖放到画布上 - 通过将一条线从生成器黄色输出“拖动”到导出器绿色输入来连接 2 个组件 - 单击导出器中的“无”按钮用于选择输出名称和格式&#xff08;导出 terragen 地…

Python装饰器的艺术

文章目录 装饰器基础示例代码:执行结果:参数化装饰器示例代码:执行结果:类装饰器示例代码:执行结果:装饰器的堆栈示例代码:执行结果:在Python中,装饰器是一种非常强大的特性,允许开发人员以一种干净、可读性强的方式修改或增强函数和方法。以下是一个关于Python装饰器…

聊聊定时器 setTimeout 的时延问题

给大家推荐一个实用面试题库 1、前端面试题库 &#xff08;面试必备&#xff09; 推荐&#xff1a;★★★★★ 地址&#xff1a;web前端面试题库 全局的 setTimeout() 方法设置一个定时器&#xff0c;一旦定时器到期&#xff0c;就会执行一个函数或指定的代码片…

【开源】前后端分离后台管理系统

系统环境 JDK 17Maven 3.0.0MySQL 5.7.0Spring Boot 3.0.10 演示 橙子官网&#xff1a;http://hengzq.cnGitHub 代码下载&#xff1a;https://github.com/mmd0308/orangeGitee 代码下载&#xff1a;https://gitee.com/hengzq/orange 项目截图