The reason for this article was a post in @pro_ansible's chat :
Vladislav? Shishkov, [17.02.21 20:59] Gentlemen, there are two questions regarding a custom long operation, for example, a backup: 1. Is it possible to tighten the progress bar of a custom bash through the ansible? (if through a plugin, then kick into some example or documentation pliz) 2. It seems like you want to write a plugin for this bash, but the question arises, how to be and how to solve the moments of execution that are idempotent?
A quick search in the backyard of memory did not suggest anything suitable. Nevertheless, I definitely remembered that the Ansible code is easy to read, and the "tinkers" support extension by both plugins and regular Python modules. And if so, then nothing prevents to once again push the boundaries of the possible. Hold my beer! ...
It is clear that standard Ansible already knows how to do both steps, only the resulting "exhaust" is collected into a single whole and transmitted to the control host after the end of the process, and we want to do this in real time. Therefore, you can at least look at the existing implementation, and as a maximum - somehow reuse the existing code.
The original question can be boiled down to two simple steps:
Capture stdout of a command on the target host
Send it to the management host.
We transfer data to the control host
I suggest starting from the end: with the organization of an additional transmission channel to the control host. The solution to this question looks quite obvious: remember that Ansible runs on top of ssh, and use the port forwarding function:
Python code
# - :
# https://github.com/ansible/ansible/blob/5078a0baa26e0eb715e86c93ec32af6bc4022e45/lib/ansible/plugins/connection/ssh.py#L662
self._add_args(
b_command,
(b"-R", b"127.0.0.1:33333:" + to_bytes(self._play_context.remote_addr, errors='surrogate_or_strict', nonstring='simplerepr') + b":33335"),
u"ANSIBLE_STREAMING/streaming set"
)
? ssh- 33333 127.0.0.1, - 33335.
netcat
( , ?): nc -lk 33335
.
, , Ansible , , : , nc 127.0.0.1 33333
, - .
stdout
- . stdout - - Ansible «shell». , - , , command. , , , . « », .
« » :
# basic.py, import'
import socket
# run_command - - :
# https://github.com/ansible/ansible/blob/5078a0baa26e0eb715e86c93ec32af6bc4022e45/lib/ansible/module_utils/basic.py#L2447
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
clientSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
clientSocket.connect(("127.0.0.1",33333));
# run_command - - :
# https://github.com/ansible/ansible/blob/5078a0baa26e0eb715e86c93ec32af6bc4022e45/lib/ansible/module_utils/basic.py#L2455
clientSocket.send(b_chunk);
# run_command - -
# https://github.com/ansible/ansible/blob/5078a0baa26e0eb715e86c93ec32af6bc4022e45/lib/ansible/module_utils/basic.py#L2481
clientSocket.close()
? , Ansible. : connection plugin, Ansible. chemtech «-2019» ( , Python- ), :-)
Ansible?
- , , Ansible. , .
, , .