问题描述
我想在我的 Mac 上设置 ansible。我在 GNS3 中做了类似的事情并且它起作用了,但在这里我需要考虑更多因素。所以我安装了 Ansible。我在 /etc/hosts 中添加了主机名,我可以使用我在那里提供的主机名进行 ping 操作。
我已经创建了 ansible 文件夹,我将要使用该文件夹并将 ansible.cfg 放入其中:
[defaults]
hostfile = ./hosts
host_key_checking = false
timeout = 5
inventory = ./hosts
[tp-lab]
lab-acc0
当我尝试运行以下命令时:ansible tx-edge-acc0 -m ping
我收到以下错误:
[WARNING]: Invalid characters were found in group names but not replaced,use -vvvv to see details
[WARNING]: Unhandled error in Python interpreter discovery for host tx-edge-acc0: unexpected output from Python interpreter discovery
[WARNING]: sftp transfer mechanism Failed on [tx-edge-acc0]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: scp transfer mechanism Failed on [tx-edge-acc0]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: Platform unkNown on host tx-edge-acc0 is using the discovered Python interpreter at /usr/bin/python,but future installation of another Python interpreter Could change the meaning of that path. See
https://docs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html for more information.
tx-edge-acc0 | Failed! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},"changed": false,"module_stderr": "Shared connection to tx-edge-acc0 closed.\r\n","module_stdout": "\r\nerror: unkNown command: /bin/sh\r\n","msg": "MODULE FAILURE\nSee stdout/stderr for the exact error","rc": 0
知道这里有什么问题吗?非常感谢
解决方法
乍一看,当 playbook 被触发时,您的 ansible 控制器似乎没有加载配置文件(尤其是 ansible.cfg)。
(来自文档)Ansible 按以下顺序搜索配置文件,处理它找到的第一个文件并忽略其余文件:
-
$ANSIBLE_CONFIG
如果设置了环境变量。 -
ansible.cfg
如果它在当前目录中。 -
~/.ansible.cfg
如果它在用户的主目录中。 -
/etc/ansible/ansible.cfg
,默认配置文件。
编辑:为了安心,最好使用完整路径
根据评论编辑
$ cat /home/ansible/ansible.cfg
[defaults]
host_key_checking = False
inventory = /home/ansible/hosts # <-- use full path to inventory file
$ cat /home/ansible/hosts
[servers]
server-a
server-b
命令和输出:
# Supplying inventory host group!
$ ansible servers -m ping
server-a | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},"changed": false,"ping": "pong"
}
server-b | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},"ping": "pong"
}