"Could not resolve hostname" Ansible

"Could not resolve hostname" Ansible

我根据this tutorial创建了我的第一个ansible playbook,所以它看起来像这样:

---
- hosts: hostb
  tasks:
      - name: Create file
        file:
            path: /tmp/yallo
            state: touch

- hosts: my_hosts
  sudo: yes
  tasks:
      - name: Create user
        user:
            name: mario
            shell: /bin/zsh

      - name: Install zlib
        yum:
            name: zlib
            state: latest

但是,我不知道应该将哪些主机放入我的主机文件中。我现在有这样的东西:

[my_hosts]
hostA
hostB

显然,它不起作用,我明白了:

ssh: Could not resolve hostname hostb: Name or service not known

那么我应该如何更改我的主机文件?我是 ansible 的新手,所以非常感谢您的帮助!

好的,Ansible 清单可以基于以下格式:

  • 主机名 => IP Address
  • 主机名 => DHCP or Hosts file hostname reference localhost/cassie.local
  • 创建您自己的别名 => hostname ansible_host=IP Address
  • 主机组 => [group_name]

这是您可以使用的最基本的结构。

Example

# Grouping
[test-group]

# IP reference
192.168.1.3

# Local hosts file reference
localhost

# Create your own alias
test ansible_host=192.168.1.4

# Create your alias with port and user to login as
test-2 ansible_host=192.168.1.5 ansible_port=1234 ansible_user=ubuntu

Grouping of hosts will only end when the end of file or another group detected. So if you wish to have hosts that don't belong to a group, make sure they're defined above the group definition.

即上面示例中的所有内容都属于 test-group,如果您执行以下操作;它将在所有主机上执行:

ansible test-group -u ubuntu -m ping


ansible 是区分大小写的主机名,在您的清单文件中是 hostB,在您的剧本中是 hostb 我认为它显示“名称或服务未知”错误的方式

将 playbook 中的主机名更改为 hostB