Ansible的安装及部署

实验环境:
ansible:172.25.254.103 #管控主机
node:172.25.254.203 #受管控主机

1.Ansible对于企业运维的重大意义

ansible是基于模块工作的,本身没有批量部署的能力,真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

2.Ansible 的安装

下载epel源的rpm包–>安装epel源,ansible就在epel源中–>dnf install ansible.noarch

dnf install sshpass-1.06-9.el8.x86_64.rpm -y  #安装软件依赖性
dnf install ansible-2.9.24-1.el8.noarch.rpm -y  #安装软件
ansible --version #查看安装信息

ansible的基本信息:

/etc/ansible/ansible.cfg		##全局配置文件,默认很少修改
/etc/ansible/hosts			##全局主机清单清单文件


ansible all --list-hosts   #列出管理主机

vim /etc/ansible/hosts  #管理被控主机

///
172.25.254.203       #添加200主机
///

ansible 172.25.254.203 -m ping           ##报错,被200拒绝,当要控制203主机时,首先需要在103主机中生成py脚本然后通过sshd服务传输到被控主机,在测试过程中,需要输入200认证信息通过后者认证

ansible 172.25.254.203 -m ping -K   #执行ansible时询问ssh密码
出现pong回馈表示和203主机之间可以实现ansible的管理

指定用户控制:
node主机:

useradd zhang
echo westos | passwd --stdin root #建立用户让ansible用zhang用户控制本主机

ansible主机:

ssh-keygen
ssh-copy-id -i /root/.ssh/id_rsa.pub zhang@172.25.254.203    #进行免密认证

ansible主机:

ansible 172.25.254.203 -m ping -u zhang
ansible 172.25.254.203 -m shell -a 'touch /mnt/file' -u zhang  #普通用户在node主机中执行命令时会被拒绝,此时需要用户权力下放

node主机:

visudo
///
101 zhang   ALL=(ALL)       NOPASSWD: ALL  #权力下放,让zhang用户在本机中可以做任何事情
///

ansible主机:

 ansible 172.25.254.203 -m shell -a 'touch /mnt/file' -u zhang --become   #become表示zhang用户在执行命令时用sudo调用
3.构建Anisble清单

清单就是ansible控制主机的列表

/etc/ansible/hosts                      ##全局清单文件

#1.直接书写受管主机名或ip,每行一个

node1.westos.com
node2.westos.com
172.25.254.240

#2.设定受管主机的组[组名称]
#清单查看:

ansible -i 清单文件  --list-hosts  #指定清单文件查看
ansible ungrouped --list  #未在任何组中的主机
ansible all --list-hosts   #列出清单

vim /etc/ansible/hosts
///
#单层清单#
[westos1]
172.25.254.203
westos_node1.westos.org

[westos2]
172.25.254.[90:100]
192.168.1.1

[linux]
192.168.1.1

#嵌套清单#
[westos:children]
westos1
westos2
///

测试:

[root@westos_ansible ansible]#ansible westos1 --list-hosts
  hosts (1):
   172.25.254.203
   westos_node1.westos.org

[root@westos_ansible ansible]#ansible westos2 --list
hosts (11):
  172.25.254.90
  172.25.254.91
  172.25.254.92
  172.25.254.93
  172.25.254.94
  172.25.254.95
  172.25.254.96
  172.25.254.97
  172.25.254.98
  172.25.254.99
  172.25.254.100

[root@westos_ansible ansible]#ansible all --list
hosts (13):
  172.25.254.203
  westos_node1.westos.org
  172.25.254.90
  172.25.254.91
  172.25.254.92
  172.25.254.93
  172.25.254.94
  172.25.254.95
  172.25.254.96
  172.25.254.97
  172.25.254.98
  172.25.254.99
  172.25.254.100

#ansible命令指定清单的正则表达式

* ## 所有
## 172.25.254.*
## westos*

[root@westos_ansible ansible]#  ansible "172*" --list
  hosts (12):
     172.25.254.203
 	 172.25.254.90
 	 172.25.254.91
     172.25.254.92
	 172.25.254.93
     172.25.254.94
	 172.25.254.95
	 172.25.254.96
	 172.25.254.97
	 172.25.254.98
	 172.25.254.99
	 172.25.254.100

: ##逻辑或
##westos1:linux
##172.25.254.100:172.25.254.200

[root@westos_ansible ansible]#  ansible "westos1:linux" --list
 hosts (3):
  172.25.254.203
  westos_node1.westos.org
  192.168.1.1

