Ansible 172.25.x.254
node1 172.25.x.1
node2 172.25.x.2
node3 172.25.x.3
实验环境配置:(需要3台主机)192.168.10.60 192.168.10.65 192.168.10.66
用xshell打开:
vim /etc/rc.d/rc.local
(本地软件仓库已经搭好)
(网络软件仓库已经配置好)
####1.Ansible对于企业运维的重大意义####
####2.Ansible
的安装
####
epel
源
dnf install ansible -y
ansible --viersion
ansible
的基本信息
:
/etc/ansible/ansible.conf ##
全局配置文件
,
默认很少修改
/etc/ansible/hosts ##全局主机清单清单文件
(ansible和shell的区别)
(ansible更适合批量操作)
####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-hosts
ansible all --list-hosts
#
单层清单
#
[list1]
node1.westos.com
node2.westos.com
[list2]
node2.westos.com
[list3]
172.25.254.240
#
嵌套清单
#
[westos:children]
list1
list3
#
测试
:
[root@ansible ansible]# ansible list1 --list-hosts
hosts (1):
172.25.254.100
[root@ansible ansible]# ansible list2 --list-hosts
hosts (1):
172.25.254.200
[root@ansible ansible]# ansible list3 --list-hosts
hosts (1):
172.25.254.240
[root@ansible ansible]# ansible westos --list-hosts
hosts (3):
172.25.254.100
172.25.254.200
172.25.254.240
#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
配置文件参数详解
####
#
ansible
清单中组名称
-m
模块
-u remote_user
#
#1.
配置文件的分类与优先级
/etc/ansible/ansible.cfg #
基本配置文件
,
找不到其他配置文件此文件生效
~/.ansible.cfg #用户当前目录中没有
ansible.cfg
此文件生效
./ansible.cfg #优先级最高
ansible 的优先级顺序
#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
,
可以修改为
shell
log_path=
##
日志文件路径
[privilege_escalation]
##
身份信息设定
become= ##连接后是否自动切换用户
become_method= ##设定切换用户的方式
,
通常用
sudo
become_user= ##在受管主机中切换到的用户
,
通常为
root
become_ask_pass ##是否需要为become_method提示输入密码,默认为false
####5.
构建用户级
Ansible
操作环境
####
(设置密码)
(生成密钥)
总结