ansible_1初学之安装与部署

前言:实验环境设置

为了实验需要,配置三个虚拟机,一台ansible主机和两台受控主机

在配置虚拟机前,将主机设置为路由器,因为ansible需要虚拟机上网下载

[root@westos_student72 Desktop]# firewall-cmd --add-masquerade 
success

1、ansible主机设置

设置主机名称、配置网卡、设置开机为无图形网络模式

[root@westos_ansible ~]# hostnamectl set-hostname westos_ansible.westos.org

[root@westos_ansible ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens3
DEVICE=ens3
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.100
PREFIX=24
DNS1=114.114.114.114
GATEWAY=172.25.254.72
[root@westos_ansible ~]# nmcli connection reload 
[root@westos_ansible ~]# nmcli connection up System\ ens3 

[root@westos_ansible ~]# systemctl set-default multi-user.target 

在确定网关设置好能正常上网后,去阿里云下载epel源

[root@westos_ansible ~]# ping www.baidu.com
PING www.a.shifen.com (36.152.44.95) 56(84) bytes of data.
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=1 ttl=53 time=238 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=2 ttl=53 time=63.9 ms

[root@westos_ansible ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@westos_ansible ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel* 
[root@westos_ansible ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
##第一句是安装 epel 配置包
##第二句是将 repo 配置中的地址替换为阿里云镜像站地址,但代码有问题,可参考评论区置顶评论代码

[root@westos_ansible ~]# dnf search ansible
[root@westos_ansible ~]# dnf install ansible-noarch -y
#下载后查看ansible版本
[root@westos_ansible ~]# ansible --version
ansible 2.9.27
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible	/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述注:即使ansible 不是在官方软件仓库里下载,也要配置好软件仓库,否则下载会失败

2、两台受控主机配置

第一台 westosb
设置主机名称、配置网卡、设置开机为无图形网络模式

[root@westosb200 ~]# hostnamectl set-hostname westosb200.westos.org

[root@westosb200 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens3
DEVICE=ens3
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.200
PREFIX=24
[root@westosb200 ~]# nmcli connection reload        
[root@westosb200 ~]# nmcli connection up System\ ens3 

[root@westosb200 ~]# systemctl set-default multi-user.target

第二台主机westosc同理

[root@westoslinux ~]# hostnamectl set-hostname westosc20.westos.org

[root@westoslinux ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens3 
DEVICE=ens3
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.20
NETMASK=255.255.255.0
[root@westoslinux ~]# nmcli connection reload 
[root@westoslinux ~]# nmcli connection up System\ ens3                 

[root@westoslinux ~]# systemctl set-default multi-user.target 

一、使用ansible的意义

  • 企业当中运维管理员要控制主机的数量肯定不是唯一的,要控制的服务器的数量肯定是很庞大的,使用传统的ssh去控制将会非常的麻烦。该软件也是基于ssh,以完成一对多的部署。远程实现一对多管理的工具,自动部署的工具。

二、ansible的基本信息

ansible的安装使用epel源,具体看前言实验环境设置中ansible主机设置的内容。

ansible的基本信息:

1)全局配置文件

/etc/ansible/ansible.cfg #此文件默认很少修改

2)全局主机清单文件

/etc/ansible/hosts

[root@westos_ansible ~]# cd /etc/ansible/
[root@westos_ansible ansible]# ls
ansible.cfg  hosts  roles

[root@westos_ansible ansible]# vim hosts
 44 [westos]
 45 172.25.254.200
 46 172.25.254.20
[root@westos_ansible ansible]# ansible westos --list
 hosts (2):
172.25.254.200
172.25.254.20

[root@westos_ansible ansible]# ansible westos -m shell -a 'touch /mnt/file' -k
#如果之前没有用ssh连接过此命令会失败,需要用ssh连接登陆清单中的两主机一次
[root@westos_ansible ansible]# ssh -l root 172.25.254.200
logout
[root@westos_ansible ansible]# ssh -l root 172.25.254.20
logout

在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述

三、构建ansible清单

  • 清单就算ansible控制主机的列表
  • /etc/ansible/hosts #全局清单文件

1、清单编写规则

在清单中,直接书写受管主机名或ip,每行一个
如:
westosa200.westos.org
172.25.254.20

2、设定受管主机的组(组名称)

1)清单查看

#如编写在全局清单文件中
ansible 清单中组名称 [-i 清单文件]	--list-hosts ##查看相应名称的清单
ansible ungrouped --list-hosts 	##查看没有在组中的清单内容
ansible all --list-hosts	##查看所有组的清单

[root@westos_ansible ~]# ansible westos --list
  hosts (2):
172.25.254.200
172.25.254.20
[root@westos_ansible ~]# ansible westos --list-hosts
  hosts (2):
172.25.254.200
172.25.254.20


#清单文件也可以在其他位置,但需要 --inventory 指定文件位置方能识别
vim /mnt/inventory #在mnt下编辑一个名为inventory的清单文件

ansible 清单中组组名称 --list --inventory /mnt/inventory
ansible ungrouped --list --inventory /mnt/inventory 
ansible all --list --inventory /mnt/inventory

如下为实验练习:
[root@westos_ansible ~]# vim /mnt/inventory
[root@westos_ansible ~]# cat /mnt/inventory 
[lee]
172.25.254.200
[root@westos_ansible ~]# ansible lee --list --inventory /mnt/inventory 
  hosts (1):
172.25.254.200

[root@westos_ansible ~]# vim /mnt/inventory 
[root@westos_ansible ~]# cat /mnt/inventory 
172.25.254.20
[lee]
172.25.254.200
[root@westos_ansible ~]# ansible ungrouped --list --inventory /mnt/inventory 
  hosts (1):
172.25.254.20
[root@westos_ansible ~]# ansible all --list --inventory /mnt/inventory 
  hosts (2):
172.25.254.20
172.25.254.200

在这里插入图片描述

在这里插入图片描述

2)编写清单

单层清单和嵌套清单:

[root@westos_ansible ~]# cd /etc/ansible/
[root@westos_ansible ansible]# ls
ansible.cfg  hosts  roles
[root@westos_ansible ansible]# vim hosts 
[westos] 			##单层清单
 172.25.254.200
 172.25.254.20

 [test1]			##单层清单
 westosb200.westos.org
 westosc20.westos.org

[testall:children] ##嵌套清单,内容是单层清单组名
 westos
 test1
##用ansible命令查看嵌套清单时,清单名称为testall 没有后面的:children

测试:

[root@westos_ansible ansible]# ansible test1 --list
  hosts (2):
westosb200.westos.org
westosc20.westos.org

[root@westos_ansible ansible]# ansible testall --list
  hosts (4):
172.25.254.200
172.25.254.20
westosb200.westos.org
westosc20.westos.org

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

3)主机规格的规范化操作

