Skip to content

Commit

Permalink
Initial tag handling added
Browse files Browse the repository at this point in the history
  • Loading branch information
LegenJCdary committed Feb 10, 2022
1 parent f758bec commit e82d82f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ansible_deployer/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def run_task(config: dict, options: dict, inventory: str):
Function implementing actual execution of ansible-playbook
"""
playbooks = get_playbooks(config, options["task"])
tags = get_tags_for_task(config, options)
if len(playbooks) < 1:
logger.error("No playbooks found for requested task %s. Nothing to do.", options['task'])
logger.error("Program will exit now.")
Expand All @@ -395,6 +396,10 @@ def run_task(config: dict, options: dict, inventory: str):
if options["limit"]:
command.append("-l")
command.append(options["limit"])
if tags:
tag_string = ",".join(tags)
command.append("-t")
command.append(tag_string)
logger.debug("Running '%s'.", command)
try:
with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as \
Expand Down Expand Up @@ -499,6 +504,15 @@ def load_global_configuration(cfg_path: str):
print(e, file=sys.stderr)
sys.exit(51)

def get_tags_for_task(config: dict, options: dict):
"""Function to get task's tags"""
tags = []
for task in config["tasks"]["tasks"]:
if task["name"] == options["task"]:
tags = task["tags"]

return tags

def main():
"""ansible-deploy endpoint function"""
global logger, conf
Expand Down
13 changes: 13 additions & 0 deletions etc/hooks/setup_work_dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ cat << END > ./runBinTrue.yaml
shell: "/bin/true"
END

cat << END > ./runBin.yaml
- hosts: all
connection: "local"
tasks:
- name: "Run /bin/true"
shell: "/bin/true"
tags: tag_true
- name: "Run /bin/false"
shell: "/bin/false"
tags: tag_false
END


cat << END > ./test_infra1_inv.yaml
[testHosts]
testHost1
Expand Down
4 changes: 4 additions & 0 deletions etc/schema/tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ tasks:
allow_limit:
type: boolean
required: True
tags:
type: list
required: False

38 changes: 38 additions & 0 deletions etc/tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ setup_hooks:
play_items:
- name: run_bin_true
file: runBinTrue.yaml
- name: run_bin
file: runBin.yaml

tasks:
- name: task_exec_bin_true
Expand All @@ -26,6 +28,7 @@ tasks:
- prod
- locked
allow_limit: True
tags: []

- name: task_empty
play_items: []
Expand All @@ -36,6 +39,7 @@ tasks:
stages:
- testing
allow_limit: True
tags: []

- name: root_only_task
play_items:
Expand All @@ -47,6 +51,7 @@ tasks:
stages:
- testing
allow_limit: True
tags: []

- name: non_root_task
play_items:
Expand All @@ -58,6 +63,7 @@ tasks:
stages:
- testing
allow_limit: True
tags: []

- name: task_with_limit
play_items:
Expand All @@ -72,4 +78,36 @@ tasks:
stages:
- testing
allow_limit: True
tags: []

- name: tagged_task_true
play_items:
- run_bin
allowed_for:
- group: root
infra:
- name: testInfra
stages:
- testing
- name: testInfra2
stages:
- testing
allow_limit: True
tags:
- tag_true

- name: tagged_task_false
play_items:
- run_bin
allowed_for:
- group: root
infra:
- name: testInfra
stages:
- testing
- name: testInfra2
stages:
- testing
allow_limit: True
tags:
- tag_false
1 change: 1 addition & 0 deletions tests/02-checkrun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ check_run_ok() {
# Correct execution.
check_run_ok "ansible-deployer run -t task_exec_bin_true -s prod -i testInfra"
check_run_ok "ansible-deployer run -t task_with_limit -s testing -i testInfra -l testHost1"
check_run_ok "ansible-deployer run -t tagged_task_true -s testing -i testInfra"
# # multiple hosts in limit
check_run_ok "ansible-deployer run -t task_with_limit -s testing -i testInfra2 -l xyzHosts"

Expand Down

0 comments on commit e82d82f

Please sign in to comment.