Skip to content

Commit

Permalink
Add support for ansible playbooks for bootstrapping #43
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Papke committed Nov 5, 2018
1 parent bbc70a6 commit d8a664d
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 27 deletions.
4 changes: 4 additions & 0 deletions data/example/ansible/apache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- hosts: all
roles:
- role: geerlingguy.apache
become: yes
1 change: 1 addition & 0 deletions data/example/ansible/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- geerlingguy.apache
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 0 additions & 12 deletions data/example/simple.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class UseCaseController extends ApplicationController {
private static final String MODEL_VAR_NAME = "useCase";
private static final String VAR_TYPE_FILE = "file";
private static final String VAR_NAME_SCRIPT_FILE = "script_file";
private static final String VAR_NAME_ANSIBLE_REQUIREMENTS_FILE = "ansible_requirements_file";
private static final String VAR_NAME_ANSIBLE_PLAYBOOK_FILE = "ansible_playbook_file";
private static final String VAR_NAME_PRIVATE_KEY_FILE = "private_key_file";
private static final String VAR_NAME_PUBLIC_KEY_FILE = "public_key_file";
private static final String VAR_NAME_RANDOM_ID = "random_id";
Expand Down Expand Up @@ -303,7 +305,7 @@ private static File writeMultipartFile(MultipartFile multipartFile) {

String multipartFileName = multipartFile.getOriginalFilename();
String prefix = FilenameUtils.getBaseName(multipartFileName);
String suffix = FilenameUtils.getExtension(multipartFileName);
String suffix = Constants.CHAR_DOT + FilenameUtils.getExtension(multipartFileName);
file = File.createTempFile(prefix, suffix);

FileUtils.writeByteArrayToFile(file, multipartFile.getBytes());
Expand Down Expand Up @@ -380,7 +382,13 @@ private File extendWithDefaultValues(List<Variable> variables, Map<String, Objec
privateKeyFile = generateKeyPair(variableMap, tempFileList);
}
else if (variableName.equals(VAR_NAME_SCRIPT_FILE)) {
addEmptyScriptFile(variableMap, tempFileList);
addEmptyScriptFile(variableMap, tempFileList, VAR_NAME_SCRIPT_FILE);
}
else if (variableName.equals(VAR_NAME_ANSIBLE_REQUIREMENTS_FILE)) {
addEmptyScriptFile(variableMap, tempFileList, VAR_NAME_ANSIBLE_REQUIREMENTS_FILE);
}
else if (variableName.equals(VAR_NAME_ANSIBLE_PLAYBOOK_FILE)) {
addEmptyScriptFile(variableMap, tempFileList, VAR_NAME_ANSIBLE_PLAYBOOK_FILE);
}
}
}
Expand All @@ -389,9 +397,9 @@ else if (variableName.equals(VAR_NAME_SCRIPT_FILE)) {
return privateKeyFile;
}

private void addEmptyScriptFile(Map<String, Object> variableMap, List<File> tempFileList) throws IOException {
private void addEmptyScriptFile(Map<String, Object> variableMap, List<File> tempFileList, String variableName) throws IOException {
File emptyScriptFile = File.createTempFile(EMPTY_SCRIPT_NAME, Constants.CHAR_EMPTY);
variableMap.put(VAR_NAME_SCRIPT_FILE, emptyScriptFile.getAbsolutePath());
variableMap.put(variableName, emptyScriptFile.getAbsolutePath());
tempFileList.add(emptyScriptFile);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ public class TerraformService extends ProvisionerService {

private static final String ANSIBLE_SSH_USER = "ansible_ssh_user";
private static final String ANSIBLE_SSH_PASS = "ansible_ssh_pass"; // NOSONAR
private static final String ANSIBLE_SUDO_PASS = "ansible_sudo_pass"; // NOSONAR
private static final String ESXI_USERNAME = "esxi_username";
private static final String ESXI_PASSWORD = "esxi_password"; // NOSONAR
private static final String USERNAME = "username";
private static final String PASSWORD = "password"; // NOSONAR
private static final String FLAG_NO_COLOR = "-no-color";
private static final String FLAG_VAR = "-var";
private static final String FLAG_FORCE = "-force";
Expand Down Expand Up @@ -114,23 +117,29 @@ protected void prepare(UseCase useCase, String action, Credentials credentials,
parameterStringBuilder.append(Constants.CHAR_NEW_LINE);
}

// create credentials map
Map<String, String> credentialsMap = new HashMap<>();

// add ansible ssh user and password
if (useCase.getProvider().equals(Constants.PROVISIONER_ESXI)) {

Map<String, String> credentialsMap = new HashMap<>();
credentialsMap.putAll(credentials.getSecretMap());
credentialsMap.put(ANSIBLE_SSH_USER, credentialsMap.get(ESXI_USERNAME));
credentialsMap.put(ANSIBLE_SSH_PASS, credentialsMap.get(ESXI_PASSWORD));

for (Entry<String, String> entry : credentialsMap.entrySet()) {
parameterStringBuilder.append(entry.getKey());
parameterStringBuilder.append(Constants.CHAR_DOUBLE_DOT);
parameterStringBuilder.append(Constants.CHAR_WHITESPACE);
parameterStringBuilder.append(entry.getValue());
parameterStringBuilder.append(Constants.CHAR_NEW_LINE);
}
}
else {
credentialsMap.put(ANSIBLE_SSH_USER, (String) variableMap.get(USERNAME));
credentialsMap.put(ANSIBLE_SSH_PASS, (String) variableMap.get(PASSWORD));
credentialsMap.put(ANSIBLE_SUDO_PASS, (String) variableMap.get(PASSWORD));
}

// add credentials map entries to parameter string
for (Entry<String, String> entry : credentialsMap.entrySet()) {
parameterStringBuilder.append(entry.getKey());
parameterStringBuilder.append(Constants.CHAR_DOUBLE_DOT);
parameterStringBuilder.append(Constants.CHAR_WHITESPACE);
parameterStringBuilder.append(entry.getValue());
parameterStringBuilder.append(Constants.CHAR_NEW_LINE);
}

// write parameter string to yaml file
fileService.createFile(parameterStringBuilder.toString(), getParameterFile(tmpFolder));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[defaults]

# disable SSH key host checking
host_key_checking = False

# hide deprecation warnings
deprecation_warnings = False

# hide command warnings
command_warnings=False
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,29 @@ resource "null_resource" "linuxprovisioning" {
inline = [
"echo '${var.password}' | sudo -S echo test",
"bash '${local.linux_prepare_script_path}' '${var.random_id}'",
]
}

provisioner "remote-exec" {
inline = [
"echo '${var.password}' | sudo -S echo test",
"bash '${local.linux_user_script_path}'",
]
}

provisioner "local-exec" {
command = "ansible-galaxy install -c -r ${var.ansible_requirements_file}"
on_failure = "continue"
}

provisioner "local-exec" {
command = "ansible-playbook -i ${vsphere_virtual_machine.linux.guest_ip_addresses.0}, -e ansible_python_interpreter=/usr/bin/python3 -e \"@parameters.yml\" --key-file ${var.private_key_file} ${var.ansible_playbook_file}"
on_failure = "continue"
}

provisioner "remote-exec" {
inline = [
"echo '${var.password}' | sudo -S echo test",
"bash '${local.linux_cleanup_script_path}'",
"rm -rf ${local.linux_script_folder_path}"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,16 @@ variableGroups:
- title: Script
name: script_file
description: Bootstrap script file to execute after vm has been created.
type: file
type: file

- title: Ansible Requirements
name: ansible_requirements_file
description: Requirements file specifying playbook dependencies to external roles.
type: file
url: "https://docs.ansible.com/ansible/2.7/reference_appendices/galaxy.html#installing-multiple-roles-from-a-file"

- title: Ansible Playbook
name: ansible_playbook_file
description: Playbook file specifying roles which have to executed on the host.
type: file
url: "https://docs.ansible.com/ansible/2.7/user_guide/playbooks.html"

0 comments on commit d8a664d

Please sign in to comment.