Compare commits
10 Commits
482e3ac5e6
...
ed06613f8e
Author | SHA1 | Date |
---|---|---|
Julien Palard | ed06613f8e | |
David Lascelles | ffda664e9b | |
Julien Palard | 64fec555f7 | |
Julien Palard | e18c42fad0 | |
Julien Palard | 0a763d075e | |
Julien Palard | f3527b905d | |
Julien Palard | ad27b7ce70 | |
Julien Palard | 2a27070928 | |
Julien Palard | 7ff01ceae7 | |
Julien Palard | 3c7f090e7b |
|
@ -0,0 +1,3 @@
|
|||
.venv/
|
||||
dist/
|
||||
build/
|
16
README.md
16
README.md
|
@ -23,8 +23,24 @@ feel free to run `ansible-parallel --check *.yml` for example.
|
|||
|
||||
## Example
|
||||
|
||||
It's easy to start:
|
||||
|
||||
```bash
|
||||
$ ansible-parallel *.yml
|
||||
```
|
||||
|
||||
When it runs, it display a live update of what's going on, one line per playbook:
|
||||
|
||||
```
|
||||
web.yml: TASK [common : Configure Debian repositories] *****************************
|
||||
gitlab.yml: TASK [common : Configure IP failover] *************************************
|
||||
staging.yml: TASK [common : Configure Debian repositories] *****************************
|
||||
dev.yml: Done.
|
||||
```
|
||||
|
||||
And when it's done, it prints a full report like:
|
||||
|
||||
```
|
||||
# Playbook playbook-webs.yml, ran in 123s
|
||||
|
||||
web1.meltygroup.com : ok=51 changed=0 unreachable=0 failed=0 skipped=12 rescued=0 ignored=0
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import os
|
||||
import asyncio
|
||||
from typing import Tuple
|
||||
from time import perf_counter
|
||||
import subprocess
|
||||
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from shutil import get_terminal_size
|
||||
from time import perf_counter
|
||||
from typing import List, Tuple
|
||||
|
||||
|
||||
def parse_args():
|
||||
|
@ -24,14 +24,11 @@ def prepare_chunk(playbook, chunk: str) -> Tuple[str, str, str]:
|
|||
|
||||
return a tree-tuple:
|
||||
- Chunk type:
|
||||
- "OK", "CHANGED", "FAILED", "UNREACHABLE": Ansible task status.
|
||||
- "TASK": Unknown task type, yet probably a task.
|
||||
- "RECAP": The big "PLAY RECAP" section at the end of a run.
|
||||
- playbook name
|
||||
- the actual chunk.
|
||||
|
||||
"""
|
||||
lines = chunk.split("\n")
|
||||
lines = chunk.strip().split("\n")
|
||||
if len(lines) >= 2:
|
||||
if "PLAY RECAP" in chunk:
|
||||
return ("RECAP", playbook, chunk)
|
||||
|
@ -43,10 +40,14 @@ def prepare_chunk(playbook, chunk: str) -> Tuple[str, str, str]:
|
|||
return ("FAILED", playbook, chunk)
|
||||
if "unreachable:" in lines[1]:
|
||||
return ("UNREACHABLE", playbook, chunk)
|
||||
return ("TASK", playbook, chunk)
|
||||
if chunk.startswith("TASK"):
|
||||
return ("TASK", playbook, chunk)
|
||||
if "ERROR!" in chunk:
|
||||
return ("ERROR", playbook, chunk)
|
||||
return ("MSG", playbook, chunk)
|
||||
|
||||
|
||||
async def run_playbook(playbook, args, results):
|
||||
async def run_playbook(playbook, args, results: asyncio.Queue):
|
||||
await results.put(("START", playbook, ""))
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
"ansible-playbook",
|
||||
|
@ -58,7 +59,10 @@ async def run_playbook(playbook, args, results):
|
|||
env={**os.environ, "ANSIBLE_FORCE_COLOR": "1"},
|
||||
)
|
||||
task = []
|
||||
while line := (await process.stdout.readline()).decode():
|
||||
while True:
|
||||
line = (await process.stdout.readline()).decode()
|
||||
if not line:
|
||||
break
|
||||
if line == "\n":
|
||||
chunk = "".join(task) + line
|
||||
await results.put(prepare_chunk(playbook, chunk))
|
||||
|
@ -70,7 +74,13 @@ async def run_playbook(playbook, args, results):
|
|||
await results.put(prepare_chunk(playbook, chunk))
|
||||
|
||||
await process.wait()
|
||||
await results.put(("DONE", playbook, ""))
|
||||
if process.returncode:
|
||||
await results.put(
|
||||
("DONE", playbook, f"Exited with error code: {process.returncode}")
|
||||
)
|
||||
else:
|
||||
await results.put(("DONE", playbook, "Done."))
|
||||
return process.returncode
|
||||
|
||||
|
||||
FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
|
||||
|
@ -79,67 +89,107 @@ DISABLE_CURSOR = "\033[?25l"
|
|||
ENABLE_CURSOR = "\033[?25h"
|
||||
|
||||
|
||||
def truncate(string, max_width=120):
|
||||
if len(string) < max_width:
|
||||
def truncate(string, max_width):
|
||||
if len(string) <= max_width:
|
||||
return string
|
||||
return string[:max_width] + "…"
|
||||
return string[: max_width - 1] + "…"
|
||||
|
||||
|
||||
async def show_progression(results):
|
||||
async def show_progression(results: asyncio.Queue, playbooks: List[str], stream):
|
||||
recaps = {}
|
||||
starts = {}
|
||||
ends = {}
|
||||
currently_running = []
|
||||
frameno = 0
|
||||
print(DISABLE_CURSOR, end="")
|
||||
stream.write(DISABLE_CURSOR)
|
||||
longest_name = max(len(playbook) for playbook in playbooks)
|
||||
for playbook in playbooks:
|
||||
stream.write(playbook + ": \n")
|
||||
columns, _ = get_terminal_size()
|
||||
try:
|
||||
while result := await results.get():
|
||||
while True:
|
||||
result = await results.get()
|
||||
if not result:
|
||||
break
|
||||
frameno += 1
|
||||
msgtype, playbook, msg = result
|
||||
position = playbooks.index(playbook)
|
||||
diff = len(playbooks) - position
|
||||
stream.write(f"\033[{diff}A")
|
||||
stream.write(
|
||||
f"\033[{longest_name + 2}C"
|
||||
) # Move right after the playbook name and :.
|
||||
if msgtype == "START":
|
||||
starts[playbook] = perf_counter()
|
||||
currently_running.append(playbook)
|
||||
stream.write("\033[0K") # EL – Erase In Line with parameter 0.
|
||||
stream.write("\033[m") # Select Graphic Rendition: Attributes off.
|
||||
stream.write("Started")
|
||||
if msgtype == "DONE":
|
||||
currently_running.remove(playbook)
|
||||
ends[playbook] = perf_counter()
|
||||
stream.write("\033[0K") # EL – Erase In Line with parameter 0.
|
||||
stream.write("\033[m") # Select Graphic Rendition: Attributes off.
|
||||
stream.write(msg)
|
||||
if msgtype == "RECAP":
|
||||
recaps[playbook] = msg
|
||||
if msgtype in ("CHANGED", "FAILED", "UNREACHABLE"):
|
||||
print(msg)
|
||||
status_line = (
|
||||
f"{len(currently_running)} playbook{'s' if len(currently_running) > 1 else ''} running: "
|
||||
f"{truncate(', '.join(currently_running), max_width=100)}"
|
||||
)
|
||||
print(
|
||||
FRAMES[frameno % len(FRAMES)],
|
||||
f"{status_line:126}",
|
||||
end="\r",
|
||||
)
|
||||
if msgtype == "TASK":
|
||||
stream.write("\033[0K") # EL – Erase In Line with parameter 0.
|
||||
stream.write("\033[m") # Select Graphic Rendition: Attributes off.
|
||||
stream.write(
|
||||
truncate(msg.split("\n")[0], max_width=columns - longest_name - 4)
|
||||
)
|
||||
if (
|
||||
msgtype == "ERROR"
|
||||
): # Collect lines that start with "ERROR" for the recap
|
||||
recaps[playbook] = (
|
||||
"\n".join([line for line in msg.split("\n") if "ERROR" in line])
|
||||
+ "\n"
|
||||
)
|
||||
stream.write(f"\033[{diff}B")
|
||||
stream.write(f"\033[{columns + 1}D")
|
||||
stream.flush()
|
||||
finally:
|
||||
print(ENABLE_CURSOR, end="")
|
||||
stream.write(ENABLE_CURSOR)
|
||||
stream.flush()
|
||||
for playbook, recap in recaps.items():
|
||||
print(
|
||||
f"# Playbook {playbook}, ran in {ends[playbook] - starts[playbook]:.0f}s",
|
||||
end="\n\n",
|
||||
stream.write(
|
||||
f"# Playbook {playbook}, ran in {ends[playbook] - starts[playbook]:.0f}s\n"
|
||||
)
|
||||
for line in recap.split("\n"):
|
||||
if "PLAY RECAP" not in line:
|
||||
print(line)
|
||||
stream.write(line)
|
||||
stream.write("\n")
|
||||
stream.flush()
|
||||
|
||||
|
||||
async def amain():
|
||||
args, remaining_args = parse_args()
|
||||
results = asyncio.Queue()
|
||||
asyncio.create_task(show_progression(results))
|
||||
await asyncio.gather(
|
||||
*[run_playbook(playbook, remaining_args, results) for playbook in args.playbook]
|
||||
# Verify all playbook files can be found
|
||||
for playbook in args.playbook:
|
||||
if not os.path.isfile(playbook):
|
||||
print("Could not find playbook:", playbook)
|
||||
return 1
|
||||
|
||||
results_queue = asyncio.Queue()
|
||||
printer_task = asyncio.create_task(
|
||||
show_progression(results_queue, args.playbook, sys.stderr)
|
||||
)
|
||||
await results.put(None)
|
||||
results = await asyncio.gather(
|
||||
*[
|
||||
run_playbook(playbook, remaining_args, results_queue)
|
||||
for playbook in args.playbook
|
||||
],
|
||||
return_exceptions=True,
|
||||
)
|
||||
await results_queue.put(None)
|
||||
await printer_task
|
||||
return sum(results)
|
||||
|
||||
|
||||
def main():
|
||||
asyncio.run(amain())
|
||||
return asyncio.run(amain())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
sys.exit(main())
|
||||
|
|
|
@ -1,3 +1,39 @@
|
|||
[build-system]
|
||||
requires = ["setuptools", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "ansible-parallel"
|
||||
# Format is YYYY.MM.DD (https://calver.org/)
|
||||
version = "2023.3.13"
|
||||
description = "Run ansible playbooks in parallel."
|
||||
authors = [
|
||||
{ name = "Julien Palard", email = "julien@palard.fr" },
|
||||
]
|
||||
classifiers = [
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Intended Audience :: System Administrators",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Natural Language :: English",
|
||||
"Programming Language :: Python :: 3",
|
||||
]
|
||||
requires-python = ">= 3.7"
|
||||
|
||||
[project.readme]
|
||||
file = "README.md"
|
||||
content-type = "text/markdown; charset=UTF-8"
|
||||
|
||||
[project.license]
|
||||
text = "MIT License"
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://git.afpy.org/mdk/ansible-parallel"
|
||||
|
||||
[project.scripts]
|
||||
ansible-parallel = "ansible_parallel:main"
|
||||
|
||||
[tool.setuptools]
|
||||
py-modules = [
|
||||
"ansible_parallel",
|
||||
]
|
||||
include-package-data = false
|
||||
|
|
28
setup.cfg
28
setup.cfg
|
@ -1,28 +0,0 @@
|
|||
[metadata]
|
||||
name = ansible-parallel
|
||||
# Format is YYYY.MM.DD (https://calver.org/)
|
||||
version = 2020.10.22
|
||||
description = Run ansible playbooks in parallel.
|
||||
long_description = file: README.md
|
||||
long_description_content_type = text/markdown; charset=UTF-8
|
||||
author = Julien Palard
|
||||
author_email = julien@palard.fr
|
||||
url = https://github.com/meltygroup/ansible-parallel
|
||||
license = MIT License
|
||||
classifiers =
|
||||
Development Status :: 4 - Beta
|
||||
Intended Audience :: System Administrators
|
||||
License :: OSI Approved :: MIT License
|
||||
Natural Language :: English
|
||||
Programming Language :: Python :: 3
|
||||
Programming Language :: Python :: 3.6
|
||||
Programming Language :: Python :: 3.7
|
||||
Programming Language :: Python :: 3.8
|
||||
Programming Language :: Python :: 3.9
|
||||
|
||||
[options]
|
||||
py_modules = ansible_parallel
|
||||
python_requires = >= 3.6
|
||||
|
||||
[options.entry_points]
|
||||
console_scripts = ansible-parallel=ansible_parallel:main
|
Loading…
Reference in New Issue