Sample Ansible blueprint
The following is an example of an Ansible blueprint file, which can be used for any use case. The blueprint file defines the inputs and the node templates for the SSH connection to the remote host through the configurable secret. The blueprint file accepts playbook files, hosts files, and the secret to be used for retrieving the SSH private key from the file.
As a sample use case, the following Ansible blueprint shows how to install an anti-virus program. This sample is in a YAML format.
tosca_definitions_version: cloudify_dsl_1_3
imports:
- http://www.getcloudify.org/spec/cloudify/5.0.0/types.yaml
- plugin:cloudify-ansible-plugin
inputs:
site_yaml_relative_path:
type: string
default: playbook.yaml
hosts_relative_path:
type: string
default: hosts
ssh_private_key_secret:
type: string
description: SSH private key contents as string
node_templates:
ansible_playbook:
type: cloudify.nodes.Root
interfaces:
cloudify.interfaces.lifecycle:
create:
implementation: ansible.cloudify_ansible.tasks.run
inputs:
site_yaml_path: { get_input: site_yaml_relative_path }
sources: { get_input: hosts_relative_path }
run_data:
ssh_private_key_file: { get_secret : { get_input: ssh_private_key_secret } }
Sample hosts file
The following is a sample hosts file, which can be used for any use case. The host file contains the remote host IP address, username, and the run datafile defined in the blueprint. The Ansible playbook runs in the remote host through an SSH connection by reading the SSH private key value.
webservers:
hosts:
web:
ansible_become: true
ansible_connection: ssh
ansible_host: <remotehost_ipaddress>
ansible_ssh_user: <remotehost_username>
ansible_ssh_private_key_file: "{{ inventory_dir }}/{{ ssh_private_key_file }}"
Sample Ansible playbook
The following is a sample Ansible playbook that contains values specific to deploying an anti-virus package. However, you can also use the template to create an Ansible playbook for any use case. This sample is in a YAML format.
- name: "Playing with Ansible and Git"
hosts: localhost
tasks:
- name: "change permission of ssh_key_file"
shell: "chmod 600 {{ ssh_private_key_file }}"
- debug: var=output.stdout_lines
- name: "Playing with Ansible and Git"
hosts: webservers
become: yes
tasks:
- name: Install ClamAV anti-virus package using yum
ansible.builtin.yum: # for rhel hosts
name: clamav
state: latest
- debug: var=output.stdout_lines
# - name: Install ClamAV anti-virus package using yum
# ansible.builtin.apt: # for ubuntu hosts
# name: clamav
# state: latest
# - debug: var=output.stdout_lines