sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
sudo nano /etc/ansible/hosts
将文件编辑为:
[Web服务器] 192.168.27.1
[服务器] host1 ansible_ssh_host: 192.168.27.1 然后
sudo mkdir /etc/ansible/group_vars
sudo nano /etc/ansible/group_vars/servers
Yaml 文件创建如下:
---
ansible_ssh_user: root
然后:
ansible -m ping all
我遇到的错误如下:
ERROR! Attempted to read "/etc/ansible/hosts" as YAML: Syntax Error while loading YAML.
The error appears to have been in '/etc/ansible/hosts': line 46, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
[Webserver]
192.168.27.1
^ here
Attempted to read "/etc/ansible/hosts" as ini file: /etc/ansible/hosts:50: Expected key=value host variable assignment, got: 192.168.27.1
请帮我解决这个问题。
答案1
看起来就像这
尝试进入文件并搜索该行中放错的空格。根本原因是/usr/local/etc/ansible/hosts
文件中的前导空格,解决方案是搜索此类空格并将其删除。论坛帖子的关键部分记录在下面。
这是由 inventory 文件中带有前导空格的注释引起的
/usr/local/etc/ansible/hosts
。这是此 Ansible 版本的新行为!因此,如果您习惯在 hosts 文件中写入类似这样的内容:
[web] # legacy servers webserver-[1:2].company.com
改成:
[web] # legacy servers webserver-[1:2].company.com
或者
[web] webserver-[1:2].company.com # legacy servers