Packer Ansible provisioner is skipping tasks in Ansible playbook - ansible

I am using the Packer Ansible provisioner in a JSON file to download a zip file from the web that contains a program and have that program run at startup by editing the Windows Registry using the win_regedit Ansible module:
---
- hosts: default
tasks:
- name: Create Bginfo Directory
win_file:
path: C:\Bginfo
state: directory
- name: Download BgInfo64.zip
win_get_url:
url: https://download.sysinternals.com/files/BGInfo.zip
dest: C:\Bginfo\BgInfo64.zip
- name: Unzip BgInfo64.zip
win_unzip:
src: C:\Bginfo\BgInfo64.zip
dest: C:\Bginfo
delete_archive: true
- name: Run Bginfo at startup
win_regedit:
key: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
name: Bginfo
data: C:\Bginfo\Bginfo64.exe C:\BgInfo\default.bgi /timer:0 /nolicprompt
type: string
When I check the machine, the registry edits were not made and the archive is not deleted after un-zipping.
As per the logs, win_regedit is using module file Using module file /opt/ansible/ansible/lib/ansible/modules/windows/win_regedit.ps1
win_unzip is using /opt/ansible/ansible/lib/ansible/modules/windows/win_unzip.ps1
I looked up the PowerShell files online and everything I'm doing seems legal by Ansible standards so I'm unsure why those tasks are not completed.

Related

Is it possible to add a play inside a play that will use copy module on a define host vi ansible?

Hi I have a playbook that fetch information from remote server and place it local server, is it possible to add a play that will copy that file in local and place it on a specific host? I plan to code it below or you have any recommendation what would be best approach? although the server 1 is not in the inventory file that the playbook used.
- name: Get compliance reporting from remote
fetch:
src: /tmp/compliancereporting.out
dest: /home/ansible/linuxpatchingv2/OUTGOING-COMPLIANCE_v2/inventory_{{ '%y%m%d%H%M%S' | strftime }}
flat: yes
- name: Copy the fetch file
host: server1
copy:
src: /home/ansible/linuxpatchingv2/OUTGOING-COMPLIANCE_v2/inventory_*
dst: /tmp/
The fetch module will copy the file(s) from remote host to Ansible control machine at <dest>/ansible_hostname.
E.g. for host1.example.co and dest: /home/ansible/linuxpatchingv2:
/home/ansible/linuxpatchingv2/host1.example.co/tmp/compliancereporting.out
So in your playbook, you will have two plays. First play will fetch the file to Ansible control machine, second will copy the file fetched in first play to remote machine.
- name: Fetch the file from host1.example.co
hosts: host1.example.co
tasks:
- name: Fetch the file
fetch:
src: /tmp/compliancereporting.out
dest: /home/ansible/linuxpatchingv2/
- name: Copy the file to remote host server1
hosts: server1
tasks:
- name: Copy report to remote path
copy:
src: /home/ansible/linuxpatchingv2/host1.example.co/tmp/compliancereporting.out
dest: /tmp/

ansible link from directory to directory

I have a playbook that creates a directory, creates content on index.html, and a link from /web_hosting to /var/www/html.
The directory is called /web_hosting
the content is /web_hosting/index.html
I do not want to change the httpd.conf default web directory to /web_hosting I just want to use a link.
After running the play when I curl the server I'm not seeing the content from the index.html file.
Can someone help me with my play?
name: setup webserver and link to folder
hosts: prod
tasks:
name: create dir
file:
path: /web_hosting
state: directory
setype: httpd_sys_content_t
mode: 0775
name: install
yum:
name: httpd
state: present
name: configure service
service:
name: httpd
state: started
enabled: true
name: create content on index.html
copy:
dest: /web_hosting/index.html
content: "hello from {{ansible_hostname}}"
name: create link
file:
src: /web_hosting
dest: /var/www/html
state: link
This doesn't sound like an Ansible problem if it is creating the files and not erroring out.
If you manually create a file in /var/www/html/ called "index2.html", can you use curl to see it? If not, then it's definitely NOT an Ansible problem.
If that test works, then look for differences in ownership, SELinux permissions, etc. Then use Ansible to set those properly on your "index.html".
I suspect you might need to enable a "follow links" setting in your webserver configuration. But again, that's not an Ansible issue either - though Ansible could update the configuration file once you figure out what setting(s) to apply.

