Crypt_GPG getting error while doing concurrent request to encrypt & sign data - gnupg

I'm using this script to encrypt and sign data.
`$gpg = new Crypt_GPG(['digest-algo' => "SHA256", 'cipher-algo' => "AES256", 'compress-algo' => "zip", "homedir" => "/home/ubuntu/.gnupg", "debug" => true]);
$gpg->addSignKey($clientKey, $passphase);
$gpg->addEncryptKey($serverKey);
$signedMessage = $gpg->encryptAndSign(json_encode($messageBody));`
I found below error in debug log:
Crypt_GPG DEBUG: GPG error stream ready for reading
Crypt_GPG DEBUG: => about to read 65536 bytes from GPG error
Crypt_GPG DEBUG: => read 80 bytes
Crypt_GPG DEBUG: ERROR: gpg: signing failed: End of file
Crypt_GPG DEBUG: ERROR: gpg: [stdin]: sign+encrypt failed: End of file
Crypt_GPG DEBUG: GPG status stream ready for reading
Crypt_GPG DEBUG: => about to read 65536 bytes from GPG status
Crypt_GPG DEBUG: => read 69 bytes
Crypt_GPG DEBUG: STATUS: BEGIN_ENCRYPTION 2 9
Crypt_GPG DEBUG: STATUS: FAILURE sign-encrypt 33570815
Crypt_GPG DEBUG: => subprocess returned an unexpected exit code: 2
Getting above error while doing concurrent request while encrypting and sign data. e.g. 10 request per second
I'm expecting it should work for atleast 5-10 concurrent request.

Related

Save ansible variable in local file

I am executing a PS script on a windows host and want to store its stdout in a file on an ansible local machine. I have a playbook like following:
---
- name: Check Antivirus software
hosts: all
become: false
gather_facts: no
tasks:
- name: Get AV details
win_shell: |
echo "script printing data on stdout"
register: result
- name: save response
copy:
content: '{{ result.stdout }}'
dest: '{{ response_file }}'
delegate_to: localhost
From the above playbook, 1st task gets executed without any issues. But 2nd task gives the following error.
TASK [save response] *******************************************************************************************************************************************
fatal: [20.15.102.192 -> localhost]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPSConnectionPool(host='localhost', port=5986): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f4940760208>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
I also tried local_action which is also giving the same error.

Neatly print Ansible debug output on multiple lines

I have a file that I want to print as debug msg line.
cat result.txt
## BEGIN :## Role Name: Deployment Checks
## REASON:- ERROR: Deployment Checks output from command FATAL: TEST FAILED.
## END :## Role Name: Deployment Checks ##
## BEGIN :## Role Name: Describe the instance
## REASON:- ERROR: Describe the instance FATAL: TEST FAILED.
## END :## Role Name: Describe the instance ##
I m using the below code to print them on the console
- name: Read result
shell: "cat result.txt | grep 'REASON:-'"
register: result
- name: print checks fail.
debug:
msg:
- "Check Failed!"
- "{{ result.stdout_lines }}"
Which prints below output in an ugly format as single line
TASK [ print checks fail.] **************************************************
fatal: [10.203.116.90]: FAILED! => {"changed": false, "msg": ["Check Failed!", ["REASON:- ERROR: Deployment Checks output from command", "REASON:- ERROR: Describe the instance "]]}
How can I print every reason as a single line? So that output is more readable. Something like below
TASK [ print checks fail.] **************************************************
fatal: [10.203.116.90]: FAILED! => {"changed": false, "msg":
["Check Failed!",
["REASON:- ERROR: Deployment Checks output from command",
"REASON:- ERROR: Describe the instance "]]}
Without a change change of callback, you could do:
- debug:
msg: "{{ ['Check Failed!'] + result.stdout_lines }}"
Which will render in
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": [
"Check Failed!",
"## REASON:- ERROR: Deployment Checks output from command FATAL: TEST FAILED.",
"## REASON:- ERROR: Describe the instance FATAL: TEST FAILED."
]
}
But as pointed by #U880D's answer, the YAML callback is way more convenient to format output properly.
For example, with the YAML output, you could do:
- debug:
msg: |-
Check Failed!
{{ result.stdout }}
Which will render in
TASK [debug] *******************************************************************
ok: [localhost] =>
msg: |-
Check Failed!
## REASON:- ERROR: Deployment Checks output from command FATAL: TEST FAILED.
## REASON:- ERROR: Describe the instance FATAL: TEST FAILED.
You may have a look into Callback plugins and configure it in ansible.cfg in example with stdout_callback: yaml.
By using this
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Read result
shell:
cmd: "grep 'REASON:-' result.txt"
register: result
- name: Show result
debug:
msg: "{{ result.stdout_lines }}"
the result would be
TASK [Show result] ***************************************************************
ok: [localhost] =>
msg:
- '## REASON:- ERROR: Deployment Checks output from command FATAL: TEST FAILED.'
- '## REASON:- ERROR: Describe the instance FATAL: TEST FAILED.'
as required.
According your example it seems that you have currently configured stdout_callback: json, and according Ansible Issue #76556 it can't be specified in the playbook level.
Further Q&A
Ansible stdout formatting
Ansible Documentation
Index of all Callback Plugins

