Ansible配置

来自CloudWiki
跳转至: 导航搜索

概述

ansible主要有两个文件:

ansible.cfg 和 inventory文件

Ansible.cfg是ansible自动化任务所用的一个核心配置文件,

Ansible可同时操作属于一个组的多台主机,是通过inventory文件配置来实现的,

配置文件ansible.cfg

查看配置文件

从下面文件的输出中可以看到配置文件的位置。

[root@localhost ansible]# ansible --version

ansible 2.10.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/local/Python3/lib/python3.7/site-packages/ansible
  executable location = /usr/local/Python3/bin/ansible
  python version = 3.7.5 (default, Feb 12 2021, 16:30:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

新建配置文件

在以下三个位置任意之一 可以建立ansible配置文件:

1./etc/ansible/ansible.cfg

2.~/ansible.cfg

3.当前目录下的ansible.cfg(常用)

在大多数场景下默认的配置就能满足大多数用户的需求。在一些特殊场景下,用户还需要自行修改这些配置文件,安装后如果没有在上述三个位置找到配置文件,那么在HOME目录新建一个.ansible.cfg文件即可。

最简实例

vi ~/ansible.cfg

[defaults]
inventory = /root/ansible_hosts
host_key_checking = False

当ansible首次去进行ssh连接一个服务器的时候,由于在本机的~/.ssh/known_hosts文件中并有fingerprint key串,ssh第一次连接的时候一般会提示输入yes 进行确认为将key字符串加入到 ~/.ssh/known_hosts 文件中。打开 host_key_checking = False的注释。可以实现跳过 ssh 首次连接提示验证部分

配置完这个文件后可通过ansible --version查看,

这时可以看到 配置文件已经显示了:

[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 
(default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]. This feature will be removed from ansible-core in version 2.12.
 Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
/usr/local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
  from cryptography.exceptions import InvalidSignature
ansible [core 2.11.12] 
  config file = /root/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
  jinja version = 2.11.3
  libyaml = True

配置参数介绍

  • [defaults] 为配置文件的大部分设置
  • [privilege_escalation] 中定义对受管主机执行特权升级
  • [paramiko_connection]、[ssh_connection] 和 [accelerate] 用于优化与受管主机的连接
  • [selinux] 定义如何配置 SELinux 交互

Ansible常见的配置参数如下。

[defaults]
inventory = ~/ansible_hosts# 表示主机清单inventory文件的位置。
forks = 5# 并发连接数,默认为5。
sudo_user = root# 设置默认执行命令的用户。
ask_pass= True # 是否需要用户输入连接密码,如果使用公钥验证,需要设置为false
remote_user= root # 在受管主机上登录用户,未指定则使用当前用户
remote_port = 22# 指定连接被管节点的管理端口,默认为22端口。建议修改,能够更加安全。
host_key_checking = False# 设置是否检查SSH主机的密钥,值为True/False。关闭后第一次连接不会提示配置实例。
timeout = 60# 设置SSH连接的超时时间,单位为秒。
log_path = /var/log/ansible.log# 指定一个存储Ansible日志的文件(默认不记录日志)。

[privilege_escalation] 
become=True  # 是否sudo
become_method=sudo  # sudo方式
#become_user=root  # sudo后变为root用户
#become_ask_pass=False  # sodu后是否验证密码

其中参数的取值范围及更加详细的配置请参考官方文档:https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg。

思考:怎样用python读写配置文件?

主机清单(inventory)概述

Ansible可同时操作属于一个组的多台主机,是通过主机清单文件配置来实现的,

组与主机的关系也是由主机清单文件来定义的。

主机可以从属于组,后者通常用于标识主机的角色。一个主机可以属于多个组

可以通过两种方式定义主机清单:通过文本文件定义静态主机清单,或从外部 provider 生成动态主机清单


编写静态清单概述

可以通过两种方式定义主机清单:通过文本文件定义静态主机清单,或从外部 provider 生成动态主机清单。

默认inventory文件路径为/etc/ansible/hosts,我们也可以通过Ansible的配置文件来指定inventory文件位置。除默认文件外,可以同时使用多个inventory文件,也可以从动态源或云上拉取inventory配置信息。

  • Ansible 静态主机清单在类似于 INI 的文本文件中定义
  • 每一部分的开头为方括号 [ ] 括起的主机组名称
  • 其后是主机条目,它们列出组中的每一台受管主机,各自占据一行
  • 主机条目由受管主机的主机名称或 IP 地址组成
  • 主机条目也可定义 Ansible 与受管主机的通信方式,其中包含传输和用户帐户信息
  • 主机清单文件的默认位置是 /etc/ansible/hosts
 
[webservers]
web1.example.com
web2.example.com
192.0.2.42

主机清单测试

测试计算机是否在清单中

 ansible 主机名 --list-hosts


列出组中所有主机

 ansible 组名 --list-hosts
[root@ecs-c6f9 ~]# ansible 114.116.251.90  --list-hosts
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the 
controller starting with Ansible 2.12. Current version: 3.6.8 (default, Sep 10 
2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]. This feature will be 
removed from ansible-core in version 2.12. Deprecation warnings can be disabled 
by setting deprecation_warnings=False in ansible.cfg.
  hosts (1):
    114.116.251.90
[root@ecs-c6f9 ~]# ansible master --list-hosts
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the 
controller starting with Ansible 2.12. Current version: 3.6.8 (default, Sep 10 
2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]. This feature will be 
removed from ansible-core in version 2.12. Deprecation warnings can be disabled 
by setting deprecation_warnings=False in ansible.cfg.
  hosts (1):
    localhost

主机清单(inventory)分类

不分组设置

一个不分组的inventory文件示例如下。

foo.example.com
192.168.100.10
one.example.com
two.example.com
three.example.com

分组设置

一个对主机进行分组的inventory文件:

[webservers]
foo.example.com
192.168.100.10
[dbservers]
one.example.com
two.example.com
three.example.com

其中方括号[]中是组名,用于对系统进行分类,便于对不同系统进行个别的管理。一个系统可以属于不同的组,比如一台服务器可以同时属于webserver组和dbserver组。这时属于两个组的变量都可以为这台主机所用。

非标准端口设置

非标准端口下的主机清单这样写
blue.example.com:4399 

连续大量的清单

可以指定主机名称或 IP 地址的范围来简化 Ansible 主机清单。可以指定数字范围和字母范围。范围具有下列语法:

#3.连续大量的清单
[web]
db-[99:101]-node.example.com
192.168.[1:5].[0:255] //192.168.1.0-192.168.5.255下的所有主机

多个主机组构成的组

多个主机组也可构成组,通过 :children 后缀来实现。

[a]
1.example.com
[b]
2.example.com

[c:children]
a
b

使用动态清单

Ansible inventory 内容也可以动态生成

动态清单信息的来源包括公共/私有云提供商、Cobbler 系统信息和 LDAP 数据库,或者配置管理数据库 (CMDB)

Ansible 含有处理来自最常见提供商的动态主机、组和变量信息的脚本,如 Amazon EC2、Cobbler、Rackspace Cloud 和OpenStack 等提供商

对于云提供商,必须在脚本能够访问的文件中定义身份验证和访问权限信息

主机清单变量

设置连接参数

对于每一个host,还可以选择连接类型和连接用户名。

#6.设置连接参数
[member]
localhost ansible_connection=local ansible_user=user
other1.example.com ansible_connection=ssh  ansible_ssh_user=pdchaan



主机清单中常用的连接参数:

  • ansible_connection: ssh连接方式,可以指定ssh local paramiko
  • ansible_host: ansible连接的主机地址。如果在主机清单中起了一个不同的别名,那么需要用这个参数指定主机的IP或者主机名
  • ansible_port:ssh端口号 默认是22
  • ansible_user:ssh连接时候使用的默认用户名
  • ansible_ssh_pass: ssh连接时候使用的密码。不过不建议用这个参数因为是明文的

更多参数连接可以参考官网:https://docs.ansible.com/ansible/latest


设置其他变量

分配变量给主机:

[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909


定义属于整个组的变量:

 
[atlanta]
host1
host2

[atlanta:vars]
ntp_server =ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

这些变量定义后可在playbooks中使用。

主机清单示例

静态清单示例

示例1:

[root@localhost ~]# mkdir /etc/ansible

[root@localhost ~]# cd /etc/ansible

[root@localhost ansible]# vi hosts

[master]
localhost ansible_connection=local ansible_ssh_user=root
10.0.0.30 ansible_ssh_user=root
[slave]
10.0.0.32 ansible_ssh_user=root


有可能每台机器登录的用户名都不一样,这里指定每台机器连接的SSH登录用户名,在执行Ansible命令时就不需要再指定用户名。如果不指定用户名,Ansible就会尝试使用本机已登录的用户名登录远程主机。

示例2:

cat /etc/ansible/hosts
# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no 连续大量主机清单[start:end]
# leading 0s:

## db-[99:101]-node.example.com


示例3:

#1.
green.example.com

#2.
[webservers]
alpha.example.org
beta.example.org

#3.
[web]
db-[99:101]-node.example.com
192.168.[1:5].[0:255] //192.168.1.0-192.168.5.255下的所有主机

#4.
blue.example.com:4399 

#5.
[a]
1.example.com
[b]
2.example.com
[c:children]
a
b

#6.
[member]
localhost ansible_connection=local ansible_user=user

以上部分 来自CSDN博主「王小番茄」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_42680332/article/details/110794173

主机清单测试

测试计算机是否在清单中

 ansible 主机名 --list-hosts


列出组中所有主机

 ansible 组名 --list-hosts
[root@ecs-c6f9 ~]# ansible 114.116.251.90  --list-hosts
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the 
controller starting with Ansible 2.12. Current version: 3.6.8 (default, Sep 10 
2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]. This feature will be 
removed from ansible-core in version 2.12. Deprecation warnings can be disabled 
by setting deprecation_warnings=False in ansible.cfg.
  hosts (1):
    114.116.251.90
[root@ecs-c6f9 ~]# ansible master --list-hosts
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the 
controller starting with Ansible 2.12. Current version: 3.6.8 (default, Sep 10 
2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]. This feature will be 
removed from ansible-core in version 2.12. Deprecation warnings can be disabled 
by setting deprecation_warnings=False in ansible.cfg.
  hosts (1):
    localhost

主机清单覆盖

覆盖 Inventory 的默认位置

使用 - -inventory PATHNAME或者 –i PATHNANE选项指定清单文件的位置

PATHNAME是所需清单文件的路径

配置实例

ansible.cfg

vi ~/ansible.cfg

[defaults]
inventory = ~/ansible_hosts
host_key_checking = False

inventory文件

[root@localhost ~]# mkdir /etc/ansible

[root@localhost ~]# cd /etc/ansible

[root@localhost ansible]# vi ansible_hosts

[master]
localhost ansible_connection=local ansible_ssh_user=root
10.0.0.30 ansible_ssh_user=root
[slave]
10.0.0.32 ansible_ssh_user=root

有可能每台机器登录的用户名都不一样,这里指定每台机器连接的SSH登录用户名,在执行Ansible命令时就不需要再指定用户名。如果不指定用户名,Ansible就会尝试使用本机已登录的用户名登录远程主机。