Last active
October 13, 2024 15:53
-
-
Save MikeKlebolt/17496a5cb58f3d36b49237df105ebf29 to your computer and use it in GitHub Desktop.
Molecule setup used for testing Windows Ansible roles with Docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: Create | |
hosts: localhost | |
connection: local | |
gather_facts: false | |
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}" | |
tasks: | |
- name: Log into a Docker registry | |
docker_login: | |
username: "{{ item.registry.credentials.username }}" | |
password: "{{ item.registry.credentials.password }}" | |
email: "{{ item.registry.credentials.email | default(omit) }}" | |
registry: "{{ item.registry.url }}" | |
docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}" | |
with_items: "{{ molecule_yml.platforms }}" | |
when: | |
- item.registry is defined | |
- item.registry.credentials is defined | |
- item.registry.credentials.username is defined | |
- name: Create Dockerfiles from image names | |
template: | |
src: "{{ molecule_scenario_directory }}/Dockerfile.j2" | |
dest: "{{ molecule_ephemeral_directory }}/Dockerfile_{{ item.image | regex_replace('[^a-zA-Z0-9_]', '_') }}" | |
with_items: "{{ molecule_yml.platforms }}" | |
when: not item.pre_build_image | default(false) | |
register: platforms | |
- name: Discover local Docker images | |
docker_image_facts: | |
name: "molecule_local/{{ item.item.name }}" | |
docker_host: "{{ item.item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}" | |
with_items: "{{ platforms.results }}" | |
when: not item.pre_build_image | default(false) | |
register: docker_images | |
- name: Build an Ansible compatible image | |
docker_image: | |
path: "{{ molecule_ephemeral_directory }}" | |
name: "molecule_local/{{ item.item.image }}" | |
docker_host: "{{ item.item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}" | |
dockerfile: "{{ item.item.dockerfile | default(item.invocation.module_args.dest) }}" | |
force: "{{ item.item.force | default(true) }}" | |
pull: "{{ item.item.pull | default(omit) }}" | |
with_items: "{{ platforms.results }}" | |
when: | |
- platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0 | |
- not item.item.pre_build_image | default(false) | |
- name: Create docker network(s) | |
docker_network: | |
name: "{{ item }}" | |
docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}" | |
state: present | |
driver: l2bridge | |
driver_options: | |
com.docker.network.windowsshim.interface: Ethernet0 | |
with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks }}" | |
- name: Create molecule instance(s) | |
docker_container: | |
name: "{{ item.name }}" | |
docker_host: "{{ item.docker_host | default(lookup('env', 'DOCKER_HOST') or 'unix://var/run/docker.sock') }}" | |
hostname: "{{ item.hostname | default(item.name) }}" | |
image: "{{ item.pre_build_image | default(false) | ternary('', 'molecule_local/') }}{{ item.image }}" | |
state: started | |
recreate: false | |
log_driver: json-file | |
command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}" | |
privileged: "{{ item.privileged | default(omit) }}" | |
security_opts: "{{ item.security_opts | default(omit) }}" | |
volumes: "{{ item.volumes | default(omit) }}" | |
tmpfs: "{{ item.tmpfs | default(omit) }}" | |
capabilities: "{{ item.capabilities | default(omit) }}" | |
exposed_ports: "{{ item.exposed_ports | default(omit) }}" | |
published_ports: "{{ item.published_ports | default(omit) }}" | |
ulimits: "{{ item.ulimits | default(omit) }}" | |
networks: "{{ item.networks | default(omit) }}" | |
network_mode: "{{ item.network_mode | default(omit) }}" | |
dns_servers: "{{ item.dns_servers | default(omit) }}" | |
env: "{{ item.env | default(omit) }}" | |
restart_policy: "{{ item.restart_policy | default(omit) }}" | |
restart_retries: "{{ item.restart_retries | default(omit) }}" | |
register: server | |
with_items: "{{ molecule_yml.platforms }}" | |
async: 7200 | |
poll: 0 | |
- name: Wait for instance(s) creation to complete | |
async_status: | |
jid: "{{ item.ansible_job_id }}" | |
register: docker_jobs | |
until: docker_jobs.finished | |
retries: 300 | |
with_items: "{{ server.results }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Molecule managed | |
FROM microsoft/windowsservercore:latest | |
RUN powershell.exe -Command \ | |
wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -Proxy <proxy> -Outfile c:\remoting.ps1; \ | |
powershell.exe -ExecutionPolicy ByPass -File c:\remoting.ps1 -EnableCredSSP; \ | |
$password = ConvertTo-SecureString "<password>" -AsPlainText -Force; \ | |
Set-LocalUser -Name administrator -Password $password; \ | |
Enable-LocalUser -Name "Administrator"; \ | |
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase | |
EXPOSE 5986 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependency: | |
name: galaxy | |
driver: | |
name: docker | |
lint: | |
name: yamllint | |
platforms: | |
- name: servercore | |
hostname: {{ servername }} | |
image: microsoft/windowsservercore:latest | |
docker_host: tcp://<windows docker host>:2375 | |
command: ping -t localhost | |
networks: | |
- name: {{ VLAN }} | |
ipv4_address: {{ ip }} | |
purge_networks: yes | |
groups: | |
- {{ group }} | |
provisioner: | |
name: ansible | |
config_options: | |
defaults: | |
fact_caching: jsonfile | |
fact_caching_connection: /tmp | |
gathering: explicit | |
force_color: 1 | |
stdout_callback: yaml | |
vault_password_file: {{ password_file }} | |
lint: | |
name: ansible-lint | |
options: | |
flush-cache: True | |
scenario: | |
name: default | |
test_sequence: | |
- lint | |
- destroy | |
- dependency | |
- syntax | |
- create | |
- prepare | |
- converge | |
- idempotence | |
- side_effect | |
- destroy | |
verifier: | |
name: testinfra | |
lint: | |
name: flake8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Mike, this is great, do you have the full molecule folder components ? Or anywhere I can find it within GitHub ?