Ansible win_package doesn't work with brackets in registy variable - ansible

I'm trying to uninstall application by using product_id. I think the reason may be only one thing - brackets, because with other applications without brackets in registry variables it works pretty fine. How can i remove there brackets or somethink to solve this?
This works fine but not this
- name: Uninstalling Total Commander IT Edition
win_package:
product_id: '{7E25FC96-BC45-4D1C-AA24-2D147AD2B8D0}_is1'
state: absent
arguments: /7

Ok, i just grab the path, remove the brackets and put it back in.
- name: Obtain IT Editions's uninstall string for edit
win_reg_stat:
path: 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{7E25FC96-BC45-4D1C-AA24-2D147AD2B8D0}_is1'
name: UninstallString
register: uninst_str_IT
- name: Write properly path
win_regedit:
path: HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{7E25FC96-BC45-4D1C-AA24-2D147AD2B8D0}_is1
name: UninstallString
data: "{{uninst_str_IT.value | regex_replace ('\"', '')}}"
- name: Uninstalling Total Commander IT Edition
win_package:
product_id: '{7E25FC96-BC45-4D1C-AA24-2D147AD2B8D0}_is1'
arguments: /SILENT
state: absent

Related

Ansible replace text in file

So ive been tasked with replaceing zabbix server. To do so i have to modify zabbix_agent file in all server and there are many. Tho in this job is the first time i see ansible so i need some help. And i am using ansible-playbook.
In zabbix_agentd.conf file there is the old zabbix conf:
HostMetadata=Linux
PidFile=/var/run/zabbix/zabbx_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=zabbix.company.com
ServerActive=zabbix.company.com
HostnameItem=system.hostname
Include=/etc/zabbix_agentd.d/
Now i need to replace "Server" and "ServerActive" to "zabbix2.company.com"
I have tried various codes from this page to work for my needs but so far it has failed. No clue what im doing wrong
Try this one
- lineinfile:
path: /etc/zabbix_agentd.conf
regexp: '^\s*{{ key }}\s*=(.*)$'
line: '{{ key }}={{ value }}'
notify: reload zabix
loop:
- {key: 'Server', value: 'zabbix2.company.com'}
- {key: 'ServerActive', value: 'zabbix2.company.com'}
Notes
Path is required; probably /etc/zabbix_agentd.conf ?
It is not necessary to search the white-space \s* in regexp. However, it would match and fix potential spaces in the configuration.
Create and notify a handler reload zabix when anything changed. See Handlers: Running Operations On Change.
Take a look at Zabix modules.
I have manged to solve this issue using this code.
---
tasks:
- name: 'replace line'
lineinfile:
dest: /etc/zabbix/zabbix_agentd.conf
regexp: '^(.*)Server=zabbix.company.com(.*)$'
line: 'Server=zabbix2.company.com'
backrefs: yes

ANSIBLE - Answer yes no to questions

I am working on a playbook that will upgrade an app I have. Part of that upgrade means that I get asked a question.
Ideally I would like to be able to answer yes / no to these questions and not have to do any sort of pause or prompt for user input.
From what I have read online it is definitely possible but I cannot seem to get it to work for me.
Here is my code:
- name: Upgrade
expect:
command: /tmp/bin/update_script.sh
environment:
JAVA_HOME: /opt/java/
responses:
Question:
- Do you want to use the standard cipher suites [N]: n
You are mixing your response types. If you provide a list, it doesn't want/need the question. If you want to specifically match questions to responses, use a regex to match the question as the key of a dictionary.
So, version 1:
- name: Upgrade
expect:
command: /tmp/bin/update_script.sh
environment:
JAVA_HOME: /opt/java/
responses:
Question:
- n
or version 2 (check my regex*):
- name: Upgrade
expect:
command: /tmp/bin/update_script.sh
environment:
JAVA_HOME: /opt/java/
responses:
Question:
cipher: n
I have used expect, but not the ansible module, and it was years ago, and I never used it much. Hopefully this will work as-is either way, but be prepared to twiddle with it. ;)
Good luck!
This done what i needed .. Unfortunately it does not allow you answer yes / no but it gets the job done for now.
It would have been nice to have more control but its works as a temp workaround for now .. When I have a permanent solution I will post again.
- name: Upgrade Starting
shell: |
yes | /tmp/bin/update_script.sh
You should just pass the answer to the question as a variable using extra_variables. This will avoid the prompt and allow you to set the variable to some default value as well.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#passing-variables-on-the-command-line
ansible-playbook playbooks/release.yml --extra-vars 'updateciphers=True updatedcipherslist=["cipher1","cipher2","cipher3"]'
-> You may need to escape the double quotes.
vars:
updateciphers: False
tasks:
- name: Upgrade
command: /tmp/bin/update_script.sh
environment:
JAVA_HOME: /opt/java/
when: updateciphers
with_items: "{{ updatedcipherslist }}"

