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
Related
I have to make an ansible playbook that connects to a brand new cisco router via a telnet connection to a terminal concentrator on a specefic telnet port.
This is my playbook so far
---
- name: Telnet test
hosts: localhost
connection: local
tasks:
- name: telnet test
telnet:
host: 10.145.0.200
port: 4009
send_newline: yes
prompts:
- '[>|#]'
command:
- enable
- term len 0
- show ver
My issue is that when the connection is made the playbook just stay in the task without sending any command to the cisco router, how can i connect to a device without username and password?
i had to cancel the playbook since it will just stay in the task forever
ansible-playbook 2.9.13
config file = /home/niclas/ansible/gc/ansible.cfg
configured module search path = ['/home/niclas/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/niclas/.local/lib/python3.8/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]
Using /home/niclas/ansible/gc/ansible.cfg as config file
host_list declined parsing /home/niclas/ansible/gc/inventory.yml as it did not pass its verify_file() method
Parsed /home/niclas/ansible/gc/inventory.yml inventory source with ini plugin
PLAYBOOK: testTelnet.yml ***********************************************************************************************
1 plays in testTelnet.yml
PLAY [Telnet test] *****************************************************************************************************
META: ran handlers
TASK [telnet test] *****************************************************************************************************
task path: /home/niclas/ansible/gc/testTelnet.yml:6
^C [ERROR]: User interrupted execution
Edit error:
❯ ansible-playbook testTelnet.yml -vvv
ansible-playbook 2.9.13
config file = /home/niclas/ansible/gc/ansible.cfg
configured module search path = ['/home/niclas/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/niclas/.local/lib/python3.8/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]
Using /home/niclas/ansible/gc/ansible.cfg as config file
host_list declined parsing /home/niclas/ansible/gc/inventory.yml as it did not pass its verify_file() method
Parsed /home/niclas/ansible/gc/inventory.yml inventory source with ini plugin
PLAYBOOK: testTelnet.yml ***********************************************************************************************
1 plays in testTelnet.yml
PLAY [Telnet test] *****************************************************************************************************
META: ran handlers
TASK [telnet test] *****************************************************************************************************
task path: /home/niclas/ansible/gc/testTelnet.yml:6
The full traceback is:
Traceback (most recent call last):
File "/home/niclas/.local/lib/python3.8/site-packages/ansible/executor/task_executor.py", line 147, in run
res = self._execute()
File "/home/niclas/.local/lib/python3.8/site-packages/ansible/executor/task_executor.py", line 665, in _execute
result = self._handler.run(task_vars=variables)
File "/home/niclas/.local/lib/python3.8/site-packages/ansible/plugins/action/telnet.py", line 59, in run
tn = telnetlib.Telnet(host, port, timeout)
File "/usr/lib/python3.8/telnetlib.py", line 218, in __init__
self.open(host, port, timeout)
File "/usr/lib/python3.8/telnetlib.py", line 235, in open
self.sock = socket.create_connection((host, port), timeout)
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
fatal: [localhost]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
PLAY RECAP *************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
I think the ansible's command interpreter can't understand the promt from the cisco. I think you should use cisco.ios plugin instead which is described here: https://docs.ansible.com/ansible/latest/collections/cisco/ios/ios_command_module.html
UPDATE
Because the telnet task does not understand cisco.ios commands, then you should use another approach for connecting. Hope your cisco device support SSH, then you can make the connection.
In this case, I think your playbook should look like this:
ansible_connection: ansible.netcommon.network_cli
ansible_network_os: cisco.ios.ios
ansible_user: cisco
ansible_password: ciscopass
ansible_become: yes
ansible_become_method: enable
ansible_become_password: if_you_have_enable_password_define_it_here
---
- hosts: ansible.ini_defined_group_name_for_cisco_device
tasks:
- name: log into cisco device and init this commands
cisco.ios.ios_command:
commands:
- show version
- term len 0
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.
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 have a playbook file containing below simple commands. ansible version 2.8.1 is installed on remote host.
Playbook file:
---
- name: Deploy accounts on AWS staging
gather_facts: False
hosts: Project_stage
roles:
- { role: stageRole }
Task file for role stageRole:
---
- name: Deploying WAR on EB
command: eb --version chdir=/opt/EB_accounts
Upon executing above playbook, i can see below exception with error message as "msg": "[Errno 2] No such file or directory",
Exception:
host_web02 (0, '', '')
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
File "/**tmp/ansible_command_payload_ynqG_D/ansible_command_payload.zip/ansible/module_utils/basic.py**", line 2561, in run_command
cmd = subprocess.Popen(args, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
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