1、ansible基础使用
Ansible是一款简洁、高效的运维自动化工具。只需要将ansible安装在主控机器上,就可以通过SSH协议实现针对大量受管服务器的批量化、剧本化的管理。
1.1、ansible安装和配置
[root@control ~]# yum repolist
[root@control ~]# yum -y install ansible
1.1.1、检查ansible的配置文件
[root@control ~]# rpm -ql ansible #查看ansible的所有文件
[root@control ~]# rpm -qc ansible #查看ansible的配置文件(c:config)
/etc/ansible/ansible.cfg #核心的配置文件
/etc/ansible/hosts #记录管理哪些主机
1.1.2、查看配置文件
[root@control ~]# vim /etc/ansible/ansible.cfg
[defaults] #下面是默认的配置设置
# some basic default values...
#inventory = /etc/ansible/hosts #被管理的主机的清单文件
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5 #批量管理并发个数,默认是5个进程
#poll_interval = 15
#sudo_user = root #远程管理的主机账号
#ask_sudo_pass = True #远程管理主机是否需要密码(默认需要)
#ask_pass = True
#transport = smart
#remote_port = 22 #默认端口22
#module_lang = C
#module_set_locale = False
[privilege_escalation] #权限提升(提升普通用户的权限)
#become=True #是否开启
#become_method=sudo #通过的方式是sudo
#become_user=root #提升为root
#become_ask_pass=False #是否要问题密码
1.1.3、查看版本
[root@control ~]# ansible --version
ansible 2.8.5
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)]
[root@control ~]# ls /usr/lib/python3.6/site-packages/ansible
cli config context.py executor __init__.py modules parsing plugins release.py utils
compat constants.py errors galaxy inventory module_utils playbook __pycache__ template vars
1.2、两种使用形式
(1)adhoc临时命令–Linux命令行——>手动使用ansible命令行来完成远程管理任务
(2)playbook–Shell脚本——>编写批量化执行的任务剧本,让后集中执行
1.3、ansible使用
[root@control ~]# man ansible
SYNOPSIS
ansible [host-pattern] [options]
命令 被管理的主机 操作选项
模块的指定操作:-m
[root@control ~]# ansible localhost --list-host #列出本机信息
hosts (1):
localhost
[root@control ~]# ansible localhost --list-host -v
Using /etc/ansible/ansible.cfg as config file #当前使用的配置文件
hosts (1):
localhost
[root@control ~]#
[root@control ~]# ansible localhost --list-host -vvvv #-v是看详细信息(v的次数越多信息约详细)
[root@control ~]# ansible all --list-hosts -v #all是查看所有的主机
Using /etc/ansible/ansible.cfg as config file
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'
hosts (0):
2、配置文件的优先级
2.1、配置文件
(1)全局的配置文件:/etc/ansible/ansible.cfg
(2)如果有时普通用户没有权限,或者不想对全局文件进行修改,这时用户可以在自己的家目录底下创建
用户配置文件:~/.ansible.cfg 这个配置文件优先全局的配置文件,会对全局默认配置进行覆盖
(3)工作目录配置文件:./.ansible.cfg 这个优先级最高,
2.2、ansible环境配置文件
(1)全局配置: /etc/ansible/ansible.cfg lN默认主机清单:/etc/ansible/hosts //存放受管主机列表
(2)默认角色目录:/etc/ansible/roles //存放预设的各种任务角色资料
(3)默认用户身份: root
(4)扩展配置:
~/.ansible.cfg //用户配置,会覆盖全局配置
./ansible.cfg //工作目录配置(本文采用这种配置方式),会覆盖全局、用户配置
举例: 工作目录配置文件优先级最高
[root@control ~]# ll /etc/ansible/ansible.cfg
-rw-r--r--. 1 root root 19980 Oct 11 2019 /etc/ansible/ansible.cfg
[root@control ~]# ls
anaconda-ks.cfg
[root@control ~]# vim ansible.cfg
[defaults]
inventory = myhosts