Escaping slash in a win_regedit path on Ansible

I have the following inside a playbook of Ansible 2.3.0.0:
- name: Disable SSL2, SSL3, RC4. Activate TLS
win_regedit:
path: 'HKLM:\System\CurrentControlSet\Control\SecurityProviders\SCHANNEL\{{ item.path }}'
name: "{{ item.name }}"
data: "{{ item.data }}"
type: dword
with_items:
# more items working correctly
- { path: "Ciphers\\RC4 128/128", name: 'Enabled', data: 0 }
- { path: "Ciphers\\RC4 40/128", name: 'Enabled', data: 0 }
- { path: "Ciphers\\RC4 56/128", name: 'Enabled', data: 0 }
I've tried every single combination of quotes and slashes I could think of to escape the /, and still either throws syntax error or considers the last 128 as another folder of the registry path rather than part of the key itself.
Is there any way Ansible can take that 128/128 literally and not as part of a path?
Sorry, but you are out of luck with win_regedit and forward slash.
win_regedit use PowerShell and Get-ItemProperty with friends under the hood.
And PowerShell treat forward slash character as level separator, whether you escape it or not.
You can google for some ways to overcome this in PowerShell (example1, example2).
But with win_regedit Ansible module you can't use that tricks.
So either you write your own PowerShell script with tricks from above articles and use script module, or prepare registry template and use win_regmerge module (it uses reg.exe under the hood) to import required settings.
Thanks to #KonstantinSuvorov I've done a workaround that, although ugly, works. Perform this step to create the registry key directly with PowerShell before the win_regedit:
- win_shell: $path=new-item -path 'HKLM:\System\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers';$key = (get-item HKLM:\).OpenSubKey("System\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers", $true);$key.CreateSubKey('RC4 128/128');$key.CreateSubKey('RC4 40/128');$key.CreateSubKey('RC4 56/128');$key.Close()

Ansible fails on presumably valid syntax

I work on a playbook. The valid syntax makes ansible complaining
Here is my playbook:
- hosts: tcagents
tasks:
- name: Create temporary dir C:\Tmp\windows-sdk-8.0
win_file: path=C:\TMP\windows-sdk-8.0 state=directory
- name: Copy windows-sdk-8.0/Windows-SDK-8.0.zip file to temporary dir on a node
win_copy:
src: Windows-SDK-8.0.zip
dest: C:\Tmp\windows-sdk-8.0\Windows-SDK-8.0.zip
- name: Unzip C:\TMP\windows-sdk-8.0\Windows-SDK-8.0.zip to C:\TMP\windows-sdk-8.0
win_unzip:
src: C:\TMP\windows-sdk-8.0\Windows-SDK-8.0.zip
dest: C:\TMP\windows-sdk-8.0
- win_command: C:\TMP\windows-sdk-8.0\setup.exe /Quiet /NoRestart
args:
chdir: C:\TMP\windows-sdk-8.0
It works without last three lines, but fails with them:
$ ansible-playbook -l windows windows-sdk-8.0/windows-sdk-8.0.yml
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/home/qaexpert/ansible-lab/windows-sdk-8.0/windows-sdk-8.0.yml': line 20, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
dest: C:\TMP\windows-sdk-8.0
- win_command: C:\TMP\windows-sdk-8.0\setup.exe /Quiet /NoRestart
^ here
Here is ansible version:
$ ansible --version
ansible 2.1.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
Please help!
Ansible win_command
is new in version 2.2 and you are running 2.1.2

how to extend windows path variable using ansible

