The following examples illustrate how you can use the Ansible plugin in CloudBees CD/RO.
Create a Run Playbook procedure with a snapshot
You can use the Run Playbook procedure to run Ansible playbooks.
To create a Run Playbook procedure:
-
Create a plugin configuration. For more information, refer to Create Ansible plugin configurations.
If using a Windows host, you must create a Windows plugin configuration. For more information, refer to Create a plugin configuration for Windows hosts. -
Navigate to
. -
Create a new procedure. For more information, refer to Create and manage procedures.
-
Select Add procedure step, and then select Create new….
-
Give the new step a name, select Definition, and then select Plugin in the left pane.
-
For the category, select Resource Management.
-
For the plugin, select EC-Ansible.
-
For the procedure, select Run Playbook.
-
Select Input parameters.
-
In the Configuration name field, add the EC-Ansible plugin configuration that you created.
-
Specify the parameters, as required for your project. For more information, refer to Run Playbook.
If using a Windows host, specify the path to the sample Windows playbook file for the Ansible Playbook path. Then, deploy the following sample Windows playbook to the path that you specified.
View the sample Windows playbook
--- - name: WindowsVm hosts: windows-vm1 connection: winrm vars: user: build tasks: - name: Create test folders on windows vm. win_file: path: C:\Users\build\test1 state: directory - name: Show test folders on vm home directory. win_shell: ls C:\Users\{{ user }}\ register: show_tables - debug: msg="{{ show_tables.stdout_lines }}" ....
-
Run the procedure. The following is an example of the step’s operations:
Figure 1. Step operationsThe task’s log should look similar to the following:
Figure 2. Task logThe task’s output should look similar to the following:
Figure 3. Task output
Create a Run Ad-Hoc Command procedure with a snapshot
You can use the Run Ad-Hoc Command procedure to run Ansible ad hoc commands.
To create a Run Ad-Hoc Command procedure:
-
Create a plugin configuration. For more information, refer to Create Ansible plugin configurations.
If using a Windows host, you must create a Windows plugin configuration. For more information, refer to Create a plugin configuration for Windows hosts. -
Navigate to
. -
Create a new procedure. For more information, refer to Create and manage procedures.
-
Select Add procedure step, and then select Create new….
-
Give the new step a name, select Definition, and then select Plugin in the left pane.
-
For the category, select Resource Management.
-
For the plugin, select EC-Ansible.
-
For the procedure, select Run Ad-Hoc Command.
-
Select Input parameters.
-
In the Configuration name field, add the EC-Ansible plugin configuration that you created.
-
Specify the parameters, as required for your project. For more information, refer to Run Ad-Hoc Command.
-
Run the procedure. The following is an example of the step’s operations:
Figure 4. Step operationThe task’s log should look similar to the following:
Figure 5. Task logIf using a Windows host, the output should look similar to the following:
Figure 6. Windows output
Add a Run Playbook task to a pipeline
You can add a Run Playbook task to a pipeline to run Ansible playbooks.
To create the Run Playbook task:
-
Create a plugin configuration. For more information, refer to Create Ansible plugin configurations.
If using a Windows host, you must create a Windows plugin configuration. For more information, refer to Create a plugin configuration for Windows hosts. -
Create a new pipeline. For more information, refer to Create a pipeline.
-
For the Pipeline stage, select Add task.
-
Give the new task a name, and then select Define.
-
For the Task type, select Plugin.
-
For the Plugin, select EC-Ansible.
-
For the Procedure, select Run Playbook.
-
Select Input parameters.
-
In the Configuration name field, add the EC-Ansible plugin configuration that you created.
-
Specify the parameters, as required for your project. For more information, refer to Run Playbook.
-
Run the pipeline.
Create an application to run Ansible playbooks
You can run Ansible playbooks from CloudBees CD/RO applications.
To run Ansible playbooks from an application:
-
Create a plugin configuration. For more information, refer to Create Ansible plugin configurations.
If using a Windows host, you must create a Windows plugin configuration. For more information, refer to Create a plugin configuration for Windows hosts. -
Create an application. For more information, refer to Create and manage applications.
-
Create an application component using the Artifact plugin or Artifactory plugin to retrieve the application’s Tomcat
*.war
artifact.Figure 7. Application component -
Select to create an application process. For more information, refer to Create application processes.
Figure 8. Application process -
Add a process step to run the EC-Ansible Run Playbook procedure. For more information, refer to Add process steps.
-
For the Step Type, select Plugins.
-
For the Plugin category, select Resource Management.
-
For the Plugin, select Ansible and then select EC-Ansible - Run Playbook.
-
In the Configuration name field, add the EC-Ansible plugin configuration that you created.
-
Add your procedure parameters and path to Ansible inventory with hosts.
-
For the Ansible Playbook path parameter, specify the path to the sample
deploy-playbook.yml
playbook file. -
Deploy the following sample
deploy-playbook.yml
in the path you specified for the Ansible Playbook path parameter.You can reuse the sample playbook with your application-specific changes. Windows platforms do not support the become
options.--- - name: Deploy Application hosts: linux-vm1 connection: ssh become: true become_user: root become_method: sudo vars: user: build tasks: - name: Delete application if exist file: state: absent path: /var/lib/tomcat7/webapps/hello-world.war - name: Copy Artifact to remote Tomcat Server copy: src: /home/ecloud/ansible/application/hello-world.war dest: /var/lib/tomcat7/webapps/ owner: root group: root mode: 0644 - name: Show application is copied shell: "ls -l /var/lib/tomcat7/webapps" register: out - debug: msg="{{ out.stdout_lines }}" - name: Restart Tomcat server shell: service tomcat7 restart register: restart - debug: msg="{{ restart.stdout_lines }}" ....
-
Map the application to an existing an Ansible resource (local or remote) and run the application.
Run playbooks with Ansible vault
-
Create a sample playbook and encrypt it using the following command:
ansible-vault encrypt /path/to/playbook.yml --ask-vault-pass.
-
Enter the vault password.
-
Create a new plugin configuration and set the Vault credentials. Ensure your Ansible playbook path parameter and password match the vault that you want to use. For more information, refer to Create Ansible plugin configurations.
When you run the Ansible Run Playbook procedure, ensure you set the Configuration name to the one you configured the Vault credentials for.