I would like to detect each separate tasks of an Ansible code to be able to make a process on the task that depends on its certain components. What I have tried so far to detect Ansible task is to perform regex
match.
So, is there any supported way to get Ansible tasks with some libraries etc? If regex search would be a recommended method, then what would be suggested regex
to match Ansible tasks?
There's built-in list-tasks
switch for ansible-playbook
:
$ ansible-playbook --list-tasks test.yml
playbook: test.yml
play #1 (test): test TAGS: []
tasks:
command TAGS: []
debug TAGS: []
If you need something more complex, use Ansible API to parse playbook and extract required data. You can start with ansible-playbook example.
Simply parsing the playbook as YAML gets list of tasks perfectly (thanks to comments).
import yaml
with open("sample_playbook.yml", "rb") as f:
file_as_task_list = yaml.load(f)
for task in file_as_task_list:
# process the task
As each -
(dash) sign means to be another task in Ansible, it also makes the file list of dictionary items (tasks) when parsing as YAML.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.