Archive for the ‘ansible’ Category

It’s presumed Chocolatey server will be installed on D drive,it’s presumed Chocolatey server has no internet access.

Choco installation files are obtained in following way:

On any windows machine with internet access following has been done:

  • installed chocolatey
  • after chocolatey has been installed following command has been executed:
    choco install chocolatey.server
  • all required files are downloaded to C:\tools folder

Capture.PNG

Copy tools folder somewhere to Ansible server, Ansible playbook will copy  it to D drive on Windows server.

Folder structure:

|—————–winplaybook/choco.yml   chocoserver/tools/

|—————group_vars/

|             | ————–features/features.yml

|————–windows/vars_win.yml   vaul_win.yml

features.yml contains list of IIS features and IIS users

---
features:
- Web-Server
- Web-Asp-Net45
- Web-AppInit 

users:

- IIS_IUSRS
- IUSR
- IIS APPPOOL\ChocolateyServer

chocoserver/tools contains chocolatey server installation (copied from windows machine with internet access)

Chocolatey API key is in vars_win.yml (unencrypted-point to vault_win.yml) and vault_win.yml (encrypted)

vars_win.yml:

api_key: '{{ vault_api_key }}'

vault_win.yml:

vault_api_key: myapi

Playbook will copy Chocolatey server files to D drive, installs IIS server and features,removes default IIS web site, creates Chocolatey application pool, sets ACL permissions on D:\tools\chocolatey.server and D:\tools\chocolatey.server\App_Data,creates Chocolatey IIS site and changes default API key

– name: install choco server
hosts: dc2
vars_files:
– group_vars/windows/vars_win.yml
– group_vars/features/features.yml
gather_facts: yes
tasks:
– name: Copy Chocolatey server to D drive
win_copy:
src: /root/win_playbooks/choco_server/
dest: D:\
– name: Ensure IIS is installed
win_feature:
name: Web-Server
state: present
include_management_tools: True
– name: Ensure IIS Web-Server and ASP.NET are installed
win_feature:
name: ‘{{ item }}’
state: present
with_items: ‘{{ features }}’
– name: Ensure Default Web Site is not present
win_iis_website:
name: “Default Web Site”
state: absent
#- name: Chocolatey.server package is installed
# win_chocolatey:
# name: “chocolatey.server”
# state: present
– name: Configure AppPool for Chocolatey.server
win_iis_webapppool:
name: ChocolateyServer
state: started
attributes:
enable32BitAppOnWin64: true
managedRuntimeVersion: v4.0
managedPipelineMode: Integrated
startMode: AlwaysRunning
autoStart: true
– name: Grant read permissions to D:\tools\chocolatey.server
win_acl:
user: ‘{{ item }}’
path: D:\tools\chocolatey.server
rights: Read
state: present
type: allow
inherit: ContainerInherit, ObjectInherit
progagation: InheritOnly
with_items: ‘{{ users }}’
– name: Grant IIS APPPOOL\ modify permissions to D:\tools\chocolatey.server\App_Data
win_acl:
user: ‘{{ item }}’
path: D:\tools\chocolatey.server
rights: Modify
state: present
type: allow
inherit: ContainerInherit, ObjectInherit
progagation: InheritOnly
with_items: ‘{{ users }}’
– name: Create Chocolatey IIS site
win_iis_website:
name: “chocolatey”
state: started
port: 80
#ip: 127.0.0.1
application_pool: “ChocolateyServer”
physical_path: D:\tools\chocolatey.server
parameters: logfile.directory:c:\ChocolateyServer\logs
register: website
– name: Change default API key
win_lineinfile:
path: D:\tools\chocolatey.server\web.config
regexp: ‘<add key=”apiKey” value=”chocolateyrocks” />’
line:’         <add key=”apiKey” value=”{{ api_key }}”/>’
state: present

Advertisements

