Compare commits

...

4 Commits

Author SHA1 Message Date
9eb5aabba4
Show failed tasks in the final report. 2023-07-26 11:08:21 +02:00
ed06613f8e
Bump version. 2023-03-13 22:48:05 +01:00
David Lascelles
ffda664e9b
Nonzero exit on failure, adding playbook detection
- Returns exit code 1 if playbook has "failed" or "fatal"
- Returns exit code 1 if playbook file is not found
2023-03-10 17:25:57 +01:00
64fec555f7 Enhance visual output: show on which task each playbook is working. 2021-01-22 17:01:59 +01:00
4 changed files with 137 additions and 74 deletions

View File

@ -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

View File

@ -1,12 +1,12 @@
import os
import asyncio
from typing import Tuple
from time import perf_counter
from shutil import get_terminal_size
import subprocess
import argparse
import asyncio
import os
import subprocess
import sys
from collections import defaultdict
from shutil import get_terminal_size
from time import perf_counter
from typing import List, Tuple
def parse_args():
@ -25,14 +25,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)
@ -41,13 +38,17 @@ def prepare_chunk(playbook, chunk: str) -> Tuple[str, str, str]:
if "changed:" in lines[1]:
return ("CHANGED", playbook, chunk)
if "failed:" in lines[1] or "fatal:" in lines[1]:
return ("FAILED", playbook, chunk)
return ("ERROR", 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",
@ -74,7 +75,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 = ["", "", "", "", "", "", "", "", "", ""]
@ -89,13 +96,16 @@ def truncate(string, max_width):
return string[: max_width - 1] + ""
async def show_progression(results):
recaps = {}
async def show_progression(results: asyncio.Queue, playbooks: List[str], stream):
recaps = defaultdict(list)
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 True:
@ -104,52 +114,80 @@ async def show_progression(results):
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 = (
FRAMES[frameno % len(FRAMES)] + " "
f"{len(currently_running)} playbook{'s' if len(currently_running) > 1 else ''} running: "
f"{', '.join(currently_running)}"
)
print(
"\033[0J", # ED (Erase In Display) with parameter 0:
# Erase from the active position to the end of the screen.
truncate(status_line, max_width=columns - 1),
end="\r",
)
recaps[playbook].append(msg)
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":
recaps[playbook].append(msg)
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()
stream.write("\n")
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)
for chunk in recap:
for line in chunk.split("\n"):
if "PLAY RECAP" in line:
continue
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
])
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())

View File

@ -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.7.26"
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

View File

@ -1,27 +0,0 @@
[metadata]
name = ansible-parallel
# Format is YYYY.MM.DD (https://calver.org/)
version = 2021.1.19
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.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
[options]
py_modules = ansible_parallel
python_requires = >= 3.7
[options.entry_points]
console_scripts = ansible-parallel=ansible_parallel:main