What are the correct commands for using the inventory file along with a playbook when using Ansible Collections google.cloud.gcp_compute. You can find an example of the inventory file very similar to what I'm using at the bottom of this article this article
I'm using this update.yml playbook:
- name: Update apt-get repo and cache
apt: update_cache=yes force_apt_get=yes cache_valid_time=3600
This is my inventory-gcp_compute.yml inventory file:
plugin: google.cloud.gcp_compute
zones: # populate inventory with instances in these regions
- us-central1-a
projects:
- vpn-server-sasp
auth_kind: serviceaccount
scopes:
- 'https://www.googleapis.com/auth/cloud-platform'
- 'https://www.googleapis.com/auth/compute.readonly'
keyed_groups:
# Create groups from GCE labels
- prefix: gcp
key: labels
hostnames:
# List host by name instead of the default public ip
- name
compose:
# Set an inventory parameter to use the Public IP address to connect to the host
# For Private ip use "networkInterfaces[0].networkIP"
ansible_host: networkInterfaces[0].accessConfigs[0].natIP
I've tried these commands:
ansible-playbook -i inventory-gcp_compute.yml update.yml
I got this error:
ansible-playbook 2.9.14
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/cheo/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.17 (default, Jul 20 2020, 15:37:01) [GCC 7.5.0]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml as it did not pass its verify_file() method
virtualbox declined parsing /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml as it did not pass its verify_file() method
[WARNING]: * Failed to parse /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory
File "/usr/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 280, in parse_source
plugin.parse(self._inventory, self._loader, source, cache=cache)
File "/usr/lib/python2.7/dist-packages/ansible/plugins/inventory/yaml.py", line 112, in parse
raise AnsibleParserError('Plugin configuration YAML file, not YAML inventory')
[WARNING]: * Failed to parse /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml with constructed plugin: Incorrect plugin name in file: google.cloud.gcp_compute
File "/usr/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 280, in parse_source
plugin.parse(self._inventory, self._loader, source, cache=cache)
File "/usr/lib/python2.7/dist-packages/ansible/plugins/inventory/constructed.py", line 109, in parse
self._read_config_data(path)
File "/usr/lib/python2.7/dist-packages/ansible/plugins/inventory/__init__.py", line 224, in _read_config_data
raise AnsibleParserError("Incorrect plugin name in file: %s" % config.get('plugin', 'none found'))
[WARNING]: Unable to parse /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! 'apt' is not a valid attribute for a Play
The error appears to be in '/home/cheo/sergio/ansible-gce/update.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Update apt-get repo and cache
^ here
Check the docs. It's looking for a file ending in .gcp_compute.yml or .gcp.yml, so while inventory.gcp_compute.yml would qualify, inventory-gcp_compute.yml does not. The errors you're getting says that that file isn't an inventory, but none of the plugin parsers claimed it.
Related
[SOLVED - answer in comments]
I am looking for a help. Maybe someone had this problem already or just have a idea what is going on and can advise? Much appreciated!
In Rundeck I've defined project with Ansible (via ansible plugin) set as a executor.
Within a project I've two resource models:
Ansible Resource Model Source (with Satellite set as dynamic inventory - all works as expected)
resourceyaml (which is not working)
Configuration for both resources is listed at the end of this post.
As said, I am struggling with nodes defined in yaml resource model -
node is visible in Nodes section, but whenever I am trying to execute a command or job against it, error is being throwed:
[WARNING]: Could not match supplied host pattern, ignoring: template
ERROR! Specified hosts and/or --limit does not match any hosts
Failed: AnsibleError: ERROR: Ansible execution returned with non zero code.
Execution failed: 116 in project Linux-Ansible: [Workflow result: , step failures: {1=Dispatch failed on 1 nodes: [template: AnsibleError: ERROR: Ansible execution returned with non zero code. + {dataContext=MultiDataContextImpl(map={}, base=null)} ]}, Node failures: {template=[AnsibleError: ERROR: Ansible execution returned with non zero code. + {dataContext=MultiDataContextImpl(map={}, base=null)} ]}, status: failed]
When I am executing ansible via cli with resourceyaml file set as inventory, I can see these warnings:
ansible -i /etc/ansible/hosts-rundeck-static.yml all --limit node001#domain.com -m shell -a id
[WARNING]: Skipping key (username) in group (template) as it is not a mapping, it is a <class 'ansible.parsing.yaml.objects.AnsibleUnicode'>
[WARNING]: Skipping key (description) in group (template) as it is not a mapping, it is a <class
'ansible.parsing.yaml.objects.AnsibleUnicode'>
[WARNING]: Skipping key (tags) in group (template) as it is not a mapping, it is a <class 'ansible.parsing.yaml.objects.AnsibleUnicode'>
[WARNING]: Skipping key (hostname) in group (template) as it is not a mapping, it is a <class 'ansible.parsing.yaml.objects.AnsibleUnicode'>
[WARNING]: Skipping key (ssh-keypath) in group (template) as it is not a mapping, it is a <class
'ansible.parsing.yaml.objects.AnsibleUnicode'>
[WARNING]: Skipping key (nodename) in group (template) as it is not a mapping, it is a <class 'ansible.parsing.yaml.objects.AnsibleUnicode'>
[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: node001#domain.com
Conf. for resource models:
YAML:
Format: resourceyaml
File Path:/etc/ansible/hosts-rundeck-static.yml
Writeable: yes
Content of hosts-rundeck-static.yml:
---
template:
nodename: node001#domain.com
hostname: 1.2.3.4
username: admin#domain.com
ssh-keypath: keys/ssh_key/key1
description: this is a template node
tags: template
Ansible inventory:
ansible inventory File path:/etc/ansible/foreman.yml
Ansible config file path:/etc/ansible
Gather Facts: yes
Ignore Host Discovery Errors: yes
SSH Authentication: privateKey
SSH User:admin#domain.com
SSH Key File path:/rundeck/id_ecdsa
Privilege escalation method.: sudo
Ansible conf:
[defaults]
strategy_plugins = /opt/mitogen/ansible_mitogen/plugins/strategy
collections_paths = /etc/ansible/collections
inventory = /etc/ansible/foreman.yml
timeout = 30
[inventory]
enable_plugins = host_list, yaml, script, ini, foreman
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
pipelining = True
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
Rundeck version: 3.3.6-20201111, with Ansible plugin version: 3.1.1
Can anyone help me with that? Thank you!
For the record answer: following this guide and defining correctly the ansible.cfg / inventory path on the Node Executor section (with filename at the end) solves this issue.
I cannot get a simple playbook to work against a Cisco Nexus switch using Ansible 2.9.13. I get a vague index out of range error.
Here is my inventory file:
[nxos:vars]
ansible_user=admin
ansible_password=password
ansible_network_os=nxos
ansible_connection=network_cli
ansible_become=yes
ansible_become_method=enable
ansible_become_password=password
[nxos]
nxos-sw01 ansible_host=172.23.180.12
Here is my playbook:
---
- name: "nxos configure nxapi"
hosts: all
gather_facts: no
tasks:
- name: "enble api"
nxos_nxapi:
enable_https: yes
When I execute I get an error about "list index out of range" that does not yield any helpful output:
$ ansible-playbook -i test.inv test.yml -vvvv
ansible-playbook 2.9.0
config file = /home/some.guy/.ansible.cfg
configured module search path = [u'/home/some.guy/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /home/some.guy/.local/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /home/some.guy/.ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /home/some.guy/tmp/ansible/test.inv as it did not pass its verify_file() method
script declined parsing /home/some.guy/tmp/ansible/test.inv as it did not pass its verify_file() method
auto declined parsing /home/some.guy/tmp/ansible/test.inv as it did not pass its verify_file() method
yaml declined parsing /home/some.guy/tmp/ansible/test.inv as it did not pass its verify_file() method
Parsed /home/some.guy/tmp/ansible/test.inv inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /home/some.guy/.local/lib/python2.7/site-packages/ansible/plugins/callback/default.pyc
PLAYBOOK: test.yml **************************************************************************************************************************************************
Positional arguments: test.yml
become_method: sudo
inventory: (u'/home/some.guy/tmp/ansible/test.inv',)
forks: 5
tags: (u'all',)
verbosity: 4
connection: smart
timeout: 10
1 plays in test.yml
PLAY [nxos configure banner] ****************************************************************************************************************************************
META: ran handlers
TASK [enble api] ****************************************************************************************************************************************************
task path: /home/some.guy/tmp/ansible/test.yml:6
<172.23.180.12> attempting to start connection
<172.23.180.12> using connection plugin network_cli
fatal: [nxos-sw01]: FAILED! => {
"msg": "Traceback (most recent call last):\n File \"/usr/bin/ansible-connection\", line 342, in <module>\n main()\n File \"/usr/bin/ansible-connection\", line 261, in main\n task_uuid = sys.argv[2]\nIndexError: list index out of range\n"
}
PLAY RECAP **********************************************************************************************************************************************************
nxos-sw01 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
I have tried different NXOS modules and using the NXAPI connectivity method. I can't seem to get anything to work. I can connect manually (SSH) to the switch from this server.
Can anyone help me to troubleshoot what I am doing wrong?
EDIT: Here is attempt to run just ansible against the device. Something wrong with how connection info is being parsed ? By running the below I am bypassing my playbook entirely and still get same error.
$ ansible -vvvv nxos-sw01 -m ping -i test.inv
ansible 2.9.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/etc/ansible/modules', u'/usr/lib/python2.7/site-packages/napalm_ansible']
ansible python module location = /home/some.guy/.local/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /home/some.guy/tmp/ansible/test.inv as it did not pass its verify_file() method
script declined parsing /home/some.guy/tmp/ansible/test.inv as it did not pass its verify_file() method
auto declined parsing /home/some.guy/tmp/ansible/test.inv as it did not pass its verify_file() method
yaml declined parsing /home/some.guy/tmp/ansible/test.inv as it did not pass its verify_file() method
Parsed /home/some.guy/tmp/ansible/test.inv inventory source with ini plugin
Loading callback plugin minimal of type stdout, v2.0 from /home/some.guy/.local/lib/python2.7/site-packages/ansible/plugins/callback/minimal.pyc
META: ran handlers
<172.23.180.12> attempting to start connection
<172.23.180.12> using connection plugin network_cli
nxos-sw01 | FAILED! => {
"msg": "Traceback (most recent call last):\n File \"/usr/bin/ansible-connection\", line 342, in <module>\n main()\n File \"/usr/bin/ansible-connection\", line 261, in main\n task_uuid = sys.argv[2]\nIndexError: list index out of range\n"
EDIT2: I ended up opening a bug ticket on Github about this issue:
https://github.com/ansible/ansible/issues/72214
After getting help on #ansible-network I was able to figure this out. Apparently I installed additional copies of python2/3 under my home directory and that was being preferred, as shown in the debug output in my OP:
ansible python module location = /home/some.guy/.local/lib/python2.7/site-packages/ansible
After removing that .local path from my home directory I now see ansible --version reporting 2.9.13 and the NXOS module works correctly.
I am trying to configure dynamic inventory in my ansible code with AWS
Here is the configuration in ansible.cfg
[defaults]
inventory = ./inventory/ec2.py
When I try to check to verify the host with ansible -m command -a "ls" all i am getting the following error
[WARNING]: * Failed to parse /Users/Shammon/Projects/ansible/inventory/ec2.py with script plugin: Inventory script
(/Users/Shammon/Projects/ansible/inventory/ec2.py) had an execution error: Traceback (most recent call last): File
"/Users/Shammon/Projects/ansible/inventory/ec2.py", line 135, in <module> from ansible.module_utils import ec2 as ec2_utils
ModuleNotFoundError: No module named 'ansible'
[WARNING]: * Failed to parse /Users/<user-name>/Projects/ansible/inventory/ec2.py with ini plugin:
/Users/<name>/Projects/ansible/inventory/ec2.py:3: Error parsing host definition ''''': No closing quotation
[WARNING]: Unable to parse /Users/<user-name>/Projects/ansible/inventory/ec2.py as an inventory source
Ansible version:
ansible 2.4.3.0
executable location = /Users/<user-name>/Library/Python/2.7/bin/ansible
I am running Ansible Tower v3.4.1 with Ansible v2.7.6 on an ubuntu 16.04 VM running on VirtualBox. I run a playbook that works when I run it from the command line using "ansible-playbook" but fails when I try to run it from Ansible Tower. I know I must have something misconfigured in ansible tower but I can't find it.
I get this warning no matter what changes I make to the inventory (hosts) file.
$ ansible-playbook 2.7.6
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/var/lib/awx/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]
Using /etc/ansible/ansible.cfg as config file
SSH password:
**/tmp/awx_74_z6yJB4/tmpVlXGCX did not meet host_list requirements**, check plugin documentation if this is unexpected
Parsed /tmp/awx_74_z6yJB4/tmpVlXGCX inventory source with script plugin
PLAYBOOK: addpool.yaml *********************************************************
1 plays in addpool.yaml
[WARNING]: **Could not match supplied host pattern, ignoring: bigip**
PLAY [Sample pool playbook] ****************************************************
17:05:43
skipping: no hosts matched
I have enabled inventory plugins for YAML, and made my hosts file into a hosts.yml file.
Here's my hosts file:
192.168.68.253
192.168.68.254
192.168.1.165
[centos]
dad2 ansible_ssh_host=192.168.1.165
[bigip]
bigip1 ansible_host=192.168.68.254
bigip2 ansible_host=192.168.68.253
Here's my playbook:
---
- name: Sample pool playbook
hosts: bigip
connection: local
tasks:
- name: create web servers pool
bigip_pool:
name: web-servers2
lb_method: ratio-member
password: admin
user: admin
server: '{{inventory_hostname}}'
validate_certs: no
I replaced hosts: bigip with hosts: all and specified the inventory in Tower as bigip which contains only the two hosts I want to change. This seems to provide the output I am looking for.
For the ansible-playbook command line, I added --limit bigip and this seems to provide the output I am looking for.
So things appear to be working, I just don't know whether this is best practice use.
If you get the error below while running a playbook with the command
ansible-playbook -i test-project/inventory.txt playbook.yml
{"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 172.31.24.10 port 22: Connection timed out", "unreachable": true}
The solution is to add, in the file ansible.cfg:
[defaults]
inventory=/etc/ansible/hosts
I think you need to remove the connection: local.
You have specified in hosts: bigip that you want these tasks to only run on hosts in the bigip group. You then specify connection: local which causes the task to run on the controller node (i.e. localhost), rather than the nodes in the bigip group. Localhost is not a member of the bigip group, and so none of the tasks in the play will trigger.
Check for special characters in absolute path of hosts file or playbook. Incase if you directly copied the path from putty, try copy and paste it from notepad or any editor
For me the issue was the format of the /etc/ansible/hosts file. You should use the :children suffix in order to use groups of groups like this:
[dev1]
dev_1 ansible_ssh_host=192.168.1.55 ansible_connection=ssh ansible_ssh_user={{username}} ansible_ssh_pass={{password}}
[dev2]
dev_2 ansible_ssh_host=192.168.1.68 ansible_connection=ssh ansible_ssh_user={{username}} ansible_ssh_pass={{password}}
[devs:children]
dev1
dev2
Reference: here
I am trying to run a playbook locally but I want all the vars in the role's task/main.yml file to refer to a group_var in a specific inventory file.
Unfortunately the playbook is unable to access to the group_vars directory as if fail to recognize the vars specified in the role.
The command ran is the following:
/usr/local/bin/ansible-playbook --connection=local /opt/ansible/playbooks/create.yml -i ./inventory-file
but fails to find the group_vars in the /group_vars directory at the same directory level of the inventory file
fatal: [127.0.0.1]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'admin_user_name' is undefined\n\nThe error appears to have been in '/opt/roles/create/tasks/main.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: create org\n ^ here\n"
}
This is my configuration:
ansible-playbook 2.7.5
config file = /etc/ansible/ansible.cfg
configured module search path = ['/opt/ansible-modules']
ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
executable location = /usr/local/bin/ansible-playbook
python version = 3.6.3 (default, Oct 3 2017, 21:45:48) [GCC 7.2.0]
Any help is appreciated.
Thanks,
dom
So, theoretically adding localhost in the inventory would have been a good solution, but in my specific case (and in general for large deployments) was not an option.
I also added --extra-vars "myvar.json" but did not work either.
Turns out (evil detail...) that the right way to add a var file via command line is: --extra-vars "#myvar.json"
Posting it here in hope nobody else struggle days to find this solution.
Cheers,
dom
As per the error, your ansible is not able to read the group_vars, Can you please make sure that your group_vars have the same folder called localhost.
Example Playbook
host is localhost
- hosts: localhost
become: true
roles:
- { role: common, tags: [ 'common' ] }
- { role: docker, tags: [ 'docker' ] }
So in group_vars, it should be localhost and in that folder file in main.yml
Or
You can create a folder called all in group_vars and create a file called all.yml
This should solve the issue