Ansible: get specific attribute and match from gather fact

Sorry to ask basic question in Ansible but need your expert advice to match and print some value. I am capturing value from set_fact(file) and wanted to match with default pool and print the result virtual server if matches. Need your advice.
tasks:
- name: Collect information of all virtual servers
bigip_device_facts:
gather_subset:
- virtual-servers
provider: "{{provider}}"
register: facts_result
- name: Display VIP's that have a specific default pool
debug: "msg={{item.name}}"
when: item.default_pool == "/Common/my-pool1" ========: Here i wanted to add pool loop pool_list
loop: "{{facts_result.ansible_facts.ansible_net_virtual_servers}}"
loop_control:
label:
- "{{item.name}}"
- "{{item.default_pool}}"
but getting error below
fatal: [abc.pqr]: FAILED! =>
msg: '''acts_result'' is undefined'
The conditional check 'item.default_pool == "/Common/my-pool1"' failed. The error was: error while evaluating conditional (item.default_pool == "/Common/my-pool1"): 'item' is undefined
Now catch is some of the VS doesnt have default pool. so this is failing.
output
ansible_facts:
ansible_net_virtual_servers:
auto_lasthop: default
availability_status: available
client_side_bits_in: 0
client_side_bits_out: 0
client_side_current_connections: 0
client_side_evicted_connections: 0
connection_limit: 0
connection_mirror_enabled: 'no'
cpu_usage_ratio_last_1_min: 0
cpu_usage_ratio_last_5_min: 0
cpu_usage_ratio_last_5_sec: 0
current_syn_cache: 0
default_pool: /Common/my-pool1 ====: failing becuse soem of virtual server has no default pool
description: abc
destination: /Common/10.10.10.53:30289
destination_address: 10.10.10.226
destination_port: 30289
enabled: 'yes'
name: apcd_30289

Cisco IOS md5 check ansible module fails

Running ansible 2.6
This error happens when I try to run the command:
verify /md5 flash:/{ios_file}
This is the output of the command:
TASK [IOS - MD5 CHECK - PASS1] **************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.module_utils.connection.ConnectionError: timeout trying to send command: verify /md5 c2960x-universalk9-mz.152-2.E8.bin
fatal: [pdctestisesw7]: FAILED! => changed=false
module_stderr: |-
Traceback (most recent call last):
File "/tmp/ansible_cr7Tgd/ansible_module_ios_command.py", line 247, in <module>
main()
File "/tmp/ansible_cr7Tgd/ansible_module_ios_command.py", line 217, in main
responses = run_commands(module, commands)
File "/tmp/ansible_cr7Tgd/ansible_modlib.zip/ansible/module_utils/network/ios/ios.py", line 148, in run_commands
File "/tmp/ansible_cr7Tgd/ansible_modlib.zip/ansible/module_utils/connection.py", line 174, in __rpc__
ansible.module_utils.connection.ConnectionError: timeout trying to send command: verify /md5 c2960x-universalk9-mz.152-2.E8.bin
module_stdout: ''
msg: MODULE FAILURE
rc: 1
to retry, use: --limit #/export/home/e130885/playbooks/ios-switch-upgrade/upgrade_ios_switch_v1.retry
Here is the task being executed:
- name: IOS - MD5 CHECK - PASS1
ios_command:
commands:
- command: "verify /md5 {{ compliant_ios_file }}"
register: md5_response
vars:
ansible_command_timeout: 3000
when: 'compliant_ios_file in dir_response.stdout[0]'
This only seems to happen on commands that take more than a second to execute.
There are different ways to achieve it, the following works fine for me. The timeout values varies depending on router type.
> - name: VERIFY_IMAGE
> ios_command:
> commands:
> - "verify /md5 flash:c800-universalk9-mz.SPA.154-3.M10.bin"
> wait_for:
> - result[0] contains "a8216179d49e598579e21b7e5abc9046"
> retries: 1
> vars:
> ansible_command_timeout: 120

Ansible error when delete the file (this module requires key=value arguments)

I get the following error when I try to delete a file:
TASK: [mulecore | Delete the anchor.txt file] *********************************
failed: => {"failed": true}
msg: this module requires key=value arguments (['the_file.stdout', 'state-absent'])
FATAL: all hosts have already failed -- aborting
This is my task:
- file: the_file.stdout state = absent
You have to mark file as path parameter:
- file: path=the_file.stdout state=absent

Resources