ansible实验环境部署
建立3台主机
1修改主机名
hostnamectl set-hostname ansible.westos.org
2.设置网络
vim /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=EthernetBOOTPROTO=none
NAME=ens160
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.91.88
PREFIX=24
GETWAY=192.168.91.67
DNS=114.114.114.114
nmcli connection reloadnmcli connection up ens160
通过ping www.baidu.com来判断是否能够连接外网
3.配置软件仓库
先挂载
mkdir /rocky8.8
mount /iso/Rocky-8.8-x86_64-dvd1.iso /rocky8.8/
然后
vim /etc/yum.repos.d/gxy.repo
[AppStream]name=AppStream
baseurl=file:///rocky8.8/AppStream
gpgcheck=0
[BaseOS]
name=BaseOS
baseurl=file:///rocky8.8/BaseOS
gpgcheck=0
下载httpd关闭防火墙
dnf install httppd -y
systemctl enable --now httpd
systemctl disable --now firewalld.service
配置epel源
vim /etc/yum.repos.d/epel.repo
[epel]name=epel
baseurl=https://mirrors.aliyun.com/epel-archive/8.8/Everything/x86_64/
gpgcheck=0
umount /rocky8.8mount /iso/Rocky-8.8-x86_64-dvd1.iso /var/www/html/westos
vim /etc/yum.repos.d/gxy.repo
[AppStream]name=AppStream
baseurl=file:///var/www/html/westos/AppStream
gpgcheck=0
[BaseOS]
name=BaseOS
baseurl=file:///var/www/html/westos/BaseOS
gpgcheck=0
部署另外两台主机
主机A:
hostnamectl set-hostname AAA.westos.org
vim /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=EthernetBOOTPROTO=none
NAME=ens160
DEVICE=ens160
ONBOOT=yes
DNS=114.114.114.114
IPADDR=192.168.91.89
PREFIX=24
nmcli connection reloadnmcli connection up ens160
主机B:
hostnamectl set-hostname AAA.westos.org
vim /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=EthernetBOOTPROTO=none
NAME=ens160
DEVICE=ens160
ONBOOT=yes
DNS=114.114.114.114
IPADDR=192.168.91.90
PREFIX=24
nmcli connection reloadnmcli connection up ens160
若是系统资源不够用,可以考虑关闭A和B两台主机的图形模式
systemctl set-default multi-user.target
init 3
ansible对于企业的重大意义及安装方法
对于ansible主机
安装ansible
dnf install ansible -ydnf install ansible
vim /etc/ansible/host
[westos]192.168.91.89
192.168.91.90
ansible westos -m shell -a 'hostname' -uroot -k

ansible中清单的书写及指定
vim /etc/ansible/host
172.25.254.9[westos]
192.168.91.89
192.168.91.90
[linux]
192.168.55.55
192.168.91.90
ansible westos --list-hostsansible all --list-hosts
ansible ungrouped --list-hosts
cd /mnt
vim testwestos
ansible testwestos --list-hosts -i /mnt/testwestos


vim /etc/ansible/host
[westosall:children]westos
linux
ansible westosall --list-hosts

ansible "westos:linux" --list-hosts
ansible "westos:&linux" --list-hosts
ansible 'westos:!linux' --list-hosts
ansible "westos:!linux" --list-hosts
ansible '~(192|172)' --list-hosts
|------------------------|-----------------------------------------------------|
| 172.25.254.[100:108] | 从100到108 |
| * | 所有 ##172.25.254.* ##westos* |
| : | 逻辑或 ##westos:linux ##172.25.254.100:172.25.254.200 |
| :& | 逻辑与 ##westos:&linux ## 主机即在 westos1 清单也在 linux 清单中 |
| :! | 逻辑非 ##westos:!linux ## 在 westos1 中不在 linux 中 |
| ~ | 以关键字开头 ~(str1|str2) ## 以条件 1 或者条件 2 开头 |
ansible的配置文件详解及企业配置方案
ansible 清单中组名称 -m 模块 -u remote_user
ansible westos -m shell -a 'whoami' -uroot -kansible westos -m shell -a 'whoami' -uxxx -k
配置文件的分类与优先级
|--------------------------|---------------------------|
| /etc/ansible/ansible.cfg | 基本配置文件,找不到其他配置文件此文件生效 |
| ~/.ansible/.ansible.cfg | 用户当前目录中没有ansible.cfg此文件生效 |
| ./ansible.cfg | 优先级最高 |


新的清单中的IP应为192.168.91.89及192.168.91.90







[defaults]
inventory = /etc/ansible/hostos, ~/.ansible/inventory
host_key_checking = False
remote_user = devops
module_name = shell
ask_pass = True
开启免密认证


ansible westos -m copy -a 'src=~/.ssh/id_rsa.pub dest=/home/devops/.ssh/authorized_keys mode=600 owner=devops group=devops'


ansible westos -m shell -a 'echo "devops ALL=(ALL) NOPASSWD:ALL " >> /etc/sudoers' -uroot -k



|--------------------|-------------------------------|
| [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= | 连接后是否自动切换用户 |
| become_method= | 设定切换用户的方式,通常用sudo |
| become_user= | 在受管主机中切换到的用户,通常为root |
| become_ask_pass | 是否需要为become_method提示输入密码,默认为false |