Ansible playbook to install NCPA agent on Windows

I am trying to install Nagios NCPA agent on Windows using Ansible play book. Here is my simple playbook
- name: Install NCPA
win_package:
path: https://assets.nagios.com/downloads/ncpa/ncpa-2.1.4.exe
- name: Copy the ncpa.cfg template
win_template:
src: ncpa.cfg.j2
dest: 'C:\Program Files (x86)\Nagios\NCPA\etc\ncpa.cfg'
- name: Restart NCPA
win_service:
name: ncpapassive
state: restarted
However I am getting the below error:
"msg": "product_id is required when the path is not an MSI or the path is an MSI but not local",
How to I find out the product_id for ncpa?
You can skip product_id if you add any of creates_* arguments to your first task, for example:
creates_path: C:\Program Files (x86)\Nagios\NCPA\___main_executable_file__.exe
Or you can search on a machine with your package istalled; per win_package manual:
product_id [ ]
You can find product ids for installed programs in the Windows registry editor either at HKLM:Software\Microsoft\Windows\CurrentVersion\Uninstall or for 32 bit programs at HKLM:Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall.
This SHOULD be set when the package is not an MSI, or the path is a url or a network share and credential delegation is not being used. The creates_* options can be used instead but is not recommended.
Finally managed to make it work with a dummy product ID.
- name: Create download directory
win_file:
path: C:\\Temp
state: directory
- name: Copy the executable package to download directory
win_copy:
src: ncpa-2.1.4.exe
dest: 'C:\Temp\ncpa-2.1.4.exe'
#- name: Download NCPA executable
# win_get_url:
# url: https://assets.nagios.com/downloads/ncpa/ncpa-2.1.4.exe
# dest: C:\Temp\ncpa-2.1.4.exe
# force: no
# skip_certificate_validation: yes
- name: Install NCPA
win_package:
path: 'C:\Temp\ncpa-2.1.4.exe'
arguments: '/S /TOKEN=demo-token'
product_id: '{ncpa}'
ignore_errors: true
register: installmsi
failed_when: "'was installed' not in installmsi.msg"
- name: Copy the ncpa.cfg template
win_template:
src: ncpa.cfg.j2
dest: 'C:\Program Files (x86)\Nagios\NCPA\etc\ncpa.cfg'
- name: Restart NCPA
win_service:
name: ncpapassive
state: restarted

Ansible win_file module register results

I am using the win_file Ansible module to create directories and files on Windows 2012 R2 servers. I want to register the results of the win_file task but I can't get it to work.
For example this playbook...
---
- hosts: windows_server
gather_facts: no
- name: create directory on remote windows server
win_file:
path: 'c:\temp}'
state: directory
register: task_results
debug: var=task_results
...does not print the results of the task.
Any help would be greatly appreciated.
Your playbook syntax is flawed... Try this:
---
- hosts: windows_server
gather_facts: no
tasks:
- name: create directory on remote windows server
win_file:
path: 'c:\temp'
state: directory
register: task_results
- debug: var=task_results
register is a part of task not module's parameter, so it should be with the same indentation.
debug is a new task, so it should be another item in a list.
Also there is not tasks keyword in your example.

template task: write to root owned directory

I want to copy a template generated file to /etc/init.d folder. But template task doesn't seem to support sudo parameter.
What is the recommended way to handle this? should I copy it to temporary directory and then move file with with sudo?
The playbook task looks like as shown below. Ansible version 1.8.2
- name: copy init script
template: src=template/optimus_api_service.sh dest=/etc/init.d/optimus-api mode=0755 force=yes owner=root group=root
I have tested the following playbook and it works.
My setup:
The User vagrant on the machine vm is allowed to execute commands password-free with sudo.
I created a simple template and installed it with the following playbook:
---
- name: Test template
hosts: vm
gather_facts: no
remote_user: vagrant
vars:
bla: blub # some variable used in the template
tasks:
- name: copy init script
sudo: yes # << you have to activate sudo
sudo_user: root # << and provide the user
template: src=template/test.j2 dest=/opt/test mode=0755 force=yes owner=root group=root

Resources