Unlike other tasks, this one requires runas (become) permissions. So, we need to specify become statement in playbook, and to add following directives in group_vars folder (see this guide how to create it.

add 4 “ansible_become” lines as per example

---
ansible_user: Administrator
ansible_password: Pass
ansible_connection: winrm
ansible_port: 5986
ansible_winrm_server_cert_validation: ignore
ansible_become: yes
ansible_become_user: Administrator
ansible_become_pass: Passw
ansible_become_method: runas
Both are same account,it’s local admin account promoted to Domain Administrator after creating AD Domain, the reason why we need to add those 4 lines is because renaming AD joined machines required Active Directory credentials, those 4 “ansible_become” lines instruct Ansible to use domain administrator credentials instead of local administrator.
- name: Change computer name
  hosts: dc2
  tasks:
   - name: Change host name
     become: yes
     win_hostname:
       name: server2
     register: name_changed
   - name: reboot server after hostname changes
     win_reboot:
       msg: "Computer name changed,rebooting...."
       pre_reboot_delay: 15
     when: name_changed.changed

 

In this example Page file will be moved to D drive, in order for Ansible to “track changes” file C:\Pagefile.log will be created after Page file is moved and server will be restarted afterwards.

In this example page file is set to automatic (InitialSize = 0; MaximumSize = 0), we can set custom Initial/Maximum size (in MB).

- name: Moving page file to another drive
  hosts: winserver
  gather_facts: yes
  tasks:
   - name: "Move Page File to D Drive"
     win_shell: |
       $a=Get-WmiObject -Query "select * from Win32_PageFileSetting"
       if ($a.Name -like 'C:\pagefile.sys') {
       $CurrentPageFile = Get-WmiObject -Query "select * from Win32_PageFileSetting where name='c:\\pagefile.sys'"
       $CurrentPageFile.delete()
       Set-WMIInstance -Class Win32_PageFileSetting -Arguments @{name="d:\pagefile.sys";InitialSize = 0; MaximumSize = 0}
       } write-output "Done" | out-file C:\Pagefile.log -Append
     args:
      creates: C:\Pagefile.log
     register: page
   - name: reboot server
     win_reboot:
       msg: "Page file moved,rebooting..."
       pre_reboot_delay: 15
     when: page.changed

Ansible have no module for setting Domain Group Policies,but we can use PowerShell commands. If we need to create Custom GPO and link it to some GPO, we can do it also by Powers shell – by setting Registry Values.

The key must be in one of the two following registry hives:

  • HKEY_LOCAL_MACHINE (HKLM) for a registry-based policy setting in Computer Configuration.
  • HKEY_CURRENT_USER (HKCU) for a registry-based policy setting in User Configuration.

Bellow is example for setting Screen Saver TimeOut to 900 seconds for User Configuration Settings GPO.

Capture

New GPO named BO-1-Desktops is created and linked to test OU.

- name: Configure GPO
        hosts: winserver
        tasks: 
          - name: Set ScreenSaver Timeout
            win_shell: | 
              New-GPLink -Name "BO-1-Desktops" -Target 
 "OU=test,DC=ad,DC=contoso,DC=com";
              Set-GPRegistryValue -Name "BO-1-Desktops" -KEY "HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop" -ValueName "ScreenSaveTimeOut" -Type DWORD      -Value 900 > C:\screensaverGPO.txt
            args: 
              creates: C:\screensaverGPO.txt

Script won’t be executed if C:\screensaverGPO.txt exists.

Example for creating DNS zone

- name: Create DNS
  hosts: winserver
  tasks:
   - name: Create Forward lookup zone
     win_shell: |
       try {$getdns=Get-DnsServerZone -Name "west01.contoso.com" -ErrorAction SilentlyContinue
       if ($getdns -eq $null) {Add-DnsServerPrimaryZone -Name "west01.contoso.com"       -ReplicationScope "Forest" -PassThru > c:\dnszone.log}
       } catch {write-host "ok"}
     args:
       creates: C:\dnszone.log

This post explained how to use it with Puppet, in this post we’ll do the same.

Powershell script is doing following:

  •  reads JSON file above
  • Creates subnets defined from JSON file
  • Creates sites defined in JSON file
  • Assigns subnet to AD site (as specified in JSON file)

JSON and PS1 files are located in  /root/win_playbooks/files/ folder.

Both files will be copied to C:\Script

Script will be running only if file C:\Script\Logs\ADSite.log is present.

- name: Configure AD sites
hosts: winserver
gather_facts: yes
tasks:
- name: Create folder
    win_file:
     path: C:\Script
     state: directory
- name: Copy files
  win_copy:
     src: /root/win_playbooks/files/
     dest: C:\Script\
- name: Run Script
  win_shell: C:\Script\createsite.ps1
  args:
     creates: C:\Script\logs\CreateADSIte.log

 

File structure is same as in this post.

Playbook for Joining Machine to AD Domain:

name: install second domain controller
 hosts: dc2
 vars_files:
 - group_vars/windows/vault_win.yml
 gather_facts: yes
 tasks:
 - name: Configure Primary DNS Server
 win_dns_client:
     adapter_names: 'Ethernet 2'
     ipv4_addresses:
       - 192.168.1.182
 - name: Join to the domain
   win_domain_membership:
     dns_domain_name: '{{ dns_name }}'
     domain_admin_user: '{{ domain_admin_user }}'
     domain_admin_password: '{{ vault_ad_admin_pass }}'
     state: domain
 register: domain_state
 - name: Reboot after joining
   win_reboot:
     msg: "Joining Domain.Rebooting..."
     pre_reboot_delay: 15
   when: domain_state.reboot_required</pre> 

DNS name,domain admin,safe mode password and admin passwords and will be stored in 2 separate files:

|——-win_playbooks/

|           |–group_vars/

|              | –windows/

|                  | —-vars_win.yml (unencrypted) – dns_name,safe_mode_password (points to vault_win.yml-vault_password),domain_admin_user,domain_admin_password (points to vault_win.yml-vault_admin_pass)

|                 |—-vault_win.yml (encrypted) – vault_password and vault_admin_pass

vars_win.yml:

dns_name: ad.contoso.com
safe_mode_password: '{{ vault_password }}'
domain_admin_user: 'administrator@ad.contoso.com'
domain_admin_password: '{{ vault_ad_admin_pass }}'

vault_win.yml:

vault_password: SomePass
vault_ad_admin_pass: SomePass

Playbook for adding additional Domain Controller:

- name: install second domain controller
  hosts: dc2
  vars_files:
   - group_vars/windows/vault_win.yml
  gather_facts: yes
  tasks:
   - name: Configure Primary DNS Server
     win_dns_client:
       adapter_names: 'Ethernet 2'
       ipv4_addresses:
         - 192.168.1.182
- name: Promote this server to Additional DC
  win_domain_controller:
       dns_domain_name: '{{ dns_name }}'
       domain_admin_user: '{{ domain_admin_user }}'
       domain_admin_password: '{{ vault_ad_admin_pass }}'
       safe_mode_password: '{{ safe_mode_password }}'
       state: domain_controller
       log_path: c:\ansible_win_domain_controller.txt