How to resolve warning "Could not match supplied host pattern, ignoring: <machine hostname>?

29.6k Views Asked by At

I'm trying to use Ansible to automate my workstation. I've been following this tutorial as an introduction. But I keep getting a warning using the ansible-pull command.

After doing sudo ansible-pull -U https://github.com/plsergent/setup-user-friendly.git i'm getting [WARNING]: Could not match supplied host pattern, ignoring: <local machine hostname>

Here is my /etc/ansible/hosts file :

[localhost]
127.0.0.1

[localhost:vars]
ansible_connection=local

and my local.yml file :

- 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

Is there a way to get rid of this warning ?

Thanks

Note: I don't have any warning when I run my playbook using ansible-playbook: sudo ansible-playbook local.yml

5

There are 5 best solutions below

1
On BEST ANSWER

Is there a way to get rid of this warning ?

You're getting the warning because ansible-pull builds a command line that includes a --limit option that is built 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]]))

Where platform.node() returns your systems node name (i.e. the output of uname -n) and socket.getfqdn() attempts to return the fully qualified domain name of your system. This means that the ansible command line will look something like this:

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

It so happens that when you provide a hostname in the --limit argument that does not match a host in your inventory, you get the Could not match supplied host pattern error. You can reproduce that like this:

$ 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

If the warning really bothers you, you can avoid it by explicitly including your local hostname in the ansible inventory by adding a -i option to your ansible-pull command line. You can either include the hosts inline:

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

Or you can point it explicitly at an existing inventory file:

ansible-pull -i /etc/ansible/hosts -U ...
0
On

I was able to solve my problem by adding --limit=all to the ansible-pull command. As @larsks indicated in their answer, ansible-pull was adding a limit to the playbook command under-the-hood, but in my case (from a containerized environment), it was adding a limit to a nonexistent host:

[WARNING]: Could not match supplied host pattern, ignoring: e7b0c924b942

By overriding the limit with --limit=all, I was able to use the values in my default inventory.

0
On

To run Ansible on the localhost, just set your /etc/ansible/hosts file like this (no need of IP):

[all]
localhost ansible_connection=local
0
On

Beware: when both "myhost" and "myhost.example.com" are in the inventory, then ansible-pull seems to get confused about how many hosts there are, and may run plays or roles twice (once on myhost, again on myhost.example.com), or may run some plays on one name and other plays on the other name, in parallel, producing an unexpected execution order.

0
On

To add to @larsks answer, it seems that using dynamic inventories with an ansible-pull will produce the warning even if the specified hostname is included in the results of the dynamic inventory script. Workaround is adding the hostname to the -i as previously stated.