Ansible Transformations¶
The Ansible transformation is implemented by two repositories:
- ansible-mps-plugin: The DeMAF plugin service that parses the Ansible deployment model (Step 1: Deployment Model Parser), runs the MPS transformation, merges its result into the EDMM model (Step 3: Model Merger), and requests the analysis of embedded deployment models.
- mps-transformation-ansible: The JetBrains MPS project that defines the Ansible metamodel (language
Ansible) and the generator that implements the Main M2M Transformation (Step 2).
Ansible Metamodel¶
The following diagram shows the Ansible metamodel as supported by the DeMAF:
classDiagram
direction TB
class AnsibleDeploymentModel
class AnsibleComponent {
<<interface>>
+String name
+boolean become
+boolean any_errors_fatal
+String become_exe
+String become_flags
+String become_user
+boolean check_mode
+String collections
+String connection
+boolean debugger
+boolean diff
+boolean ignore_errors
+boolean ignore_unreachable
+boolean no_log
+int port
+String remote_user
+boolean run_once
+String tags
+int throttle
+int timeout
}
class NonPlayComponent {
<<interface>>
+boolean delegate_facts
+boolean delegate_to
+boolean when
}
class Play {
+boolean force_handler
+boolean gather_facts
+boolean gather_timeout
+int max_fail_percentage
+int serial
+String strategy
+String vars_prompt
}
class Role
class Host {
+String hostName
+String group
}
class Task {
+String args
+int async
+boolean changed_when
+int delay
+boolean failed_when
+int poll
+int retries
+boolean until
+String with
+List~String~ loop
}
class Variable {
+String name
+String value
}
class File {
+String path
}
class Module {
<<interface>>
+String name
}
class apt {
+String apt_name
+String pkgs
+String state
}
class docker_image {
+String image_name
+String source
}
class docker_container {
+String container_name
+String image
+String restart_policy
+String memory
+String state
+String network_mode
+String log_driver
}
class docker_network {
+String network_name
+String driver
}
class launchd {
+String launchd_name
+String state
+boolean enabled
}
AnsibleDeploymentModel "1" *-- "0..*" Play : plays
Play ..|> AnsibleComponent
Role ..|> NonPlayComponent
Task ..|> NonPlayComponent
AnsibleComponent "1" *-- "0..*" Variable : vars
AnsibleComponent "1" *-- "0..1" Variable : environment
Play "1" *-- "0..*" Host : hosts
Play "1" *-- "0..*" Task : pre_tasks
Play "1" *-- "0..*" Task : tasks
Play "1" *-- "0..*" Task : post_tasks
Play "1" *-- "0..*" Task : handlers
Play "1" *-- "0..*" Role : roles
Play "1" *-- "0..*" File : vars_files
Host "1" *-- "0..*" Variable : vars
Role "1" *-- "0..*" Variable : vars
Role "1" *-- "0..*" Variable : defaults
Role "1" *-- "0..*" Task : tasks
Role "1" *-- "0..*" Task : handlers
Role "1" *-- "0..*" File : files
Role "1" --> "0..*" Role : dependencies
Task "1" *-- "1" Module : action
Task "1" *-- "0..1" Module : local_action
Task "1" *-- "0..*" Variable : register
Task "1" *-- "0..*" Variable : loop_control
Module "1" *-- "0..*" Variable : vars
apt ..|> Module
docker_image ..|> Module
docker_container ..|> Module
docker_network ..|> Module
launchd ..|> Module
docker_container "1" *-- "0..*" Variable : env
docker_container "1" *-- "0..*" Variable : networks
docker_container "1" *-- "0..*" Variable : log_options
The metamodel corresponds to the Ansible language of the MPS project.
Role dependencies are stored as role names (String) in the MPS language.
Module represents the action of a task; its subtypes represent the supported Ansible modules.
Only a part of the metamodel is parsed by the plugin and imported into the MPS transformation:
| Element | Parsed and imported |
|---|---|
Play |
name, become, hosts, pre_tasks, tasks, post_tasks, roles, vars |
Host |
hostName, group, vars |
Role |
name, vars, tasks, handlers, dependencies, files (defaults are only used to resolve variables in the plugin) |
Task |
name, become, vars, action, loop |
apt |
name, state |
docker_image |
name, source |
docker_container |
name, image, restart_policy, memory, state, network_mode, log_driver, env, networks, log_options |
docker_network |
name, driver |
launchd |
name, state, enabled |
All other attributes and associations, e.g., the handlers of a play, the vars_files, and the environment, are not parsed.
Transformation Process¶
The transformation follows the four steps of the DeMAF transformation process: the deployment model parser, the main model-to-model (M2M) transformation, the model merger, and the post M2M transformation.
Step 1: Deployment Model Parser¶
The parser reads the playbooks at the given locations, which must be files with the extension .yaml or .yml.
Directories are not supported due to the structure of Ansible projects.
A playbook is parsed as a list of plays; plays with the same name as a previously parsed play replace it.
For each play, the parser reads:
- Hosts: The inventory is read from the file
hosts.yamlorhosts.ymlin the directory of the playbook. It is expected to be structured as<group>: hosts: <host name>: <variable>: <value>. The hosts are filtered by thehostsof the play, which may beall, a host name, a group name, or a list of host names and group names. If no host matches, all hosts of the inventory are used. - Tasks: The
pre_tasks,tasks, andpost_tasksof the play. - Roles: For each role in
roles(given as- role: <name>), the filesroles/<name>/{tasks,handlers,vars,defaults,meta}/main.yamlrelative to the playbook are read. The dependencies are read from thedependenciesinmeta/main.yaml. All files inroles/<name>/files(including subdirectories) are added as files of the role. - Variables: The
varsof the play, of the roles (vars/main.yaml), of the role defaults (defaults/main.yaml), of the hosts, and of the tasks. Only variables with a string value or a list of string values are parsed.
For each task, the parser reads the name, become, vars, loop, and the action module.
The following modules are parsed:
| Module key in the task | Parsed into |
|---|---|
docker_container |
docker_container |
docker_image |
docker_image |
docker_network |
docker_network |
apt |
apt |
community.general.launchd |
launchd; a name of {{ item }} is replaced by the values of the task's loop |
Tasks with any other module are parsed with an unspecified action and are transformed without artifacts.
After parsing, references to variables in the form {{ <variable name> }} are replaced by the variable values, in the following order: the variables of the task and its role, the role defaults, and the variables of the play.
Note
Modules are only recognized by the keys listed above, not by their fully qualified collection names, e.g., community.docker.docker_container.
Role files with the extension .yml are not read, and roles must be given as - role: <name>; a role given as a plain string causes an exception.
The parser does not annotate the lines of the parsed files in the technology-specific deployment model (TSDM).
Step 2: Main M2M Transformation¶
The plugin serializes the parsed Ansible model to XML, which is imported and transformed into an EDMM model by the generator of the MPS project.
Overview of Mappings¶
| Ansible Construct | EDMM Construct |
|---|---|
| Ansible Deployment Model | Base Component Type & Relation Types |
| Host (of a play) | Component & Component Type |
| Role | Component & Component Type |
| Roles and Hosts in the same Play | hosted-on Relation |
| Role Dependency | connects-to Relation |
| Variable | Component Property |
| Task | Operation |
| Module | Operation Artifact |
docker_container Module |
Component Artifact & Component Property |
| File | Component Artifact |
| Play | not mapped to a component |
| Handler | not transformed |
Details¶
Ansible Deployment Model¶
For each transformed Ansible deployment model, the component type BaseType and the relation types DependsOn, HostedOn, and ConnectsTo are created, where HostedOn and ConnectsTo are derived from DependsOn.
Host¶
Each host of a play is mapped to:
- A component with the host name. It gets
- a property for each variable of the host,
- an operation for each task of the play's
pre_tasks,tasks, andpost_tasks(see Task and Module), and - an artifact of type
docker_imagefor eachdocker_containertask in thetasksof the play. - A component type
<host name>-typederived fromBaseTypewith the variable names of the host as property keys.
Role¶
Each role of a play is mapped to:
- A component with the name of the role. It gets
- a property for each variable of the role (
vars/main.yaml) and for each environment variable (env) of adocker_containertask of the role, - an operation for each task of the role (see Task and Module),
- an artifact of type
docker_imagefor eachdocker_containertask of the role, and - an artifact of type
filefor each file of the role, named after the file with the file URIfile:<absolute path>. - A component type
<role name>-typederived fromBaseTypewith the environment variables of thedocker_containertasks of the role as properties.
Roles and Hosts in the same Play¶
For each role and each host of the same play, a hosted-on relation <role name>_hostedOn_<host name> is created from the component of the role to the component of the host.
Role Dependency¶
For each dependency of a role, a connects-to relation <role name>_connectsTo_<dependency> is created from the component of the role to the component of the role with the name of the dependency in the same play.
Task and Module¶
Each task is mapped to an operation with the name of the task. Depending on the module of the task, the operation gets the following artifacts:
| Module | Artifacts of the operation |
|---|---|
docker_container |
docker_image artifact named after the image, and bash artifact /bin/sh docker run <image> |
docker_image |
docker_image artifact named after the image name, and bash artifact /bin/sh docker pull <name> |
docker_network |
bash artifact /bin/sh docker network create -d <driver> <name> |
apt |
apt artifact named after the package name |
launchd |
launchd artifact named <name>, extended by enabled and the state if set |
| any other module | none |
The file URI of these artifacts is -.
Step 3: Model Merger¶
The plugin merges the result of the MPS transformation into the EDMM model of the current transformation process:
- Components and relations are added to the existing ones.
- Component types and relation types with the same name are merged. Components and relations of the merged type are changed to use the existing type, and properties and operations that are not present are added to the existing component type.
Note
Components and relations are not checked for duplicates. If the same Ansible deployment model is analyzed several times in a transformation process, its components and relations are added several times.
Step 4: Post M2M Transformation¶
There is no post M2M transformation.
Embedded Deployment Models¶
The DeMAF currently supports the following embeddings of Ansible deployment models, which are described in detail in the overview of embedded deployment models:
- Ansible → Docker (reference embedding)