Ansible 配置文件

1,350 阅读2分钟

开始

Ansible 的配置文件的查找顺序如下:

  • 环境变量 ANSIBLE_CONFIG
  • 当前目录下的 ansible.cfg
  • home 目录下的 ~/.ansible.cfg
  • /etc/ansible/ansible.cfg

Ansible 使用找到的第一个文件,忽略其余的。

example file on Github

ansible-config

语法:

ansible-config [view|dump|list] [--help] [options] [ansible.cfg]

参数:

  • --version
  • -c <CONFIG_FILE>, --config <CONFIG_FILE>:默认按顺序找到的第一个
  • -h, --help
  • -v, --verbose-vvv 更详细,-vvvv开启连接 debug 模式

动作:

  • list:查看 lib/constants.py
  • dump:查看当前设置,合并指定的 ansible.cfg,使用 --only-changed参数可显示和默认相比改变的配置
  • view:查看当前

环境变量

环境变量会覆盖所有配置文件读取到的配置,可以使用 ansible-config 工具查看详情,也可见 lib/constants.py

配置文件

配置文件 ansible.cfg 切割成不同段,简介绍几个常用的。

defaults

inventory

主机目录

inventory = /etc/ansible/hosts

forks

与主机通信时的默认并行进程数,默认的 5 实在太小了,只要系统承受的住可以尽可能增加

forks = 5

poll_interval

异步任务的回查频率,秒

poll_interval = 15

sudo_user

sudo 使用的用户

sudo_user = root

ask_sudo_pass

执行 sudo 之前是否询问 sudo 密码

ask_sudo_pass = True

ask_pass

是否自动弹出密码

ask_pass = True

transport

Ansible 默认使用支持 ControlPersistOpenSSH,但有些系统的 OpenSSH 版本太旧,不支持 ControlPersist,此时会使用 Paramiko

smart 根据操作系统和 ssh 版本在 sshparamiko 中自动选择。

其他选项有 local 等。

transport = smart

remote_port

remote_port = 22

host_key_checking

密钥检测

host_key_checking = False

sudo_exe

sudo_exe = sudo

sudo_flags

sudo_flags = -H -S -n

timeout

SSH timeout

timeout = 10

remote_user

playbook 默认使用的用户。ansible 默认使用当前用户

remote_user = root

log_path

logging 功能默认关闭,除指定此路径

log_path = /var/log/ansible.log

module_name

ansible 命令(-m)默使用的模块

module_name = command

executable

用于 sudo 命令下

executable = /bin/sh

private_key_file

private_key_file = /path/to/file

privilege_escalation

become=True
become_method=sudo
become_user=root
become_ask_pass=False

paramiko_connection

record_host_keys=False

pty=False

look_for_keys = False

host_key_auto_add = True

ssh_connection

ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
control_path_dir = ~/.ansible/cp
control_path =
pipelining = False
scp_if_ssh = smart
transfer_method = smart
sftp_batch_mode = False
use_tty = True
retries = 3

persistent_connection

accelerate

selinux

colors

always = no
context = 3