当前位置:网站首页>ansible的安装与部署
ansible的安装与部署
2022-08-02 03:35:00 【linyxg】
一、ansible的安装方法
1、搭建epel源 ,从阿里云上下载(必须要网)
dnf install sshpass -y
dnf install ansible
ansible --viersion 查看
先安装psassh(ansible依赖性),再安装ansible
2、从网上下载ansible安装包的
安装方法1
cd /etc/yum.repos.d/
vim westos.repo
[Ansible]
name=westosansible
baseurl=http:///ansible
gpgcheck=0
enabled=1
dnf install sshpass
dnf install ansible
3、从网上下载epel.rpm安装包
wget https://(epel源的rpm包)
rpm -ivh epel…noarch.rpm
cd /etc/yum.repos.d/
dnf install sshpass
dnf install ansible
二、ansible的基本信息
查看ansible的配置文件:rpm -cq ansible
全局配置文件,默认很少修改:/etc/ansible/ansible.cfg
全局主机清单,清单文件:/etc/ansible/hosts (对所有用户生效)
三、构建ansible清单(清单就是ansible控制主机的列表)
实验文件内容:
[[email protected] yum.repos.d]# cat /mnt/westos
172.25.254.3
westos.westos.org
[java]
node[1:5].westos.org
c.westos.org
[[email protected] yum.repos.d]# cat /etc/ansible/hosts
[westos]
172.25.254.125
172.25.254.200
172.25.254.[1:10]
[linux]
172.25.254.25
172.25.254.125
[list3]
node2.westos.com
[test:children]
westos
linux
list3
vim /etc/ansible/hosts
书写方法;
1.直接书写受管主机名或ip,每行一个
2.设定受管主机的组 :[组名] --ip|主机名
1.单层清单:
[列表名]
主机ip|主机名(每行一个)
2.嵌套清单
格式[test:children]
列表名
列表名
例子:[test:children]
westos
linux
list3
3.主机规格的范围化操作
通过指定主机名称或ip的范围可以简化ansible主机清单
172.25.254.[1:10]
4.指定其他清单
vim /mnt/westos(名字随意)
[java]
node[1:10].westos.org
查看方式:ansible -i /mnt/westos all --list (指定清单)
5.清单查看:
ansible -i /mnt/westos all --list (指定清单)
ansible -i /mnt/westos ungrouped --list 抓取不在[列表]中的主机
ansible all --list-hosts(-hosts可省略不写) 查看所有清单
6.正则表达时
- 所有
查看指定文件内所有node后面跟任何字符的:ansible -i /mnt/westos node*.westos.org --list
: 逻辑或
查看指定文件内所有node和westos后接任意字符的:ansible -i /mnt/westos node*:westos*.org --list
ansible -i /mnt/westos node*:c.westos.org --list
:&逻辑与
查看既在westos又在linux列表中的主机:ansible ‘westos:&linux’ --list-hosts
:! 非 (不在列表李的)
查看westos和linux中除相同主机外的所有主机:sible ‘westos:!linux’ --list
~ 以什么开头的
查看指定文件中以node开头的主机:ansible -i /mnt/westos ~node --list
~(条件1|条件2) 以条件1或者条件2开头的
查看指定文件中以node和c开头的主机ansible -i /mnt/westos ‘~(node|c)’ --list
四、ansible配置文件参数详解
ansible 清单中组名称 -m 模块 -u remote_user -K (模块–>命令 -u --> 远程主机用户名称 -K 提示输密码)
1.配置文件的分类与优先级
基本配置文件,找不到其他配置文件此文件生效(主配置文件);/etc/ansible/ansible.cfg
用户当前目录中没有ansible.cfg此文件生效:~/.ansible.cfg
优先级最高:./ansible.cfg
2.常用配置参数
(1)79行 host_key_checking = False 取消注释,false第一次连接受馆主机时不输入yes建立host_key
第一次ping westos列表里的用户不用输yes:ansible westos -m ping -k
ansible westos -m shell -a ‘hostname’ -k
ansible westos -m shell -a ‘useradd test’ -k
ansible westos -m shell -a ‘userdel -r test’ -k
(2)第14行 inventory = /etc/ansible/hosts,/mnt/westos 添加指定清单
ansible all --list 在未添加/mnt/westos文件之前只能列出/etc/ansible/hosts里面的主机,添加后列出两个文件里的全部主机。
(3)第17行 remote_tmp = ~/.ansible/tmp 在本机把命令转换位python脚本,上传给远程主机的家目录下
ansible westos -m shell -a “sleep 10” -k
(4)第15行 remote_user = westos 设置默认登陆用户
-u 远程主机用户 (若未指定远程主机用户身份,则使用当前主机的身份作为远程主机的登陆用户)
become 默认用户是root
2.9版本用的become_user不用sudo_user
7.用户的专属配置的设置(在目录/root/.ansible下的操作运行的三用户专属配置文件)
生成密钥:ssh-keygen
切换到根目录下:cd
切换密钥所在目录:cd .ssh/
查看:ls
把所和客户主机125绑定,形成免密登陆:ssh-copy-id -i id_rsa.pub [email protected]
把所和客户主机225绑定,形成免密登陆:ssh-copy-id -i id_rsa.pub [email protected]
尝试登陆看看是否免密:ssh [email protected]
尝试登陆看看是否免密:ssh [email protected]
编辑文件:vim /etc/ansible/ansible.cfg
修改345行为False: become_ask_pass=False
remote_user = westos
此时:两台客户机操作
visudo
添加第101行: westos ALL=(root) NOPASSWD: ALL
注释:108行 #%wheel ALL=(ALL) ALL
因为westos的附属组为wheel
免密登陆并且建立了tset用户:ansible westos -m shell -a ‘useradd test’
免密删除建立的用户:ansible westos -m shell -a ‘userdel -r test’
mkdir .ansible/
切换到:cd .ansible/(都在此目录下)
查看文件:ls
复制原清单作为此用户的清单:tail -n 12 /etc/ansible/hosts > inventory
复制基础配置文件作为此用户配置文件:cp /etc/ansible/ansible.cfg .
测试: ansible westos -a ‘id’
ls
编辑此用户的配置文件:vim ansible.cfg
内容:
inventory = /root/.ansible/inventory
remote_user = westos
#roles_path = /etc/ansible/roles
host_key_checking = False
module_name = command
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
查看当前用户id:ansible westos -a ‘id’
查看当前用户清单:cat /root/.ansible/inventory
内容:
[westos]
172.25.254.125
172.25.254.225
[linux]
172.25.254.25
[test:children]
westos
linux
此命令 ansible westos -a ‘id’
等于:ansible westos -m command -a ‘id’
编辑文件:vim ansible.cfg
修改为:module_name = ping
运行:ansible westos
改回:vim ansible.cfg
module_name = command
边栏推荐
猜你喜欢

