Ansible ImportError: cannot import name 'prepare_multipart' Issue - ansible

I'm running the below uri module to find the status of jenkins page after a restart.But unfortunately i'n getting an error as Ansible ImportError: cannot import name 'prepare_multipart' Issue
module:
- name: result-test
uri:
url: "http://{{ jenkins_cm_hostname }}:{{ jenkins_http_port }}{{ jenkins_url_prefix }}"
method: GET
return_content: "yes"
timeout: 5
body_format: raw
follow_redirects: "no"
status_code: 200,403
register: result
output:
"module_stdout": "/etc/bashrc: line 94: TMOUT: readonly variable\r\nTraceback (most recent call last):\r\n File \"/home/ec2-user/.ansible/tmp/ansible-tmp-1628318438.5060062-180720-115929401717244/AnsiballZ_uri.py\", line 100, in <module>\r\n _ansiballz_main()\r\n File \"/home/ec2-user/.ansible/tmp/ansible-tmp-1628318438.5060062-180720-115929401717244/AnsiballZ_uri.py\", line 92, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/ec2-user/.ansible/tmp/ansible-tmp-1628318438.5060062-180720-115929401717244/AnsiballZ_uri.py\", line 41, in invoke_module\r\n run_name='__main__', alter_sys=True)\r\n File \"/usr/lib64/python2.7/runpy.py\", line 176, in run_module\r\n fname, loader, pkg_name)\r\n File \"/usr/lib64/python2.7/runpy.py\", line 82, in _run_module_code\r\n mod_name, mod_fname, mod_loader, pkg_name)\r\n File \"/usr/lib64/python2.7/runpy.py\", line 72, in _run_code\r\n exec code in run_globals\r\n File \"/tmp/ansible_ansible.legacy.uri_payload_bBh_2I/ansible_ansible.legacy.uri_payload.zip/ansible/modules/uri.py\", line 423, in <module>\r\nImportError: cannot import name prepare_multipart\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
Additional details:
Some of the blogs said it is due to multiple ansible installation(via yum,pip..etc).But here i have ansible controller which ansible was installed only using pip and the node where i'm running this module ,there is no ansible installed.Targets are provisioned using terraform.
Could you please guide me how to troubleshot this issue

Related

Ansible community.aws.ecs_taskdefinition fails with `AttributeError: 'str' object has no attribute 'get'`

