Ansible plugin use cases

4 minute readExtensibilityDeveloper productivity

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:

  1. 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.
  2. Navigate to DevOps Essentials  Procedures.

  3. Create a new procedure. For more information, refer to Create and manage procedures.

  4. Select Add procedure step, and then select Create new…​.

  5. Give the new step a name, select Definition, and then select Plugin in the left pane.

  6. For the category, select Resource Management.

  7. For the plugin, select EC-Ansible.

  8. For the procedure, select Run Playbook.

  9. Select Input parameters.

  10. In the Configuration name field, add the EC-Ansible plugin configuration that you created.

  11. 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 }}"
    ....
  12. Run the procedure. The following is an example of the step’s operations:

    Step operations
    Figure 1. Step operations

    The task’s log should look similar to the following:

    Task log
    Figure 2. Task log

    The task’s output should look similar to the following:

    Task output
    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:

  1. 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.
  2. Navigate to DevOps Essentials  Procedures.

  3. Create a new procedure. For more information, refer to Create and manage procedures.

  4. Select Add procedure step, and then select Create new…​.

  5. Give the new step a name, select Definition, and then select Plugin in the left pane.

  6. For the category, select Resource Management.

  7. For the plugin, select EC-Ansible.

  8. For the procedure, select Run Ad-Hoc Command.

  9. Select Input parameters.

  10. In the Configuration name field, add the EC-Ansible plugin configuration that you created.

  11. Specify the parameters, as required for your project. For more information, refer to Run Ad-Hoc Command.

  12. Run the procedure. The following is an example of the step’s operations:

    Step operation
    Figure 4. Step operation

    The task’s log should look similar to the following:

    Task log
    Figure 5. Task log

    If using a Windows host, the output should look similar to the following:

    Windows output
    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:

  1. 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.
  2. Create a new pipeline. For more information, refer to Create a pipeline.

  3. For the Pipeline stage, select Add task.

  4. Give the new task a name, and then select Define.

  5. For the Task type, select Plugin.

  6. For the Plugin, select EC-Ansible.

  7. For the Procedure, select Run Playbook.

  8. Select Input parameters.

  9. In the Configuration name field, add the EC-Ansible plugin configuration that you created.

  10. Specify the parameters, as required for your project. For more information, refer to Run Playbook.

  11. 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:

  1. 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.
  2. Create an application. For more information, refer to Create and manage applications.

  3. Create an application component using the Artifact plugin or Artifactory plugin to retrieve the application’s Tomcat *.war artifact.

    Application component
    Figure 7. Application component
  4. Select app plus gear icon to create an application process. For more information, refer to Create application processes.

    Application process
    Figure 8. Application process
  5. Add a process step to run the EC-Ansible Run Playbook procedure. For more information, refer to Add process steps.

  6. For the Step Type, select Plugins.

  7. For the Plugin category, select Resource Management.

  8. For the Plugin, select Ansible and then select EC-Ansible - Run Playbook.

  9. In the Configuration name field, add the EC-Ansible plugin configuration that you created.

  10. Add your procedure parameters and path to Ansible inventory with hosts.

  11. For the Ansible Playbook path parameter, specify the path to the sample deploy-playbook.yml playbook file.

  12. 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 }}"
    ....
  13. Map the application to an existing an Ansible resource (local or remote) and run the application.

Run playbooks with Ansible vault

  1. Create a sample playbook and encrypt it using the following command:

    ansible-vault encrypt /path/to/playbook.yml --ask-vault-pass.
  2. Enter the vault password.

  3. 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.