通过指定主机名称或IP的范围可以简化Ansible主机清单
语法: [start:end]

[root@westos_ansible ansible]# vim hosts
[testmore]
172.25.254.[10:20]

[root@westos_ansible ansible]# ansible testmore --list
  hosts (11):
172.25.254.10
172.25.254.11
172.25.254.12
172.25.254.13
172.25.254.14
172.25.254.15
172.25.254.16
172.25.254.17
172.25.254.18
172.25.254.19
172.25.254.20

在这里插入图片描述在这里插入图片描述

4)指定其他清单文件

ansible 清单组名 -i 指定清单文件位置 --list

就和上面的最后加 --inventory 指定清单文件位置一个效果

[root@westos_ansible ansible]# cat /mnt/inventory 
172.25.254.20
[lee]
172.25.254.200

[root@westos_ansible ansible]# ansible lee -i /mnt/inventory --list
  hosts (1):
172.25.254.200
[root@westos_ansible ansible]# ansible ungrouped  -i /mnt/inventory --list
  hosts (1):
172.25.254.20
[root@westos_ansible ansible]# ansible all -i /mnt/inventory --list
  hosts (2):
172.25.254.20
172.25.254.200

5)ansible命令指定清单之正则表达式

*			##所有
		##172.25.254.*
		##westos*
		
: 			##逻辑或
		##westos1:linux
		##172.25.254.100:172.25.254.200
		
:& 			##逻辑与
		##westos1:&linux
		##主机即在westos1清单也在linux清单中
		
:! 			##逻辑非
		##westos1:!linux
		##在westos1中不在linux中
		
~ 			##以关键字开头

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

测试:

vim hosts
多创立几个清单组

在这里插入图片描述

[root@westos_ansible ansible]# ansible 172* --list

[root@westos_ansible ansible]# ansible *org --list

[root@westos_ansible ansible]# ansible *westos* --list

[root@westos_ansible ansible]# ansible westos:test1 --list

[root@westos_ansible ansible]# ansible 'westos:&testmore' --list

[root@westos_ansible ansible]# ansible 'westos:!testmore' --list

[root@westos_ansible ansible]# ansible 'testmore:!westos' --list

[root@westos_ansible ansible]# ansible '~westos' --list

[root@westos_ansible ansible]# ansible '~(node|192)' --list

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述在这里插入图片描述

四、ansible配置文件参数详解

ansible执行命令格式

