inventory not being able to read when using ansible 2 api

158 views
Skip to first unread message

Oğuz Yarımtepe

unread,
Oct 24, 2016, 9:31:56 AM10/24/16
to Ansible Project
Here is my structure.

✗ tree -L 2
.
├── ansible.cfg
├── ansible.cfg.bak
├── group_vars
│   ├── default
│   └── default.bak
├── hosts
├── playbook.yml
├── playbook_hostname.yml
├── playbook_ip.yml
├── roles
│   ├── hostname
│   ├── ip
│   └── satellite


When i run the below code, i am not able to connect to the remote machine. When i check with tcpdump, no connection request is sent, it seems somehow no connection details are read from inventory files. 

This is the error i saw when i install the ansible 2.0.0.1

 raise AnsibleError("no host vars as host is not in inventory: %s" % hostname)
ansible.errors.AnsibleError: ERROR! no host vars as host is not in inventory: 10.35.75.247



def run(self):
Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts',
'syntax', 'connection', 'module_path', 'forks',
'remote_user', 'private_key_file', 'ssh_common_args',
'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become',
'become_method', 'become_user', 'verbosity', 'check'])

variable_manager = VariableManager()
loader = DataLoader()

options = Options(listtags='', listtasks='', listhosts='', syntax='',
connection='paramiko', module_path='', forks=5, remote_user='root',
private_key_file='', ssh_common_args='', ssh_extra_args='',
sftp_extra_args='', scp_extra_args='', become=True, become_method='sudo',
become_user='root', verbosity=1, check='')

passwords = dict(conn_pass='xxxx', become_pass='xxxx')

# create inventory and pass to var manager
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.hostname_hosts)


variable_manager.extra_vars = {'hosts': self.host, 'hostname': self.hostname,
'conn_name': self.conn_name, 'guest_ip4_address': self.guest_ip4_address,
'guest_ip4_address_for_restart': self.guest_ip4_address_for_restart,
'guest_gw4_address': self.guest_gw4_address, 'template_ip': self.template_ip} # This can accomodate various other command line arguments.`

variable_manager.set_inventory(inventory)


self.logger.info("Running the playbook: changing the hostname and setting ip")

pbex = PlaybookExecutor(playbooks=[self.hostname_playbook_path, self.ip_playbook_path], inventory=inventory,
variable_manager=variable_manager, loader=loader,
options=options, passwords=passwords)

results = pbex.run()



Here is ansible.cfg

[defaults]
inventory = hosts


here is hosts file:

[local]
127.0.0.1 ansible_python_interpreter=python

[default]
10.35.75.247 ansible_python_interpreter=python ansible_user=root ansible_ssh_pass=xxxx

the group_vars/default is empty. 


and the playbook_hostname.yml is here

---
- name: Set the hostname
  hosts: default

  roles:
    - hostname


Any idea now?

somehow i am not using the right method to read inventory and its predefined settings. Will be great if someone help.

Brian Coca

unread,
Oct 24, 2016, 9:37:37 AM10/24/16
to ansible...@googlegroups.com
You don't show how/where you define `self.hostname_hosts`.


----------
Brian Coca

Oğuz Yarımtepe

unread,
Oct 24, 2016, 9:42:16 AM10/24/16
to ansible...@googlegroups.com
Here is the self variables. They are read from a settings.yml and  are set during execution

self.data = data

self.host = data['host']
self.hostname = data['hostname']
self.conn_name = data['conn_name']
if ipam.ip:
self.guest_ip4_address = ipam.restart_ip
else:
self.guest_ip4_address = data['guest_ip4_address']

if ipam.ip:
self.guest_ip4_address_for_restart = ipam.ip
else:
self.guest_ip4_address_for_restart = data['guest_ip4_address_for_restart']

if ipam.gw:
self.guest_gw4_address = ipam.gw
else:
self.guest_gw4_address = data['guest_gw4_address']

self.template_ip = data['template_ip']

self.host = data['host']
self.hostname_hosts = data['hostname_hosts']
self.hostname_playbook_path = data['hostname_playbook']
self.ip_playbook_path = data['ip_playbook']
self.logger = logger

and at the yml file here is the line:

host: 'default'
hostname_hosts: '/full/path/of/the/file/hosts'

So i am setting the hosts file location i sent with my previous email.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/p8OzBaUbjpU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7cTCgv%2BGXOjcGXhewNSQT-B7CPM3%3DYPGL_ryG6yiD7rOw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Oğuz Yarımtepe
http://about.me/oguzy

Oğuz Yarımtepe

unread,
Oct 24, 2016, 12:22:36 PM10/24/16
to ansible...@googlegroups.com
I realized that i haven't set the ansible.cfg file path. My cli app is at another path and although i set the inventory variable there, nothing changed. Still not able to connect to the remote host. It is somehow not able to connect, not able to understand the user and password vars defined at the inventory. 

On Mon, Oct 24, 2016 at 4:37 PM, Brian Coca <bc...@ansible.com> wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/p8OzBaUbjpU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Oğuz Yarımtepe

unread,
Oct 24, 2016, 2:14:57 PM10/24/16
to ansible...@googlegroups.com
It seems that the problem is definetly about reading the inventory. at the /Users/oyarimtepe/Dev/vmware/command-center/cc-cli/venv/lib/python2.7/site-packages/ansible/inventory/__init__.py

def get_host(self, hostname):
if hostname not in self._hosts_cache:
self._hosts_cache[hostname] = self._get_host(hostname)
..

hosts_cache is empty. 
Thats why it is throwing exception. Seems i am doing something wrong about defining an Inventory as host.

Here is my hosts file /Users/oyarimtepe/Dev/vmware/command-center/register/hosts
[local]
127.0.0.1 ansible_python_interpreter=python

[default]
10.35.75.247 ansible_python_interpreter=python ansible_user=root ansible_ssh_pass=xxxx

I set it at 

# create inventory and pass to var manager
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.hostname_hosts)


variable_manager.extra_vars = {'hosts': self.host, 'hostname': self.hostname,
'conn_name': self.conn_name, 'guest_ip4_address': self.guest_ip4_address,
'guest_ip4_address_for_restart': self.guest_ip4_address_for_restart,
'guest_gw4_address': self.guest_gw4_address, 'template_ip': self.template_ip} # This can accomodate various other command line arguments.`

variable_manager.set_inventory(inventory)

Below are the variables:

host: 'default'
hostname: 'somehostname'
suffix: 'xxx'
conn_name: 'ens192'
guest_ip4_address_for_restart: '10.35.101.220'
guest_ip4_address: '10.35.101.220/24'
guest_gw4_address: '10.35.101.1'

self.hostname_hosts is set as /Users/oyarimtepe/Dev/vmware/command-center/register/hosts. Is it true to use a file there?

Also the hosts part at variable_manager.extra_vars is default, is it true?

Any idea what i am doing wrong?

I also set the ansible.cfg path and below is the content
[defaults]
hostfile = /Users/oyarimtepe/Dev/vmware/command-center/register/hosts
inventory = /Users/oyarimtepe/Dev/vmware/command-center/register/hosts

On Mon, Oct 24, 2016 at 4:37 PM, Brian Coca <bc...@ansible.com> wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/p8OzBaUbjpU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages