我正在尝试使用Ansible自动化我的工作站。我一直在关注this tutorial作为介绍。但是我一直在使用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-playbook
运行我的剧本时,我没有任何警告:sudo ansible-playbook local.yml
有没有办法摆脱这个警告?
您收到警告,因为ansible-pull
构建了一个命令行,其中包含--limit
构建的like this选项:
node = platform.node()
host = socket.getfqdn()
limit_opts = 'localhost,%s,127.0.0.1' % ','.join(set([host, node, host.split('.')[0], node.split('.')[0]]))
其中platform.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-pull
选项,在ansible清单中明确包含您的本地主机名来避免它。您可以包含内联主机:
ansible-pull -i localhost,myhostname -U ...
或者您可以在现有库存文件中明确指出它:
ansible-pull -i /etc/ansible/hosts -U ...
要添加到@larsks答案,即使指定的主机名包含在动态库存脚本的结果中,似乎使用带有ansible-pull的动态库存也会产生警告。解决方法是将主机名添加到-i,如前所述。