Go back

How to run Ansible Playbooks from Semaphore Web UI

This section explains how to leverage this VM for automating configuration management, application deployment, and task automation using Ansible and Semaphore UI.

In Ansible automation, you can use a wide variety of machines as target hosts. Since Ansible is agentless, it communicates over standard protocols like SSH (for Linux/Unix-based systems) and WinRM (for Windows-based systems).

  • The VM comes with the AnsibleExampleProject to get you started with. To run the example ‘install_apache.yaml’ playbook on localhost follow below steps.

  • Connect to Ansible Semaphore VM using terminal and run ‘apache2’ command, it will return with error ‘Command apache2 not found’.

apache2

/img/azure/ansible-semaphore-vm/apache2-on-localhost-before-install.png

  • We will need SSH keys to execute the playbooks on target machines. New SSH keys get created when the Ansible Semaphore VM is deployed. Run below command in the terminal and copy the private-key from start to end.
  cat /home/ubuntu/.ssh/id_rsa

/img/azure/ansible-semaphore-vm/cat-existing-private-key.png

  • Note if you don’t want to use the existing keys for security purpose then you can remove them and create the new pair.

To delete the existing keys run:

  sudo rm -r /home/ubuntu/.ssh/id_rsa /home/ubuntu/.ssh/id_rsa.pub

/img/azure/ansible-semaphore-vm/delete-existing-keypair.png

To create the new SSH key pair run below command and keep the passphrase empty.

  ssh-keygen -t rsa -b 4096 -f /home/ubuntu/.ssh/id_rsa

/img/azure/ansible-semaphore-vm/create-new-keypair.png

Now copy the newly created ssh private key from begining to end by running:

  cat /home/ubuntu/.ssh/id_rsa
  • Now Login to Semaphore UI using admin user and its password.

/img/azure/ansible-semaphore-vm/semaphore-login.png

  • Here AnsibleExampleProject is already created and configured for you to get started.

  • Go to Key Store option from left pane. Click on edit option in front of “Ansible Example KS” . New pop up will appear. Tick the Override checkbox at the bottom left. This will enable the page to edit. Paste the id_rsa private key content from the ssh terminal as shown in the above command. Keep all other fields as it is. Save the changes.

/img/azure/ansible-semaphore-vm/example-keystore.png

/img/azure/ansible-semaphore-vm/edit-example-keystore.png

  • ‘Ansible Example Repository’ is already created to run the ‘Ansible Example Template’. From left pane click on Repository and click on edit button of Ansible Example Repository to view the details.

/img/azure/ansible-semaphore-vm/example-repository.png

  • Repository path is set to /srv/ansible where we have our install_apache.yaml playbook created on this VM.

/img/azure/ansible-semaphore-vm/example-repository-detail-view.png

/img/azure/ansible-semaphore-vm/install_apache_playbook_content.png

  • Next we have Inventory configured to static type with localhost and ‘Ansible Example KS’ credentials which is our SSH key. Click on Inventory option from left pane and click on edit button of ‘Ansible Example Inventory’. We will also cover how to install apache on other ubuntu host in later section of this guide.

/img/azure/ansible-semaphore-vm/example-inventory.png

/img/azure/ansible-semaphore-vm/example-inventory-detail-view.png

  • Now Go to Task Template option from left pane, click on Install Apache2 template. It will open a detail view. Click on edit button.

/img/azure/ansible-semaphore-vm/task-template.png

/img/azure/ansible-semaphore-vm/task-template-edit-view.png

  • Click on Edit button at the top right to see the content of this template. Go through the Name, description, Playbook Filename, Inventory and repository field. Click on Cancel button to go back to template page.

/img/azure/ansible-semaphore-vm/task-template-detail-view.png

  • Click on Run button, a pop-up will appear. Provide any optional message here and hit run. Wait for it to finish. If the template is not showing any progress update even after 1-2 minute, refresh the page.

/img/azure/ansible-semaphore-vm/run-template.png

/img/azure/ansible-semaphore-vm/provide-message.png

/img/azure/ansible-semaphore-vm/template-execute-view.png

  • Once template is executed, goto terminal and execute ‘apache2’ command, now the command will be available as our playbook installed the apache2 on our localhost system.

/img/azure/ansible-semaphore-vm/apache2-on-localhost-after-install.png

  • Now run ‘sudo apt remove apache2 -y && sudo apt autoremove -y’ in the terminal to uninstall apache2 server from localhost.
sudo apt remove apache2 -y && sudo apt autoremove -y

/img/azure/ansible-semaphore-vm/apache2-remove-on-localhost.png

  • Follow below steps to run Ansible Example Template on any other ubuntu host. Please note that install_apache.yaml playbook is written with apt package manager. So it will run successfully on host which uses apt package manager like ubuntu and debian.

  • First we need to copy the public keys from Ansible VM to the target ubuntu VM. To do so connect to Ansible VM terminal and run below command:

Copy the keys on target VM using ssh-copy-id command:

ssh-copy-id user@target-host-ip

e.g ssh-copy-id ubuntu@172.190.241.204

  • It will prompt - ‘Are you sure you want to continue connecting (yes/no/[fingerprint])?’, enter yes here.

  • It will ask for the password of target machine. Provide the password and hit enter.

It should show ‘Number of key(s) added: 1’ success message.

/img/azure/ansible-semaphore-vm/copy-ssh-keys.png

  • Now try logging into the machine, with:
ssh user@target-host-ip

e.g ssh ubuntu@172.190.241.204

It will not ask you for any password and connect you to the target machine using SSH keys.

/img/azure/ansible-semaphore-vm/connect-to-target-using-ssh.png

  • Once you are successfully logged in to the host VM, check for apache2 is available or not by running apache2 command.
apache2

/img/azure/ansible-semaphore-vm/apache-on-target-before-install.png

  • Go to Semaphore UI and edit ‘Ansible Example Inventory’ to add the new target machine public IP as shown in the below screenshot. We are running playbook on localhost as well as on ubuntu target vm. If you don’t want to install apache2 on localhost , simply remove the localhost entry from the ‘Ansible Example Inventory’ and keep only ubuntu target machine IP. Save the changes.

/img/azure/ansible-semaphore-vm/update-inventory-with-target-ip.png

  • Now we are good to go for playbook execution on target machine.

  • Run the template and it should install apache2 on both localhost and new ubuntu target machine.

/img/azure/ansible-semaphore-vm/run-template-2.png

/img/azure/ansible-semaphore-vm/provide-message-02.png

/img/azure/ansible-semaphore-vm/template-executed-on-ubuntu-target-and-loalhost.png

  • Now the template is executed successfully, check the apache2 on localhost as well as on ubuntu target machine.

/img/azure/ansible-semaphore-vm/apache-on-localhost-2.png

/img/azure/ansible-semaphore-vm/apache-on-ubuntu-target-after-install.png

For more details on Ansible or Semaphore please refer to Official Documentation Page

Go back