Reputation: 41
I'm making a playbook that deploys a VM from a template, then collects the new IP Address gotten from DHCP and then automatically updates the new VM. I have gotten so far that I have a variable with the IP Address I want to use for the update play. I supply it as hosts: variablename
Only this isn't working and I get the error: [WARNING]: Could not match supplied host pattern, ignoring: 172.16.0.33.
I searched around and found that I should use a hosts patterns with an alias, but I couldn't get the syntax right and after trying I got this error:
**ERROR! Unexpected Exception, this is probably a bug: unhashable type: 'AnsibleMapping'
the full traceback was:
Traceback (most recent call last):
File "/usr/bin/ansible-playbook", line 123, in <module>
exit_code = cli.run()
File "/usr/lib/python3/dist-packages/ansible/cli/playbook.py", line 127, in run
results = pbex.run()
File "/usr/lib/python3/dist-packages/ansible/executor/playbook_executor.py", line 116, in run
all_vars = self._variable_manager.get_vars(play=play)
File "/usr/lib/python3/dist-packages/ansible/vars/manager.py", line 171, in get_vars
magic_variables = self._get_magic_variables(
File "/usr/lib/python3/dist-packages/ansible/vars/manager.py", line 482, in _get_magic_variables
_hosts_all = [h.name for h in self._inventory.get_hosts(pattern=pattern, ignore_restrictions=True)]
File "/usr/lib/python3/dist-packages/ansible/inventory/manager.py", line 374, in get_hosts
if pattern_hash not in self._hosts_patterns_cache:
TypeError: unhashable type: 'AnsibleMapping'**
I followed the same syntax as https://docs.ansible.com/ansible/latest/user_guide/intro_patterns.html [Limitations of patterns] and https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#inventory-aliases
My testcode that got the error above. This is not the whole playbook, but it's irrelevant to the question. The first error is when I just use an IP for hosts:
- name: Update all packages using apt
hosts:
host1:
#This was also tried seperately
#ansible_port: 5555
#ansible_host: 172.16.0.33
host: 172.16.0.33
become: yes
remote_user: user01
tasks:
- name: Ansible Update Cache and Upgrade all Packages
register: updatesys
apt:
name: "*"
state: latest
update_cache: yes
Is this not how you do it? Can someone elaborate? How can I just use an IP to connect to my hosts and execute commands without making an inventory file? I can't do this due to the nature of the playbook. Everytime it makes new VM's with new names and IP. I can't manually add / delete them.
Upvotes: 1
Views: 3534
Reputation: 363
Actually, you can create temporary inventory files on the go with the add_host module. Below you can see an implementation I've used while I was working with an API to create virtual machines and add them to the inventory to operate on them in the same playbook.
- name: Create vm
hosts: localhost
tasks:
- name: An API call to get information about the vm you just created
uri:
url: API endpoint
method: GET
register: mongo_server
- name: Add the host to the inventory
add_host:
args: "ssh-common-args='-o StrictHostKeyChecking=no'"
name: "{{mongo_server.name}}"
groups: "A"
ansible_ssh_user: "{{mongo_server.user}}"
ansible_ssh_pass: "{{mongo_server.pass}}"
public_ip_addr: "{{mongo_server.ip_addr}}"
- name: Connect A and use B's information in it
hosts: A
tasks:
- name: Example task
debug:
msg: "randomtext"
Upvotes: 6