I have the following Ansible setup with default configs:
ansible 2.10.3 config file = None
configured module search path = ['/Users/kagarlickij/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/Cellar/ansible/2.10.3_1/libexec/lib/python3.9/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.9.0 (default, Nov 30 2020, 15:22:45) [Clang 12.0.0 (clang-1200.0.32.27)]
However I managed to reproduce issue with Python 3.6 and 2.7 in Docker
My Ansible role is the following:
---
- name: Create task definition
ecs_taskdefinition:
family: nginx
containers:
- name: nginx
essential: true
image: "nginx"
environment:
- name: "PORT"
value: "8080"
launch_type: FARGATE
cpu: 512
memory: 1GB
state: present
network_mode: awsvpc
..it's taken from "official" module's example - https://docs.ansible.com/ansible/latest/collections/community/aws/ecs_taskdefinition_module.html#examples
The problem is that execution ansible-playbook --connection=local ./ansible/prowler-fargate.yml
..fails with error: An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'str' object has no attribute 'get'
-vvv output:
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /Users/kagarlickij/.ansible/tmp/ansible-tmp-1607191378.7803059-92712-125949682076654/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/Users/kagarlickij/.ansible/tmp/ansible-tmp-1607191378.7803059-92712-125949682076654/AnsiballZ_ecs_taskdefinition.py", line 102, in <module>
_ansiballz_main()
File "/Users/kagarlickij/.ansible/tmp/ansible-tmp-1607191378.7803059-92712-125949682076654/AnsiballZ_ecs_taskdefinition.py", line 94, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/Users/kagarlickij/.ansible/tmp/ansible-tmp-1607191378.7803059-92712-125949682076654/AnsiballZ_ecs_taskdefinition.py", line 40, in invoke_module
runpy.run_module(mod_name='ansible_collections.community.aws.plugins.modules.ecs_taskdefinition', init_globals=None, run_name='__main__', alter_sys=True)
File "/usr/local/Cellar/python#3.9/3.9.0_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 210, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "/usr/local/Cellar/python#3.9/3.9.0_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/local/Cellar/python#3.9/3.9.0_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/var/folders/bz/pqkg_1bj7tz6q3hr6s1tzkvh0000gn/T/ansible_ecs_taskdefinition_payload_to2ropfx/ansible_ecs_taskdefinition_payload.zip/ansible_collections/community/aws/plugins/modules/ecs_taskdefinition.py", line 517, in <module>
File "/var/folders/bz/pqkg_1bj7tz6q3hr6s1tzkvh0000gn/T/ansible_ecs_taskdefinition_payload_to2ropfx/ansible_ecs_taskdefinition_payload.zip/ansible_collections/community/aws/plugins/modules/ecs_taskdefinition.py", line 353, in main
AttributeError: 'str' object has no attribute 'get'
fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n File \"/Users/kagarlickij/.ansible/tmp/ansible-tmp-1607191378.7803059-92712-125949682076654/AnsiballZ_ecs_taskdefinition.py\", line 102, in <module>\n _ansiballz_main()\n File \"/Users/kagarlickij/.ansible/tmp/ansible-tmp-1607191378.7803059-92712-125949682076654/AnsiballZ_ecs_taskdefinition.py\", line 94, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/Users/kagarlickij/.ansible/tmp/ansible-tmp-1607191378.7803059-92712-125949682076654/AnsiballZ_ecs_taskdefinition.py\", line 40, in invoke_module\n runpy.run_module(mod_name='ansible_collections.community.aws.plugins.modules.ecs_taskdefinition', init_globals=None, run_name='__main__', alter_sys=True)\n File \"/usr/local/Cellar/python#3.9/3.9.0_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py\", line 210, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File \"/usr/local/Cellar/python#3.9/3.9.0_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py\", line 97, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File \"/usr/local/Cellar/python#3.9/3.9.0_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/var/folders/bz/pqkg_1bj7tz6q3hr6s1tzkvh0000gn/T/ansible_ecs_taskdefinition_payload_to2ropfx/ansible_ecs_taskdefinition_payload.zip/ansible_collections/community/aws/plugins/modules/ecs_taskdefinition.py\", line 517, in <module>\n File \"/var/folders/bz/pqkg_1bj7tz6q3hr6s1tzkvh0000gn/T/ansible_ecs_taskdefinition_payload_to2ropfx/ansible_ecs_taskdefinition_payload.zip/ansible_collections/community/aws/plugins/modules/ecs_taskdefinition.py\", line 353, in main\nAttributeError: 'str' object has no attribute 'get'\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
Code is fine - it's fully functional when executed on Jenkins server, just cant make it work on my mac and any Docker container
Nearby module is working fine with the following code:
---
- name: RUN a task on Fargate
ecs_task:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
region: "{{ aws_region }}"
operation: run
cluster: "{{ cluster_name }}"
task_definition: "{{ task_definition_name }}"
count: 1
started_by: ansible_user
launch_type: FARGATE
network_configuration:
subnets:
- "{{ subnet_id }}"
security_groups:
- "{{ security_group_id }}"
register: task_output
Any ideas why that issue is happening and how to get rid of it?

How to use the ansible module ios_l2_interfaces to loop through ios facts?

I want to loop through every interface on a Cisco IOS Switch and set a specific layer 2 configuration if the description matches a particular word (-ap- stands for accesspoint).
This is the playbook:
---
- name: Cisco Layer 2 Interface
hosts: test
strategy: free
vars:
ansible_command_timeout: 60
desc_search: "-ap-"
tasks:
- name: IOS facts
ios_facts:
gather_subset:
- '!all'
- '!min'
gather_network_resources:
- 'interfaces'
- name: Layer 2 config
ios_l2_interfaces:
config:
- name: "{{ item.key }}"
trunk:
allowed_vlans: 10,20,30,40
native_vlan: 10
encapsulation: dot1q
state: replaced
loop: "{{ ansible_net_interfaces|dict2items }}"
when: item.value.description is search(desc_search)
...
Getting the facts is working fine. However the module ios_l2_interfaces failes with the following error. It seems the key of - name: item.key is not working however the error message clearly shows it is there.
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'get'
failed: [demo-sw-02.nb.edeka.net] (item={'key': u'GigabitEthernet1/0/24', 'value': {u'macaddress': u'xxxx.xxxx.xxxx', u'lineprotocol': None, u'description': u'#demo-ap-02', u'duplex': None, u'mediatype': u'10/100/1000BaseTX', u'mtu': 1500, u'operstatus': u'up', u'bandwidth': 1000000, u'ipv4': [], u'type': u'Gigabit Ethernet'}}) => {
"ansible_loop_var": "item",
"changed": false,
"item": {
"key": "GigabitEthernet1/0/24",
"value": {
"bandwidth": 1000000,
"description": "#demo-ap-02",
"duplex": null,
"ipv4": [],
"lineprotocol": null,
"macaddress": "xxxx.xxxx.xxxx",
"mediatype": "10/100/1000BaseTX",
"mtu": 1500,
"operstatus": "up",
"type": "Gigabit Ethernet"
}
},
"rc": 1
}
MSG:
MODULE FAILURE
See stdout/stderr for the exact error
MODULE_STDERR:
Traceback (most recent call last):
File "/home/ansible/.ansible/tmp/ansible-local-7161cuIEBE/ansible-tmp-1579863214.5-251145517474981/AnsiballZ_ios_l2_interfaces.py", line 102, in <module>
_ansiballz_main()
File "/home/ansible/.ansible/tmp/ansible-local-7161cuIEBE/ansible-tmp-1579863214.5-251145517474981/AnsiballZ_ios_l2_interfaces.py", line 94, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/ansible/.ansible/tmp/ansible-local-7161cuIEBE/ansible-tmp-1579863214.5-251145517474981/AnsiballZ_ios_l2_interfaces.py", line 40, in invoke_module
runpy.run_module(mod_name='ansible.modules.network.ios.ios_l2_interfaces', init_globals=None, run_name='__main__', alter_sys=True)
File "/usr/lib64/python2.7/runpy.py", line 188, in run_module
fname, loader, pkg_name)
File "/usr/lib64/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/modules/network/ios/ios_l2_interfaces.py", line 364, in <module>
File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/modules/network/ios/ios_l2_interfaces.py", line 359, in main
File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/config/l2_interfaces/l2_interfaces.py", line 63, in execute_module
File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/config/l2_interfaces/l2_interfaces.py", line 88, in set_config
File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/config/l2_interfaces/l2_interfaces.py", line 113, in set_state
File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/config/l2_interfaces/l2_interfaces.py", line 134, in _state_replaced
File "/tmp/ansible_ios_l2_interfaces_payload_CAXtV1/ansible_ios_l2_interfaces_payload.zip/ansible/module_utils/network/ios/utils/utils.py", line 75, in filter_dict_having_none_value
AttributeError: 'NoneType' object has no attribute 'get'
I am using ansible 2.9.4.
"when" condition worked properly. "filter_dict_having_none_value" suggest problem with dict2items filter.