Hash table problem solving method

rosdep update失败解决办法(亲测有效)

心余力绌:企业面临的软件供应链安全困境

基础IO(上):文件管理和描述符

MQ-5 combustible gas sensor interface with Arduino

未来智安XDR及核心组件产品上榜《嘶吼2022网络安全产业图谱》

Flame sensor connected with Arduino

学习(四):显示FPS,和自定义显示调试

Process (below): process control, termination, waiting, replacement

Application of electronic flow on business trip
随机推荐
剑指Offer 33.二叉搜索树的后序遍历序列
防抖和节流(实例讲解)
机械臂运动学解析
AD实战篇
剑指Offer 47.礼物的最大值 动态规划
全加器高进位和低进位的理解
TeamCode 产品 UI 全新升级,快来体验吧
剑指Offer 32.Ⅲ从上到下打印二叉树
渗透测试(PenTest)基础指南
【网络基础】浏览器输入一个URL之后,都发生了什么(详细讲解)
STM32 CAN 介绍以及相关配置
UKlog.dat和QQ,微信文件的转移
对周期内时间段是否重叠进行校验
位居榜首 | 未来智安荣登CCIA「2022年中国网安产业潜力之星」榜单
【LeetCode】链表相加 进位
功能性网站
MongoDB相关记录
企业需要知道的5个 IAM 最佳实践
js平层数组转树形结构(解决数据处理后源数据篡改)
基础IO(上):文件管理和描述符