1.  
  2. 主页
  3.  / 
  4. Ansible 教程
  5.  / 
  6. Ansible 配置文件

Ansible 配置文件

主配置文件

Ansible 获取配置的优先级:

  • ANSIBLE_CFG:首先,Ansible命令会先检查环境变量,及这个环境变量将指向的配置文件;
  • ./ansible.cfg:其次,将会检查当前目录下的ansible.cfg配置文件;
  • ~/.ansible.cfg:再次,将会检查当前用户home目录下的.ansible.cfg配置文件;
  • /etc/ansible/ansible.cfg:最后,将会检查在安装Ansible时自动生成的配置文件。

Ansible 的主配置文件(/etc/ansible/ansible.cfg)中,大部分的配置内容无需进行修改。

下面说说常用配置。

[defaults]
#inventory      = /etc/ansible/hosts    # 主机列表配置文件
#library        = /usr/share/my_modules/    # 库文件存放位置
#remote_tmp     = ~/.ansible/tmp    # 临时 py 命令文件存放在远程目录上,任务执行完成则删除
#local_tmp      = ~/.ansible/tmp    # 本机的临时命令执行目录
#forks          = 5    # 默认开启的进程数量
#poll_interval  = 15    # 默认轮询时间间隔
#sudo_user      = root    # 默认 sudo 用户
#ask_sudo_pass = True    # 是否需要sudo密码
#ask_pass      = True    # 是否需要密码
#remote_port    = 22    # 默认远程连接端口
#host_key_checking = False    # 检查对应服务器的 host_key ,建议取消注释
#log_path = /var/log/ansible.log    # 日志文件,建议启用
#module_name = command    # 默认模块,可以修改为 shell 模块
#timeout = 10  # 默认超时时间

主机清单配置

默认配置文件位置为:/etc/ansible/hosts

配置规则

  • “#”开始表示注释,空格行被忽略
  • 一个主机名或者IP地址可以出现在多个分组中
  • 主机列表可以使用主机名或者IP地址
  • 使用[start:end]表示连续的主机,只支持升序排列
  • 使用[]设置远程主机分组。[]内写的是组名
  • 分组支持嵌套,一个组可以包含其他组,新组名中以“:children“后缀
# 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
# leading 0s:

## db-[99:101]-node.example.com
这篇文章对您有用吗?

我们要如何帮助您?

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注