macos 上配置 ansible 环境变量的核心是通过 ~/.zshrc 设置 ansible_config、ansible_remote_user 等变量并配合 ansible.cfg 文件,按优先级控制配置生效,运行 source ~/.zshrc 后用 ansible-config dump 验证。
macos 上配置 ansible 自动化运维所需的 shell 环境变量,核心是让 ansible 命令能正确读取连接参数、模块路径、并发数等设置,同时避免每次手动指定配置文件或参数。关键不在于“改系统级环境变量”,而在于按优先级控制配置生效顺序,并适配 macos 的 shell(zsh 为默认)。
确认当前 Shell 和配置文件位置
macOS Catalina 及之后版本默认使用 zsh,配置文件通常是:
-
~/.zshrc(推荐:用户级、每次新终端都加载) -
~/.zprofile(登录 shell 加载,适合 PATH 类全局变量) - 避免修改
/etc/zshrc或/etc/profile,除非需系统级统一配置
设置常用 Ansible 环境变量
在 ~/.zshrc 中添加以下导出语句(根据实际需求启用):
-
指定自定义配置文件路径:
export ANSIBLE_CONFIG="$HOME/.ansible/ansible.cfg" -
设置默认远程用户:
export ANSIBLE_REMOTE_USER="admin" -
禁用 SSH 密钥检查(仅测试环境):
export ANSIBLE_HOST_KEY_CHECKING=False -
调整并发数(提升批量执行效率):
export ANSIBLE_FORKS=10 -
指定 Python 解释器(防止远程主机无 python3):
export ANSIBLE_PYTHON_INTERPRETER="/usr/bin/python3"
保存后运行 source ~/.zshrc 生效。可用 echo $ANSIBLE_REMOTE_USER 验证。
配合 ansible.cfg 文件使用更稳妥
环境变量适合快速覆盖,但复杂配置(如 inventory 路径、插件目录、日志开关)建议写入 ansible.cfg。例如创建 ~/.ansible/ansible.cfg:
macOS 微信消息自动化工具。通过 GUI 自动化实现:发送消息给指定联系人、读取聊天内容、监控新消息。适用于需要自动化微信操作的场景,如定时发送、批量回复、消息备份等。依赖 peekaboo 进行屏幕截图和 UI 交互。仅支持 macOS。开源地址:https://github.com/chairmanmia...
[defaults] inventory = $HOME/ansible/inventory remote_user = admin forks = 10 host_key_checking = False log_path = $HOME/ansible/ansible.log library = $HOME/ansible/library
注意:$HOME 在 ansible.cfg 中会被自动展开,无需用 ${HOME};但环境变量中必须用 $HOME。
验证配置是否生效
执行命令确认当前生效的配置:
-
ansible-config dump --only-changed:只显示被修改过的配置项 -
ansible all -m ping -vvv:详细输出中会显示读取的 config 文件路径和使用的 remote_user -
ansible-inventory --graph:检查 inventory 是否按预期加载
若看到 Using /Users/xxx/.ansible/ansible.cfg as config file,说明环境变量 ANSIBLE_CONFIG 已正确生效。










