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!
Related
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
I have been creating different connectors in our confluent installation by using the curl command to PUT a configuration to a url. The set-up is normally done by adding the configuration and curl call in a shell script. We either write separate shell scripts for each environment or pass the server names - broker and connect - as parameters.
I want to write an ansible script to define new connectors which will basically be same script that can be run in all environments and will only depend on the hosts file in each environment. This will help in eliminating user error / typos in server names when running it in different environments.
My current plan is to just convert the shell script and use ansible shell or command module to call 'curl' and pass the parameters. But I am not sure, if that is the correct way, even if it may get the job done.
Does anyone have any suggestion to do it correctly / properly.
Thank you
UPDATE
Based on #Zeitounator's recommendation, I tried to use the uri module. But keep on getting error
---
- name: Create Kafka Connector
hosts: kafka_connect
vars:
connect_url: https://{{ kafka_connect[0] }}:8083
connector_name: CamelTestConnector
cert_path: /tmp/test/
tasks:
- name: prepare connector config
template:
src: camelconnector.json
dest: /tmp/camelconnector.json
delegate_to: localhost
run_once: yes
- name: Create Connector
uri:
url: "https://kafka-connect1-dev:8083/connectors/{{ connector_name }}/config"
client_cert: "{{ cert_path }}certificate.pem"
client_key: "{{ cert_path }}priv.key"
method: PUT
body: "{{ lookup('file', '/tmp/camelconnector.json' ) }}"
body_format: json
validate_certs: no
status_code: [201, 201, 204]
headers:
Content-Type: application/json
# If you're interested in the response
return_content: yes
register: api_result
run_once: yes
- debug:
var=api_result
#curl --cert /software/scripts/clientcerts/certificate.pem --key /software/scripts/clientcerts/priv.key -k -X PUT -H "${HEADER}" --data "${DATA}" https://"${1}":8083/connectors/CamelTestConnector/config
#curl --cert /software/scripts/clientcerts/certificate.pem --key /software/scripts/clientcerts/priv.key -k https://"${1}":8083/connectors/CamelTestConnector/status
This is the error I keep getting
TASK [Create Connector] ***********************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: IOError: [Errno 2] No such file or directory
fatal: [kafka-connect1-dev]: FAILED! => {"changed": false, "content": "", "elapsed": 0, "msg": "Status code was -1 and not [201, 201, 204]: An unknown error occurred: [Errno 2] No such file or directory", "redirected": false, "status": -1, "url": "https://kafka-connect1-dev/connectors/"}
I have verified that the json file is present. I am unable to identify which file it is complaining about. Tried various iterations for connect-url, but all give the same error.
How do I identify which file it is complaining about?
Thanks
UPDATE 2
I ran the script in verbose mode and I get below stack trace. If I comment out the cert and key parts, then I get connection refused, so it seems the cert and key are required. I tried to search for the cause of the error without success. The certificate and key files do exist at the location. What else do I need to check?
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_uri_payload_6FB8tM/ansible_uri_payload.zip/ansible/module_utils/urls.py", line 1494, in fetch_url
unix_socket=unix_socket, ca_path=ca_path)
File "/tmp/ansible_uri_payload_6FB8tM/ansible_uri_payload.zip/ansible/module_utils/urls.py", line 1390, in open_url
unredirected_headers=unredirected_headers)
File "/tmp/ansible_uri_payload_6FB8tM/ansible_uri_payload.zip/ansible/module_utils/urls.py", line 1294, in open
r = urllib_request.urlopen(*urlopen_args)
File "/usr/lib64/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib64/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
File "/usr/lib64/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/tmp/ansible_uri_payload_6FB8tM/ansible_uri_payload.zip/ansible/module_utils/urls.py", line 467, in https_open
return self.do_open(self._build_https_connection, req)
File "/usr/lib64/python2.7/urllib2.py", line 1183, in do_open
h = http_class(host, timeout=req.timeout, **http_conn_args)
File "/tmp/ansible_uri_payload_6FB8tM/ansible_uri_payload.zip/ansible/module_utils/urls.py", line 480, in _build_https_connection
return httplib.HTTPSConnection(host, **kwargs)
File "/usr/lib64/python2.7/httplib.py", line 1259, in __init__
context.load_cert_chain(cert_file, key_file)
IOError: [Errno 2] No such file or directory
Pls. Help
I`m have this playbook:
---
- name: Install service
hosts: some_host
vars:
app: "{{ app_name }}-{{ tag }}.war"
app_folder: "{{ client_dir }}/{{ app_name }}"
tasks:
- name: Unpack archive Java
unarchive:
src={{ java_distrib }}
dest={{ app_folder }}
creates="{{ app_folder }}/{{ java_home }}"
mode=0555
all work fine when i install on remote host.
But when hosts is same machine from i run ansible-playbook i got this error:
TASK [Unpack archive Java] *********************************************************************************************************
fatal: [10.2.9.6]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 10.2.9.6 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File \"/home/stend/.ansible/tmp/ansible-tmp-1586175452.58-42679814338845/AnsiballZ_unarchive.py\", line 114, in <module>\r\n _ansiballz_main()\r\n File \"/home/stend/.ansible/tmp/ansible-tmp-1586175452.58-42679814338845/AnsiballZ_unarchive.py\", line 106, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/stend/.ansible/tmp/ansible-tmp-1586175452.58-42679814338845/AnsiballZ_unarchive.py\", line 49, in invoke_module\r\n imp.load_module('__main__', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_unarchive_payload_f_poEj/__main__.py\", line 157, in <module>\r\n File \"/tmp/ansible_unarchive_payload_f_poEj/ansible_unarchive_payload.zip/ansible/module_utils/urls.py\", line 402, in <module>\r\nAttributeError: 'module' object has no attribute 'HTTPSConnection'\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
and im not understand whats going on. Why connection closed? im try unarchive to local machine. Where i`m wrong? Pls help !
Problem solved, i`m re install python and library libssl1.1
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
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!