:& ##逻辑与
##westos1:&linux
##主机既在westos1清单也在linux清单中

[root@westos_ansible ansible]#  ansible "westos2:&linux" --list
  hosts (1):
    192.168.1.1

:! ##逻辑非
##westos1:!linux
##在westos1中不在linux中

[root@westos_ansible ansible]#  ansible 'westos1:!linux' --list
  hosts (2):
	172.25.254.203
	westos_node1.westos.org

~ ##以关键字开头

[root@westos_ansible ansible]#  ansible "~172" --list
  hosts (12):
    172.25.254.203
    172.25.254.90
    172.25.254.91
	172.25.254.92
    172.25.254.93
    172.25.254.94
    172.25.254.95
    172.25.254.96
    172.25.254.97
    172.25.254.98
    172.25.254.99
    172.25.254.100

~(str1|str2) ##以条件1或者条件2开头

[root@westos_ansible ansible]#  ansible "~(172|192)" --list
 hosts (13):
  172.25.254.203
  172.25.254.90
  172.25.254.91
  172.25.254.92
  172.25.254.93
  172.25.254.94
  172.25.254.95
  172.25.254.96
  172.25.254.97
  172.25.254.98
  172.25.254.99
  172.25.254.100
  192.168.1.1

#指定其他清单文件

vim inventory
/// 
172.25.254.203
[westostest]
172.25.254.103
172.25.254.203
///

测试:

ansible all -i inventory  --list-hosts
ansible westostest -i inventory  --list-hosts
ansible ungrouped -i inventory  --list-hosts
4.Ansible配置文件参数详解

ansible 清单中组名称 -m 模块 -u remote_user

#1.配置文件的分类与优先级
vim /etc/ansible/ansible.cfg #基本配置文件,找不到其他配置文件此文件生效
~/.ansible.cfg #用户当前目录中没有ansible.cfg此文件生效
./ansible.cfg #优先级最高

#2.常用配置参数

vim /etc/ansible/ansible.cfg	
 ///
#[default]				##基本信息设定
inventory=				##指定清单路径
remote_user=				##在受管主机上登陆的用户名称,未指定使用当前用户
ask_pass=				##是否提示输入SSH密码,如果公钥登陆设定为false
library=				##库文件存放目录
local_tmp=				##本机临时命令执行目录
remote_tmp=				##远程主机临时py命令文件存放目录
forks=					##同时处理受控主机数量
host_key_checking=			##第一次连接受管主机时是否要输入yes建立host_key
sudo_user=				##默认sudo用户
ask_sudo_pass=				##每次在受控主机执行ansible命令时是否询问sudo密码
module_name=				##默认模块,默认使用command,可以修改为shell
log_path=				##日志文件路径
///

[privilege_escalation]			##身份信息设定
become=	True   				##连接后是否自动切换用户
become_method=sudo			##设定切换用户的方式,通常用sudo
become_user=root				##在受管主机中切换到的用户,通常为root
become_ask_pass=False				##是否需要为become_method提示输入密码,默认为false
///
host_key_checking = False        #不在询问时候接受受控主机的key
module_name = ping               #ansible命令运行时默认使用模块
///

实验:
ansible主机:

useradd zhang
echo westos | passwd --stdin zhang
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub zhang@172.25.254.103  #对zhang用户生成免密认证
visudo		##两台主机都需要做zhang用户的权力下放
///
101 zhang  ALL=(ALL)       NOPASSWD:ALL
///

useradd zjl
su - zjl
mkdir -p /home/zjl/ansible
scp root@172.25.254.103:/root/.ssh/id_rsa /home/zjl/.ssh/id_rsa		##注意是zjl用户
##用key对指定devops用户加密;两台主机都要做密钥,此处是因为最开始环境配置的时候已经对	203主机做过密钥加密
cd /home/zjl/ansible
vim ansible.cfg
///
[defaults]
inventory		=~/ansible/inventory
host_key_checking	=False
ask_pass		=False
#roles_path		=
remote_user		=zhang
module_name		=shell

[privilege_escalation]
become=True
becuserome_method=sudo    
become_user=root
become_sak_pass=False          #become 是以root身份调用sudo
///

vim inventory
///
[westos]
172.25.254.103
172.25.254.203
///

ansible westos -m ping

在这里插入图片描述

zjl用户可以免密登录114主机和214主机的zhang用户

ssh -l zhang 172.25.254.103

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值