How to fix the error 'module' object has no attribute 'UnexpectedResponse' in Ansible playbook

Here is the playbook for your reference:
---
- name: Closing notes
hosts: XXXX
gather_facts: yes
tasks:
- name: Modify a change request
snow_record:
state: present
table: change_request
username: admin
password: XXXXX
instance: XXXXX
number: XXXXX
data:
state: -3
register: incident
- debug:
var: incident.record.state
Output:
fatal: [XXXXXXX]: FAILED! => {
"changed": false,
"module_stderr": "Shared connection to XXXXXXX closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n File \"/home/ansible/.ansible/tmp/ansible-tmp-1577091421.74-160409156883610/AnsiballZ_snow_record.py\", line 114, in \r\n _ansiballz_main()\r\n File \"/home/ansible/.ansible/tmp/ansible-tmp-1577091421.74-160409156883610/AnsiballZ_snow_record.py\", line 106, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/ansible/.ansible/tmp/ansible-tmp-1577091421.74-160409156883610/AnsiballZ_snow_record.py\", line 49, in invoke_module\r\n imp.load_module('main', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_snow_record_payload_qHs01F/main.py\", line 334, in \r\n File \"/tmp/ansible_snow_record_payload_qHs01F/main.py\", line 330, in main\r\n File \"/tmp/ansible_snow_record_payload_qHs01F/main.py\", line 319, in run_module\r\nAttributeError: 'module' object has no attribute 'UnexpectedResponse'\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
需要指定python的环境为 python3,就可以解决
You need to make the Python environment as Python 3, you can solve this problem
eg
[all:vars]
ansible_python_interpreter=/usr/bin/python3

Uploading File to Nexus 9000 with Ansible Playbook

Trying to automate file uploading process to the Cisco Nexus 9000 device with Ansible Playbook.
- name: Upload File to Nexus Device
gather_facts: no
hosts: localhost
connection: local
tasks:
- name: Uploading file
nxos_file_copy:
provider:
username: admin
ssh_keyfile: "files/id_rsa"
host: 192.168.0.100
validate_certs: no
transport: cli
local_file: "config.txt"
remote_file: "config.txt"
When running the playbook I am getting following error
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py", line 255, in <module>
main()
File "/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py", line 245, in main
transfer_file(module, dest)
File "/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py", line 174, in transfer_file
port=port)
File "/home/user/venv/ansible-cox/local/lib/python2.7/site-packages/paramiko/client.py", line 424, in connect
passphrase,
File "/home/user/venv/ansible-cox/local/lib/python2.7/site-packages/paramiko/client.py", line 714, in _auth
raise saved_exception
paramiko.ssh_exception.PasswordRequiredException: Private key file is encrypted
fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py\", line 255, in <module>\n main()\n File \"/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py\", line 245, in main\n transfer_file(module, dest)\n File \"/tmp/ansible_4UEm25/ansible_module_nxos_file_copy.py\", line 174, in transfer_file\n port=port)\n File \"/home/user/venv/ansible-cox/local/lib/python2.7/site-packages/paramiko/client.py\", line 424, in connect\n passphrase,\n File \"/home/user/venv/ansible-cox/local/lib/python2.7/site-packages/paramiko/client.py\", line 714, in _auth\n raise saved_exception\nparamiko.ssh_exception.PasswordRequiredException: Private key file is encrypted\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
When SSH-ing from local terminal with the same key it works
(ansible) ~/B/c/Ansible on master ⨯ ssh admin#192.168.0.100 -i files/id_rsa
User Access Verification
NX9K#
I am using Ansible 2.5.4 version.
Any help is greatly appreciated!