Using win_environment, it is possible to add/remove environment variables to a windows host.
But to modify variables that are already there, win_environment does not seem to be useful as u can't read old value to modify and update a variable. right?
EDIT: Since Ansible 2.3, the win_path module does all the heavy lifting for you. Just give it a list of items that should be present in the path and it'll make sure they're present and in the relative order you specified.
(if you're still using an ancient version of Ansible, the following is still the way to go)
To get this to work sanely, you'll want to combine with a replace and search filter to only make the change if the value you want isn't in there. For instance (this is for Ansible 1.9):
- raw: echo %PATH%
register: path_out
- win_environment:
name: path
value: "{{ path_out.stdout | regex_replace('[\r\n]*', '') + ';C:\\\\newpath' }}"
state: present
level: machine
when: not (path_out.stdout | search("(?i)c:\\\\newpath"))
This is a lot harder than it should be- I've got half a mind to hack up a win_path module for 2.0 to make it easier...
For 2.0, raw runs under Powershell, so you'd want Get-Item env:PATH instead.
I just spent some hours fighting with Ansible, Jinja2, and JSON backslash hell and finally found a generic solution for this - ie, one that lets you add ANY directory to the system path, and won't add the same path twice. I adapted Devis' solution but made both the SETX command and the when: clause accept (the same) {{item}}, so it could be parameterized. Here's what I came up with.
Save this as extend-path.yml:
---
- name: Get current machine PATH.
raw: $ENV:PATH
register: path_out
- name: "Add {{ item }} to PATH."
raw: SETX /M PATH "$ENV:PATH;{{ item }}"
when: "not (path_out.stdout | urlencode | search( '{{ item | urlencode }}' ) )"
changed_when: true
And then, for example, in your playbook.yml:
---
tasks:
- name: Add tools to PATH.
include: extend-path.yml
with_items:
- C:\bin
- C:\Program Files (x86)\CMake\bin
- C:\Program Files\git\cmd
(As you see, I actually lost the backslash war and decided to bypass it entirely by using urlencode.)
Try this with Ansible 2.0
- name: Get actual PATH
raw: $ENV:PATH
register: path_out
tags: path
- name: Add Notepad++ to PATH
raw: SETX /M PATH "$ENV:PATH;C:\Program Files (x86)\Notepad++"
when: path_out.stdout.find('Notepad') == -1
tags: path
Here is an example that sets msbuild to the machine path. You could add more items if needed. It's important that you only retrieve the Machine path before then modifying the machine path. If you just call $ENV:PATH, you will get the machine path combined with the user path. If you use that to set the machine path, then you are copying all your user path values to the machine path which I'm assuming is not what you want.
- name: Get System PATH
raw: '[Environment]::GetEnvironmentVariables("Machine").Path'
register: path_out
- name: Modify System PATH
raw: SETX /M PATH "$([Environment]::GetEnvironmentVariables("Machine").Path | Out-String);{{ item }}"
when: path_out.stdout.find(item) == -1
with_items:
- 'C:\Program Files (x86)\MSBuild\14.0\Bin'
You can use Powershell for adding a string to the Path. The code below adds a given path to the PATH variable while ensuring path isn't modified if the given path is already existent in PATH.
$env = [Environment]::GetEnvironmentVariable('path','machine') -split ';'
$msdeploypath = 'C:\Program Files\IIS\Microsoft Web Deploy'
if ($env -notcontains $msdeploypath) {
$env += $msdeploypath
[Environment]::SetEnvironmentVariable('path', ($env -join ';'), 'machine')
Write-Host "changed"
}
In Ansible 2 you can also use the raw module for that as it uses Powershell
- name: Set Path
raw: $env = [Environment]::GetEnvironmentVariable('path','machine') -split ';' ; $msdeploypath = 'C:\Program Files\IIS\Microsoft Web Deploy' ; if ($env -notcontains $msdeploypath) { $env += $msdeploypath ; [Environment]::SetEnvironmentVariable('path', ($env -join ';'), 'machine') ; Write-Host "changed" }
register: pathchange
changed_when: pathchange.stdout.find('changed') != -1
Casey's solution is pretty close. The only problem is that [Environment]::SetEnvironmentVariable adds a newline at the end of the PATH. So when you add to it, it puts all your new values on another line making the PATH not work. Here's what I did, and it works pretty well.
Just needed to add a split on newlines... then the system PATH variable gets set correctly.
It's a combination of Casey's solution and Chris Hillery's:
in a file called extend-path.yml:
---
- name: Get current machine PATH.
raw: "$([Environment]::GetEnvironmentVariables(\"Machine\").Path -split '\r\n')"
register: path_out
- name: Print Out PATH
debug:
msg: "PATH: {{ path_out }}"
- name: "Add {{ item }} to PATH."
raw: SETX /M PATH "$($([Environment]::GetEnvironmentVariables("Machine").Path -split '\r\n'));{{ item }}"
when: path_out.stdout.find(item) == -1
changed_when: true
Then to call it, in your playbook:
- name: Update system PATH
include: tasks/win_system_path.yml
with_items:
- C:\Program Files\Git\bin

Resources