Ansible 172.25.x.254
node1 172.25.x.1
node2 172.25.x.2
node3 172.25.x.3
一.Ansible对于企业运维的重大意义
二.Ansible的安装
epel源
在rhel8.x服务器上安装并启用 epel仓库
[root@student_26 yum.repos.d]# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
dnf install ansible -y
ansible --viersion
ansible的基本信息
:
/etc/ansible/ansible.conf
##全局配置文件,默认很少修改
/etc/ansible/hosts
##全局主机清单清单文件
[root@student_26 iso]# rpm -qc ansible
三.构建Anisble清单
清单就是ansible控制主机的列表
/etc/ansible/hosts
##全局清单文件
1.直接书写受管主机名或ip,每行一个
node1.westos.com
node2.westos.com
172.25.254.240
2.设定受管主机的组[组名称]
#清单查看:
ansible 清单中组名称 [-i 清单文件] --list-hosts
vim /etc/ansible/hosts
ansible ungrouped --list-hosts
ansible all --list
ansible all --list-hosts
单层清单
[list1]
node1.westos.com
node2.westos.com
[list2]
node2.westos.com
[list3]
172.25.254.240
嵌套清单
[westos:children]
list1
list3
vim /etc/ansible/hosts
测试:
[root@student_26 yum.repos.d]# ansible westos --list-hosts
[root@student_26 yum.repos.d]# ansible linux --list-hosts
[root@student_26 iso]# ansible test --list
[root@student_26 iso]# ansible -i /mnt/westos java --list-hosts
3.主机规格的范围化操作
#通过指定主机名称或IP的范围可以简化Ansible主机清单
#语法:
[start:end]
[westostest]
172.25.254.[100:108]
#测试:
[root@ansible ansible]# ansible westostest --list-hosts
hosts (9):
172.25.254.100
172.25.254.101
172.25.254.102
172.25.254.103
172.25.254.104
172.25.254.105
172.25.254.106
172.25.254.107
172.25.254.108
#4.指定其他清单文件
vim inventory
172.25.254.240
[westostest]
172.25.254.100
172.25.254.200
#测试
ansible all -i inventory --list-hosts
ansible westostest -i inventory --list-hosts
ansible ungrouped -i inventory --list-hosts
#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开头
##EXAMPLE
vim /mnt/inventory
172.25.0.250
192.25.0.250
[westos_list1]
node1.westos.com
[westos_list2]
172.25.0.2
172.25.0.1
[westos_list3]
172.25.0.1
172.25.0.3
[westos_all:children]
westos_list2
westos_list3
[]:
ansible 'westos’ -i inventory --list
ansible ‘172.*’ -i inventory --list
[:]:
ansible ‘westos_list1:westos_list2’ -i inventory --list
[:&]
ansible ‘westos_list2:&westos_list3’ -i inventory --list
[:!]
ansible ‘westos_list2:!westos_list3’ -i inventory --list
[~]
ansible ‘~westos’ -i inventory --list
####4.Ansible配置文件参数详解####
#2.常用配置参数
#[default] ##基本信息设定
inventory=
##指定清单路径
remote_user=
##在受管主机上登陆的用户名称,未指定使用当前用户
ask_pass=
##是否提示输入SSH密码,如果公钥登陆设定为false
[root@student_26 iso]# ansible test -m ping
[root@student_26 yum.repos.d]# ansible all -m ping -k
[root@student_26 yum.repos.d]# ansible all -m shell -a 'hostname' -k
受控机126
226
主机26
[root@student_26 yum.repos.d]# ansible all -m shell -a 'useradd test' -k
受控机 126 226
主机26
[root@student_26 yum.repos.d]# ansible all -m shell -a 'userdel -r test' -k
library= ##库文件存放目录
local_tmp= ##本机临时命令执行目录
remote_tmp= ##远程主机临时py命令文件存放目录
forks= ##默认并发数量
host_key_checking= ##第一次连接受管主机时是否要输入yes建立host_key
ask_pass = True
打开
注释
ask_pass= False
等同于注释
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
ansible 清单中组名称 -m 模块 -u remote_user
#受控机root
visudo
100 ricky ALL=(ALL) NOPASSWD:ALL
107 #%wheel
#1.配置文件的分类与优先级
/etc/ansible/ansible.cfg
#基本配置文件,找不到其他配置文件此文件生效
~/.ansible.cfg
#用户当前目录中没有ansible.cfg此文件生效
1 # config file for ansible -- https://ansible.com/
2 # ===============================================
3
4 # nearly all parameters can be overridden in ansible-playbook
5 # or with command line flags. ansible will read ANSIBLE_CONFIG,
6 # ansible.cfg in the current working directory, .ansible.cfg in
7 # the home directory or /etc/ansible/ansible.cfg, whichever it
8 # finds first
9
10 [defaults]
11
12 # some basic default values...
13
14 inventory = /root/.ansible/inventory
15 remote_user = westos
16 #roles_path = /etc/ansible/roles
17 host_key_checking = False
18 module_name = shell
19
20 [privilege_escalation]
21 #become=True
22 #become_method=sudo
23 #become_user=root
24 #become_ask_pass= FALSE
25
inventory中新建清单
./ansible.cfg
#优先级最高
####5.构建用户级Ansible操作环境####