Compare commits
10 Commits
482e3ac5e6
...
ed06613f8e
Author | SHA1 | Date | |
---|---|---|---|
ed06613f8e | |||
|
ffda664e9b | ||
64fec555f7 | |||
e18c42fad0 | |||
0a763d075e | |||
f3527b905d | |||
ad27b7ce70 | |||
2a27070928 | |||
7ff01ceae7 | |||
3c7f090e7b |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -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
|
## Example
|
||||||
|
|
||||||
|
It's easy to start:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ ansible-parallel *.yml
|
$ 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
|
# Playbook playbook-webs.yml, ran in 123s
|
||||||
|
|
||||||
web1.meltygroup.com : ok=51 changed=0 unreachable=0 failed=0 skipped=12 rescued=0 ignored=0
|
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 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():
|
def parse_args():
|
||||||
|
@ -24,14 +24,11 @@ def prepare_chunk(playbook, chunk: str) -> Tuple[str, str, str]:
|
||||||
|
|
||||||
return a tree-tuple:
|
return a tree-tuple:
|
||||||
- Chunk type:
|
- 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
|
- playbook name
|
||||||
- the actual chunk.
|
- the actual chunk.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
lines = chunk.split("\n")
|
lines = chunk.strip().split("\n")
|
||||||
if len(lines) >= 2:
|
if len(lines) >= 2:
|
||||||
if "PLAY RECAP" in chunk:
|
if "PLAY RECAP" in chunk:
|
||||||
return ("RECAP", playbook, chunk)
|
return ("RECAP", playbook, chunk)
|
||||||
|
@ -43,10 +40,14 @@ def prepare_chunk(playbook, chunk: str) -> Tuple[str, str, str]:
|
||||||
return ("FAILED", playbook, chunk)
|
return ("FAILED", playbook, chunk)
|
||||||
if "unreachable:" in lines[1]:
|
if "unreachable:" in lines[1]:
|
||||||
return ("UNREACHABLE", playbook, chunk)
|
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, ""))
|
await results.put(("START", playbook, ""))
|
||||||
process = await asyncio.create_subprocess_exec(
|
process = await asyncio.create_subprocess_exec(
|
||||||
"ansible-playbook",
|
"ansible-playbook",
|
||||||
|
@ -58,7 +59,10 @@ async def run_playbook(playbook, args, results):
|
||||||
env={**os.environ, "ANSIBLE_FORCE_COLOR": "1"},
|
env={**os.environ, "ANSIBLE_FORCE_COLOR": "1"},
|
||||||
)
|
)
|
||||||
task = []
|
task = []
|
||||||
while line := (await process.stdout.readline()).decode():
|
while True:
|
||||||
|
line = (await process.stdout.readline()).decode()
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
if line == "\n":
|
if line == "\n":
|
||||||
chunk = "".join(task) + line
|
chunk = "".join(task) + line
|
||||||
await results.put(prepare_chunk(playbook, chunk))
|
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 results.put(prepare_chunk(playbook, chunk))
|
||||||
|
|
||||||
await process.wait()
|
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 = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
|
FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
|
||||||
|
@ -79,67 +89,107 @@ DISABLE_CURSOR = "\033[?25l"
|
||||||
ENABLE_CURSOR = "\033[?25h"
|
ENABLE_CURSOR = "\033[?25h"
|
||||||
|
|
||||||
|
|
||||||
def truncate(string, max_width=120):
|
def truncate(string, max_width):
|
||||||
if len(string) < max_width:
|
if len(string) <= max_width:
|
||||||
return string
|
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 = {}
|
recaps = {}
|
||||||
starts = {}
|
starts = {}
|
||||||
ends = {}
|
ends = {}
|
||||||
currently_running = []
|
currently_running = []
|
||||||
frameno = 0
|
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:
|
try:
|
||||||
while result := await results.get():
|
while True:
|
||||||
|
result = await results.get()
|
||||||
|
if not result:
|
||||||
|
break
|
||||||
frameno += 1
|
frameno += 1
|
||||||
msgtype, playbook, msg = result
|
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":
|
if msgtype == "START":
|
||||||
starts[playbook] = perf_counter()
|
starts[playbook] = perf_counter()
|
||||||
currently_running.append(playbook)
|
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":
|
if msgtype == "DONE":
|
||||||
currently_running.remove(playbook)
|
currently_running.remove(playbook)
|
||||||
ends[playbook] = perf_counter()
|
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":
|
if msgtype == "RECAP":
|
||||||
recaps[playbook] = msg
|
recaps[playbook] = msg
|
||||||
if msgtype in ("CHANGED", "FAILED", "UNREACHABLE"):
|
if msgtype == "TASK":
|
||||||
print(msg)
|
stream.write("\033[0K") # EL – Erase In Line with parameter 0.
|
||||||
status_line = (
|
stream.write("\033[m") # Select Graphic Rendition: Attributes off.
|
||||||
f"{len(currently_running)} playbook{'s' if len(currently_running) > 1 else ''} running: "
|
stream.write(
|
||||||
f"{truncate(', '.join(currently_running), max_width=100)}"
|
truncate(msg.split("\n")[0], max_width=columns - longest_name - 4)
|
||||||
)
|
)
|
||||||
print(
|
if (
|
||||||
FRAMES[frameno % len(FRAMES)],
|
msgtype == "ERROR"
|
||||||
f"{status_line:126}",
|
): # Collect lines that start with "ERROR" for the recap
|
||||||
end="\r",
|
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:
|
finally:
|
||||||
print(ENABLE_CURSOR, end="")
|
stream.write(ENABLE_CURSOR)
|
||||||
|
stream.flush()
|
||||||
for playbook, recap in recaps.items():
|
for playbook, recap in recaps.items():
|
||||||
print(
|
stream.write(
|
||||||
f"# Playbook {playbook}, ran in {ends[playbook] - starts[playbook]:.0f}s",
|
f"# Playbook {playbook}, ran in {ends[playbook] - starts[playbook]:.0f}s\n"
|
||||||
end="\n\n",
|
|
||||||
)
|
)
|
||||||
for line in recap.split("\n"):
|
for line in recap.split("\n"):
|
||||||
if "PLAY RECAP" not in line:
|
if "PLAY RECAP" not in line:
|
||||||
print(line)
|
stream.write(line)
|
||||||
|
stream.write("\n")
|
||||||
|
stream.flush()
|
||||||
|
|
||||||
|
|
||||||
async def amain():
|
async def amain():
|
||||||
args, remaining_args = parse_args()
|
args, remaining_args = parse_args()
|
||||||
results = asyncio.Queue()
|
# Verify all playbook files can be found
|
||||||
asyncio.create_task(show_progression(results))
|
for playbook in args.playbook:
|
||||||
await asyncio.gather(
|
if not os.path.isfile(playbook):
|
||||||
*[run_playbook(playbook, remaining_args, results) for playbook in args.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():
|
def main():
|
||||||
asyncio.run(amain())
|
return asyncio.run(amain())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
sys.exit(main())
|
||||||
|
|
|
@ -1,3 +1,39 @@
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["setuptools", "wheel"]
|
requires = ["setuptools", "wheel"]
|
||||||
build-backend = "setuptools.build_meta"
|
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
Block a user