ansible ansible.cfg 配置项详解

ansible.cfg 文件是 Ansible 的主配置文件,用于定义各种全局设置和行为。通过这个文件,你可以定制 Ansible 的工作方式,包括默认 Inventory 文件的位置、日志记录、并行任务的数量等。以下是 ansible.cfg 中常见配置项的详细解读。

1. 配置文件位置

Ansible 会按照以下顺序查找 ansible.cfg 文件:

  1. 当前目录下的 ansible.cfg
  2. 用户主目录下的 .ansible.cfg
  3. 系统级别的 /etc/ansible/ansible.cfg

如果多个文件存在,优先级从高到低依次为上述顺序。

2. 常见配置项

[defaults] 部分

这是最常用的部分,包含了大多数配置项。

  • inventory:指定 Inventory 文件或目录的路径,默认是 /etc/ansible/hosts

    inventory = /path/to/inventory
    
  • remote_user:指定远程主机上的默认用户名。

    remote_user = alice
    
  • private_key_file:指定私钥文件路径,用于 SSH 密钥认证。

    private_key_file = /path/to/private_key
    
  • ask_pass:是否提示输入 SSH 密码,默认为 False

    ask_pass = False
    
  • become:是否启用特权提升(如 sudo),默认为 False

    become = True
    
  • become_method:指定特权提升的方法,默认是 sudo

    become_method = sudo
    
  • become_user:指定特权提升后使用的用户,默认是 root

    become_user = root
    
  • ask_become_pass:是否提示输入特权提升密码,默认为 False

    ask_become_pass = False
    
  • forks:并发执行的最大任务数,默认是 5。

    forks = 10
    
  • host_key_checking:是否检查 SSH 主机密钥,默认为 True

    host_key_checking = False
    
  • log_path:指定日志文件路径,以便将所有输出写入文件。

    log_path = /var/log/ansible.log
    
  • gathering:控制何时收集事实信息,默认是 implicit(在首次使用时收集)。

    gathering = smart
    
  • fact_caching:指定事实缓存的方式,可以是 memoryjsonfileredis

    fact_caching = jsonfile
    fact_caching_connection = /tmp/fact_cache
    fact_caching_timeout = 86400
    
  • timeout:SSH 连接超时时间,默认是 10 秒。

    timeout = 30
    
  • retry_files_enabled:是否启用重试文件,默认为 False

    retry_files_enabled = False
    
[privilege_escalation] 部分

这部分配置与特权提升相关的行为。

  • become:是否启用特权提升,默认为 False

    become = True
    
  • become_method:指定特权提升的方法,默认是 sudo

    become_method = sudo
    
  • become_user:指定特权提升后使用的用户,默认是 root

    become_user = root
    
  • become_ask_pass:是否提示输入特权提升密码,默认为 False

    become_ask_pass = False
    
[ssh_connection] 部分

这部分配置 SSH 连接相关的选项。

  • scp_if_ssh:当可能时使用 SCP 而不是 SFTP,默认为 True

    scp_if_ssh = True
    
  • control_path:指定 SSH 控制套接字的路径模板,默认是 ~/.ansible/cp/ansible-ssh-%h-%p-%r

    control_path = %(directory)s/%%h-%%p-%%r
    
  • pipelining:是否启用 SSH 操作管道化,默认为 False。启用后可以减少 SSH 连接次数,但需要目标系统支持。

    pipelining = True
    
  • ssh_args:附加的 SSH 参数,默认是 -C -o ControlMaster=auto -o ControlPersist=60s

    ssh_args = -o ControlMaster=auto -o ControlPersist=60s
    
  • retries:SSH 连接失败后的重试次数,默认是 3。

    retries = 10
    
[paramiko_connection] 部分

这部分配置 Paramiko 库连接相关的选项(适用于较老版本的 Ansible,默认连接方式已改为 SSH)。

  • record_host_keys:是否记录新的主机密钥,默认为 True

    record_host_keys = False
    
  • allow_world_readable_tmpfiles:是否允许世界可读的临时文件,默认为 False

    allow_world_readable_tmpfiles = True
    
[persistent_connection] 部分

这部分配置持久连接相关的选项。

  • connect_timeout:持久连接的连接超时时间,默认是 30 秒。

    connect_timeout = 30
    
  • command_timeout:持久连接的命令超时时间,默认是 10 秒。

    command_timeout = 30
    

3. 示例配置文件

[defaults]
inventory = /etc/ansible/hosts
remote_user = ansible
private_key_file = /home/ansible/.ssh/id_rsa
ask_pass = False
become = True
become_method = sudo
become_user = root
ask_become_pass = False
forks = 10
host_key_checking = False
log_path = /var/log/ansible.log
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/fact_cache
fact_caching_timeout = 86400
timeout = 30
retry_files_enabled = False

[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False

[ssh_connection]
scp_if_ssh = True
control_path = %(directory)s/%%h-%%p-%%r
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
retries = 10

[paramiko_connection]
record_host_keys = False
allow_world_readable_tmpfiles = True

[persistent_connection]
connect_timeout = 30
command_timeout = 30

你可能感兴趣的:(Linux,ansible,linux)