Ansible bigip_command module

Is there a way you can ignore an error "wait_for" throws when a conditional statement hasn't been satisfied?
In my play, I have a task to see which LTM in the pair is active, and it fails when it hits the standby (which makes sense).
But it would be nice if you could ignore this error.
Traceback (most recent call last):
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 691, in <module>
main()
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 680, in main
results = mm.exec_module()
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 617, in exec_module
result = manager.exec_module()
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 409, in exec_module
changed = self.execute()
File "/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py", line 498, in execute
raise FailedConditionsError(errmsg, failed_conditions)
ansible.module_utils.network.common.parsing.FailedConditionsError: One or more conditional statements have not been satisfied.
fatal: [x.x.x.x -> localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 691, in <module>\n main()\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 680, in main\n results = mm.exec_module()\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 617, in exec_module\n result = manager.exec_module()\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 409, in exec_module\n changed = self.execute()\n File \"/tmp/ansible_yIW5Ex/ansible_module_bigip_command.py\", line 498, in execute\n raise FailedConditionsError(errmsg, failed_conditions)\nansible.module_utils.network.common.parsing.FailedConditionsError: One or more conditional statements have not been satisfied.\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
And here is the task I'm working with: (Note: the host inventory file contains 2 IPs only)
- name : Checking which LTM is active....
bigip_command:
server: "{{ inventory_hostname }}"
user: "{{ remote_username }}"
password: "{{ remote_passwd }}"
commands:
- "tmsh show sys failover"
- "tmsh list /sys management-ip | grep -o x.x.x.x"
wait_for:
- result[0] contains active
validate_certs: no
delegate_to: localhost
Please check the module documentation here -- There is a "match" directive that defaults to "all" which implies that in the "wait_for" section all checks should pan out as true. You can explicitly set this to "any" which would make your run pass if at least 1 condition in "wait_for" is met.
You also have a "retries" and "interval" flag which will tell the module how many retries to make and how far apart they should be, respectively.
I hope this helps.
Cheers!

Resources