ansible 清单中组名称 -m 模块 -u remote_user	#-u后的remoute_user是指执行的远程用户的身份

1、配置文件的分类与优先级

配置文件优先级
/etc/ansible/ansible.cfg基本配置文件,找不到其他配置文件此文件生效
~/.ansible.cfg用户当前目录中没有ansible.cfg,则此文件生效
./ansible.cfg用户当前目录中的ansible.cfg文件优先级最高
从上表可以看出三个文件优先级依次升高

实验:ansible命令连接远程主机是否检查指纹

[root@westos_ansible ansible]# rm -fr ~/.ssh/known_hosts  ##删除ssh指纹
[root@westos_ansible ansible]# ansible westos -m ping -k

在这里插入图片描述解决该问题:
对优先级最低的基本配置文件修改

[root@westos_ansible ansible]# vim /etc/ansible/ansible.cfg 
70 # uncomment this to disable SSH key host checking
71 host_key_checking = False
修改后再执行ansible指令,就可以输入ssh密码执行了
[root@westos_ansible ansible]# ansible westos -m ping -k

在这里插入图片描述
在这里插入图片描述

实验:用ansible指令查询被控主机身份

[root@westos_ansible ansible]# ansible westos -m shell -a 'whoami' -k
##当未指定身份时,被控主机默认使用root身份

在这里插入图片描述
-u指定被控主机身份

[root@westos_ansible ansible]# ansible westos -m shell -a 'whoami' -k -u westos

在这里插入图片描述
修改基本配置文件

[root@westos_ansible ansible]# vim /etc/ansible/ansible.cfg 
107 #remote_user = root  ##默认是root
108 remote_user = westos  ##指定远程受控主机用户为westos

[root@westos_ansible ansible]# ansible westos -m shell -a 'whoami' -k

在这里插入图片描述
在这里插入图片描述在家目录创建优先级高于基本配置文件的.ansible.cfg

[root@westos_ansible ansible]# cd 
[root@westos_ansible ~]# vim .ansible.cfg
[defaults]
remote_user = lee
## 两个远程受控主机并没有创建lee用户,所以如果下面命令执行报错,就说明~/.ansible.cfg优先级更高
[root@westos_ansible ~]# ansible westos -m shell -a 'whoami' -k

在这里插入图片描述
在这里插入图片描述再cd到/mnt目录下,并在当前目录创建一个ansible.cfg,用户在当前目录中,此文件优先级最高

[root@westos_ansible ~]# cd /mnt
[root@westos_ansible mnt]# vim ansible.cfg
[defaults]
remote_user = root  

此时执行ansible指令查询远程受控主机用户应为root
[root@westos_ansible mnt]# ansible westos -m shell -a 'whoami' -k

在这里插入图片描述
在这里插入图片描述
从实验可以了解到三个配置文件的优先级比较

2、常用配置参数

[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,可以修改为shel
log_path=		##日志文件路径

[privilege_escalation]		##身份信息设定
become=		##连接后是否自动切换用户
become_method=		##设定切换用户的方式,通常用sudo
become_user=	##在受管主机中切换到的用户,通常为root
become_ask_pass		##是否需要为become_method提示输入密码,默认为false

先删除上个实验新创建的两个文件

[root@westos_ansible mnt]# rm -fr /mnt/ansible.cfg 
[root@westos_ansible mnt]# rm -fr ~/.ansible.cfg

1)inventory

指定清单路径

[root@westos_ansible mnt]# vim /etc/ansible/ansible.cfg
14 inventory      = /etc/ansible/hosts, /mnt/lee
 ##修改后 /mnt/lee也是能被系统直接识别的清单文件

[root@westos_ansible mnt]# vim /mnt/lee
[lee]
lee.lee.org

[root@westos_ansible mnt]# ansible lee --list
  hosts (1):
lee.lee.org

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2)remote_tmp

远程主机临时py命令文件存放目录
#remote_tmp     = ~/.ansible/tmp

[root@westos_ansible mnt]# cd ~/.ansible/
[root@westos_ansible .ansible]# ls
cp  tmp
[root@westos_ansible .ansible]# cd tmp/
[root@westos_ansible tmp]# ls

[root@westos_ansible tmp]# ansible westos -m shell -a 'sleep 1000' -k
## 执行ansible命令时会产生python脚本

用主机再开一个sheel窗口ssh连到ansible主机查看脚本文件
[westos@westos_student72 Desktop]$ ssh -l root 172.25.254.100
[root@westos_ansible ~]# cd ~/.ansible/tmp/
[root@westos_ansible tmp]# ls
ansible-local-6746k_5p9pyy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值