Ansible plug-in
The Ansible plug-in enables you to configure resources with Ansible and provides an agentless method for performing operations on remote hosts.
Playbook run operation
Similar to the Script Plug-in and the Fabric Plug-in, there is no one node type associated with the Ansible plug-in. Instead, you modify existing node types to perform one or more of their lifecycle operations using the Ansible plug-in and any additional inputs that you provide.
Node types
cloudify.nodes.ansible.Executor
Ansible Playbook Executor Node uses ansible.cloudify_ansible.tasks.run as the start
action.
Properties:
Property | Description |
site_yaml_path | A path to your site.yaml or main.yaml in your Ansible Playbook. |
sources | Your Inventory sources. Either YAML or a path to a file. If not provided, the inventory will be taken from the sources runtime property. |
run_data | Variable values. |
options_config | Command-line options, such as tags or skip_tags . |
ansible_env_vars | A dictionary of environment variables to set. Default is {"ANSIBLE_HOST_KEY_CHECKING": "False"} . |
debug_level | Debug level. |
"ANSIBLE_FACT_PATH"
environment variable, where you can add custom .fact
files. If the .fact
files are executable, they are run on the remote host. The JSON output is written to the standard output stdout
. Ansible reads the JSON data from the stdout
output and stores the data in the inventory in the runtime_properties.facts.ansible_local.{fact_file_name}
file. For example, you could add a YAML file in your playbook that is similar to the following YAML file sample.
- hosts: all
connection: local
tasks:
- name: "Set fact: output dictionary"
set_fact:
output_dict:
just_a_test: "my value from ansible gathered fact !!"
- name: "Creates facts directory if it doesn't exist"
file:
path: "{{ lookup('ansible.builtin.env', 'ANSIBLE_FACT_PATH') }}"
state: directory
- name: "Insert custom fact file"
copy:
content: "{{ output_dict | to_nice_json }}"
dest: "{{ lookup('ansible.builtin.env', 'ANSIBLE_FACT_PATH') }}/custom.fact"
mode: 0644
Operations
ansible.cloudify_ansible.tasks.run
Execute the equivalent of ansible-playbook
on the Ansible Playbook provided in the site_yaml_path
input.
Inputs:
Input | Description |
site_yaml_path | A path to your site.yaml or main.yaml in your Ansible Playbook. |
sources | Your Inventory sources. Either YAML or a path to a file. If not provided the inventory will be take from the sources runtime property. |
run_data | Variable values. |
options_config | Command-line options, such as tags or skip_tags . |
ansible_env_vars | A dictionary of environment variables to set. |
debug_level | Debug level. Default is 2. |
In addition, you can provide additional key-word args parameters to the AnsiblePlaybookFromFile
class, such as options_config
.
Inventory sources
For all inventory sources, we require these parameters:
Parameter | Description |
ansible_host | The hostname or IP address of the host to SSH into. |
ansible_user | The username to use with SSH. |
ansible_ssh_private_key_file | The private key file to use with SSH. |
In addition, we handle these parameters if provided and highly recommend them:
Parameter | Description |
ansible_become | A boolean value, true or false whether to assume the user privileges. |
ansible_ssh_common_args | Additional arguments to the SSH command like, we suggest, '-o StrictHostKeyChecking=no' . |
Using compute nodes
If your operation is mapped on the lifecycle operation of a node template derived from cloudify.nodes.Compute
, we will attempt to generate the sources
parameter from the node properties.
Provision some component on a VM. The following sample is in a YAML format.
compute_and_component:
type: cloudify.nodes.Compute
properties:
ip: { get_input: ip }
agent_config:
install_method: none
key: { get_input: private_key_path }
user: { get_input: username }
interfaces:
cloudify.interfaces.lifecycle:
start:
implementation: ansible.cloudify_ansible.tasks.run
inputs:
site_yaml_path: resources/component/site.yaml
Using relationships
Use the cloudify.ansible.relationships.connected_to_host
relationship defined in the plug-in to populate the sources parameter, if the target node is derived from cloudify.nodes.Compute
.
Inputs:
Input | Description |
group_name | Ansible node group name |
hostname | Host name |
host_config | Host configuration, which includes the proceeding inputs. |
ansible_host | The hostname or IP address of the host to SSH into. |
ansible_user | The username to use with SSH. |
ansible_ssh_private_key_file | The private key file to use with SSH. |
ansible_become | A boolean value, true or false whether to assume the user privileges. |
ansible_ssh_common_args | Additional arguments to the SSH command, by default: '-o StrictHostKeyChecking=no' |
Example relationship usage: The following sample is in a YAML format.
component:
type: cloudify.nodes.Root
interfaces:
cloudify.interfaces.lifecycle:
start:
implementation: ansible.cloudify_ansible.tasks.run
inputs:
site_yaml_path: resources/component/site.yaml
sources: { get_attribute: [ SELF, sources ] }
relationships:
- type: cloudify.ansible.relationships.connected_to_host
target: compute
compute:
type: cloudify.nodes.Compute
properties:
ip: { get_input: ip }
agent_config:
install_method: none
key: { get_input: private_key_path }
user: { get_input: username }
More examples:
Basic usage with no special node or relationship type behavior: The following sample is in a YAML format.
my_node:
type: cloudify.nodes.Root
interfaces:
cloudify.interfaces.lifecycle:
create:
implementation: ansible.cloudify_ansible.tasks.run
inputs:
site_yaml_path: resources/my_ansible_playbook/site.yaml
sources:
webservers:
hosts:
web:
ansible_host: { get_input: ip }
ansible_user: { get_input: username }
ansible_ssh_private_key_file: { get_input: private_key_path }
ansible_become: true
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
Passing run_data
at runtime. The following sample is in a YAML format.
component:
type: cloudify.nodes.Root
interfaces:
cloudify.interfaces.lifecycle:
create:
implementation: ansible.cloudify_ansible.tasks.run
inputs:
site_yaml_path: resources/my_ansible_playbook/site.yaml
sources:
foo_group:
hosts:
foo_host:
ansible_host: { get_input: ip }
ansible_user: { get_input: username }
ansible_ssh_private_key_file: { get_input: private_key_path }
ansible_become: true
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
run_data:
foo: bar