https://github.com/duffney/ansible-azure-samples
Sample Ansible playbooks for deploying and managing Azure resources.
https://github.com/duffney/ansible-azure-samples
Last synced: 3 months ago
JSON representation
Sample Ansible playbooks for deploying and managing Azure resources.
- Host: GitHub
- URL: https://github.com/duffney/ansible-azure-samples
- Owner: duffney
- Created: 2021-01-27T16:18:39.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-06T15:06:09.000Z (about 5 years ago)
- Last Synced: 2025-12-01T06:34:18.795Z (7 months ago)
- Language: Dockerfile
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ansible-on-azure
Sample Ansible playbooks for deploying and managing Azure resources.
## Prerequisites
* [Docker Desktop]()
* [PowerShell]() + [Azure PowerShell]() OR [AzCLI]()
## 1. Create an Azure AD Service Principal
### PowerShell
1. Create the Azure AD Service Principal Account
```powershell
$password = ''
$credentials = New-Object Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential `
-Property @{ StartDate=Get-Date; EndDate=Get-Date -Year 2024; Password=$password}
$spSplat = @{
DisplayName = 'ansible'
PasswordCredential = $credentials
}
$sp = New-AzAdServicePrincipal @spSplat
```
Replace `` with your password.
2. Assign the Contributor Role to the Service Principal
```powershell
$subId = (Get-AzContext).Subscription.Id
$roleAssignmentSplat = @{
ObjectId = $sp.id;
RoleDefinitionName = 'Contributor';
Scope = "/subscriptions/$subId"
}
New-AzRoleAssignment @roleAssignmentSplat
```
> NOTE: To improve security, change the scope of the role assignment to a resource group instead of a subscription.
### AzCLI
```azurecli
```
## 2. Build the Ansible Container
**Build the Ansible container image.**
Change to the `\ansible-container\$version` directory, `$version` represents the version of Ansible you want to run.
```powershell
docker build . -t ansible
```
## 3. Run the Ansible Container
### Output the required Azure AD Service Principal information
```powershell
@{
subscriptionId = (Get-AzContext).Subscription.Id
clientid = (Get-AzADServicePrincipal -DisplayName 'ansible').ApplicationId.Guid
tenantid = (Get-AzContext).Tenant.Id
}
```
### Run the Container
Windows
```powershell
docker run -it --rm --volume ${PWD}:/ansible --workdir /ansible `
--env "AZURE_SUBSCRIPTION_ID=" `
--env "AZURE_CLIENT_ID=" `
--env "AZURE_SECRET=" `
--env "AZURE_TENANT=" `
ansible
```
Linux
```bash
docker run -it --rm --volume "$(pwd):/ansible" --workdir /ansible \
--env "AZURE_SUBSCRIPTION_ID=" \
--env "AZURE_CLIENT_ID=" \
--env "AZURE_SECRET=" \
--env "AZURE_TENANT=" \
ansible
```
Replace `` with the information output in step 3.