Skip to content

Instantly share code, notes, and snippets.

@thejhh
Last active December 10, 2020 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejhh/1995488d6fc5c5a0449cea840ae52325 to your computer and use it in GitHub Desktop.
Save thejhh/1995488d6fc5c5a0449cea840ae52325 to your computer and use it in GitHub Desktop.
Example how to write Ansible playbooks in TypeScript
// Copyright (c) 2020 Sendanor. All rights reserved.
import Ansible, {AnsibleTask as Task} from "../utils/Ansible";
import Build from "../utils/Build";
import Tag from "../types/Tag";
import Play from "../types/Play";
import {FileTaskState} from "../types/FileTask";
enum InstallWpVariable {
WP_CLI_EXISTS = "wp_cli_exists"
}
export class InstallWp {
public static Variable = InstallWpVariable;
public static play (
hosts : string,
commandName = 'wp',
binDir = '{{ ansible_env.HOME }}/bin',
wpUrl = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar'
) : Play {
return Ansible.play({
hosts: hosts,
tasks: [
Task.stat({
name: "Check if wp-cli exists",
stat: {
path: `${binDir}/${commandName}`
},
register: InstallWp.Variable.WP_CLI_EXISTS,
changed_when: false,
tags: [Tag.ALWAYS]
}),
Task.shell({
name: "Update wp-cli",
shell: `${binDir}/${commandName} cli update --yes`,
when: `${InstallWp.Variable.WP_CLI_EXISTS}.stat.exists`,
tags: [Tag.WP]
}),
Task.file({
name: "Create bin directory if does not exist",
file: {
path: binDir,
state: FileTaskState.DIRECTORY,
mode: '0700'
},
when: `not ${InstallWp.Variable.WP_CLI_EXISTS}.stat.exists`
}),
Task.getUrl({
name: "Download wp-cli",
getUrl: {
url: wpUrl,
dest: `${binDir}/${commandName}`,
forceBasicAuth: true,
mode: '0700'
},
when: `not ${InstallWp.Variable.WP_CLI_EXISTS}.stat.exists`,
tags: [
Tag.WP,
Tag.INSTALL
]
}),
]
})
}
}
Build.json( Ansible.playbook([ InstallWp.play("wordpress") ]) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment