4

我正在尝试使用 Ansible 来自动化我的工作站。我一直在关注本教程作为介绍。但我不断收到使用该ansible-pull命令的警告。

做完后sudo ansible-pull -U https://github.com/plsergent/setup-user-friendly.git我得到了[WARNING]: Could not match supplied host pattern, ignoring: <local machine hostname>

这是我的/etc/ansible/hosts文件:

[localhost]
127.0.0.1

[localhost:vars]
ansible_connection=local

和我的local.yml文件:

- hosts: localhost
  become: true
  pre_tasks:
    - name: update repositories
      apt: update_cache=yes
      changed_when: False

  tasks:
    - include: tasks/packages.yml
    - include: tasks/users.yml
    - include: tasks/cron.yml

有没有办法摆脱这个警告?

谢谢

注意:当我使用以下命令运行我的剧本时,我没有任何警告ansible-playbooksudo ansible-playbook local.yml

4

3 回答 3

4

有没有办法摆脱这个警告?

您收到警告是因为ansible-pull构建了一个命令行,其中包含这样--limit构建的选项:

node = platform.node()
host = socket.getfqdn()
limit_opts = 'localhost,%s,127.0.0.1' % ','.join(set([host, node, host.split('.')[0], node.split('.')[0]]))

Whereplatform.node()返回您的系统节点名称(即 的输出uname -n)并socket.getfqdn()尝试返回您系统的完全限定域名。这意味着 ansible 命令行将如下所示:

ansible-playbook -l localhost,yourhostname.example.com,yourhostname,127.0.0.1 ...

碰巧的是,当您在--limit参数中提供的主机名与清单中的主机不匹配时,您会收到Could not match supplied host pattern错误消息。您可以像这样重现:

$ ansible all -m ping  -l host-that-does-not-exist
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'

 [WARNING]: Could not match supplied host pattern, ignoring: host-that-does-not-exist

如果警告真的让您感到困扰,您可以通过在命令行中添加一个-i选项来明确地将您的本地主机名包含在 ansible 清单中来避免它。ansible-pull您可以包括内联主机:

ansible-pull -i localhost,myhostname -U ...

或者,您可以将其显式指向现有库存文件:

ansible-pull -i /etc/ansible/hosts -U ...
于 2019-04-24T01:24:48.253 回答
1

要添加到@larsks 答案,似乎使用带有 ansible-pull 的动态清单会产生警告,即使指定的主机名包含在动态清单脚本的结果中。解决方法是将主机名添加到 -i 中,如前所述。

于 2019-05-10T15:04:23.173 回答
0

当心:当“myhost”和“myhost.example.com”都在清单中时,ansible-pull 似乎对有多少主机感到困惑,并且可能会运行两次角色或角色(一次在 myhost,再次在 myhost .example.com),或者可能在一个名称上运行一些播放,而在另一个名称上运行其他播放,并行产生意外的执行顺序。

于 2019-09-20T17:22:24.190 回答