You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行vi编辑Ansible配置文件显示为空,需获取带注释的默认配置文件

解决Ansible默认配置文件为空的问题

1. 检查路径是否正确

你执行命令时遗漏了根目录前缀/,正确的配置文件路径应该是/etc/ansible/hosts和/etc/ansible/ansible.cfg。如果之前误在当前目录下创建了空的etc/ansible/文件夹及文件,先执行以下命令删除:

rm -rf ./etc

之后再用正确路径打开文件:

vi /etc/ansible/hosts
vi /etc/ansible/ansible.cfg

2. 生成带注释的默认配置文件

如果系统中原本没有默认配置文件,可以通过复制Ansible提供的示例文件来获取带完整注释的配置:

复制示例ansible.cfg

cp /usr/share/doc/ansible/examples/ansible.cfg /etc/ansible/

复制示例hosts文件

cp /usr/share/doc/ansible/examples/hosts /etc/ansible/

3. 手动添加默认带注释配置(如果示例文件不存在)

如果找不到系统中的示例文件,可以手动创建并添加以下默认配置内容:

默认ansible.cfg(带注释)

# config file for ansible -- https://ansible.com/
# ===============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[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
#poll_interval  = 15
#sudo_user      = root
#ask_sudo_pass = True
#ask_pass      = True
#transport      = smart
#remote_port    = 22
#module_lang    = C
#module_set_locale = False

# plays will gather facts by default, which contain information about
# the remote system.
# gather_facts = True

# some connection options
# ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
# scp_if_ssh = True
# sftp_batch_mode = False
# private_key_file = ~/.ssh/id_rsa
# timeout = 10
# tunnel = False
# environment = {}
# no_log = False
# system_warnings = True
# deprecation_warnings = True
# command_warnings = False

# set plugin path directories here, separate with colons
# action_plugins     = /usr/share/ansible/plugins/action
# become_plugins     = /usr/share/ansible/plugins/become
# cache_plugins      = /usr/share/ansible/plugins/cache
# callback_plugins   = /usr/share/ansible/plugins/callback
# connection_plugins = /usr/share/ansible/plugins/connection
# lookup_plugins     = /usr/share/ansible/plugins/lookup
# inventory_plugins  = /usr/share/ansible/plugins/inventory
# vars_plugins       = /usr/share/ansible/plugins/vars
# filter_plugins     = /usr/share/ansible/plugins/filter
# test_plugins       = /usr/share/ansible/plugins/test
# terminal_plugins   = /usr/share/ansible/plugins/terminal
# strategy_plugins   = /usr/share/ansible/plugins/strategy

# by default, ansible will use the 'linear' strategy but you may want to try
# another one
# strategy = linear

# by default callbacks are not loaded unless you provide a list
# callback_whitelist = timer, mail

# determine whether includes in tasks and handlers are "static" by default.
# static includes are read during playbook parsing rather than execution.
# this can be overridden per-include with static: yes/no
# default_static_inventory = True

# controls if a missing handler for a notification event is an error or a warning
# error_on_missing_handler = True

# change this path to point to your 'host_key_checking' setting
# host_key_checking = False

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

# [paramiko_connection]

# uncomment this line to cause the paramiko connection to not record new host
# keys encountered.  Increases performance on new host additions.  Setting works independently of the host key checking setting above.
# record_host_keys=False

# by default, Ansible requests a pseudo-terminal for commands executed under sudo. Uncomment this line to disable this behavior.
# pty=False

# [ssh_connection]

# ssh arguments to use
# Leaving off ControlPersist will result in poor performance, so use
# paramiko on older platforms rather than removing it
# ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s

# The base directory for the ControlPath sockets.
# control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r

# Enabling pipelining reduces the number of SSH operations required to
# execute a module on the remote server. This can result in a significant
# performance improvement when enabled, however when using "sudo:" you must
# first disable 'requiretty' in /etc/sudoers
# pipelining = False

# [persistent_connection]

# Enable persistent connections
# connect_timeout = 30
# command_timeout = 30
# connect_retry_timeout = 15
# retries = 3
# socket_path = ~/.ansible/pc/

# [colors]
# highlight = white
# verbose = blue
# warn = bright purple
# error = red
# debug = dark gray
# deprecate = purple
# skip = cyan
# unreachable = red
# ok = green
# changed = yellow
# diff_add = green
# diff_remove = red
# diff_lines = cyan

默认hosts文件(带注释)

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or IP addresses
#   - A hostname/IP can be a member of multiple groups

# Example 1: Ungrouped hosts, specify before any group headers.

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

# Example 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

# Example 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

内容的提问来源于stack exchange,提问作者Ayush Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 20:25:58