I have created a playbook to install/upgrade a third party vendor application on a Windows VM. I am using the ansible tower to execute the playbook. I used the below script to upgrade
- name: Run the installer to upgrade the app
win_shell: powershell.exe -executionpolicy bypass -noninteractive -nologo -file '.\Install.ps1'
args:
chdir: "{{ app_path }}"
I wasn't able to proceed on the installation. While trying it manually, I have noticed that there were 2 db migration script execution related pop-up windows appearing (triggered by the anti-virus as it is an execution of an exe file in middle of the installer script - due to security reasons). I had to select 'Allow this file' option to continue the execution, while trying it manually.
I was told by the product vendor that I need to add the folders where the script is available as an exception in anti-virus to prevent the pop-up. But I would like to know - is there an option for silent / unattended execution of the scripts on Windows VM in ansible - which will directly take care of allowing the execution? I heard about -s in RHEL VMs; but couldn't find any options for Windows VMs.
Appreciate your help!!
You can try the following code snippet
- hosts: localhost
tasks:
- name: Install Chocolatey
win_shell: |
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
args:
executable: powershell.exe
register: install_choco
changed_when: install_choco.rc == 0
failed_when: install_choco.rc != 0
ignore_errors: yes
- name: Install Ansible
win_shell: choco install ansible -y
args:
executable: powershell.exe
register: install_ansible
changed_when: install_ansible.rc == 0
failed_when: install_ansible.rc != 0
ignore_errors: yes
- name: Install Git
win_shell: choco install git -y
args:
executable: powershell.exe
register: install_git
changed_when: install_git.rc == 0
failed_when: install_git.rc != 0
ignore_errors: yes
Related
I have a script that I want to write as a playbook.
first I should go to my specific directory and then run some tsm commands for backup and cleanup.
I tried to change directory which works fine. but the tasks after that("list files") is running in my home directory.
How can I change the working directory for all tasks?
Thanks for your help!
- name: "Change Directory"
win_shell: cd /d E:\mydirectory
args:
executable: cmd
- name: list files
win_shell: dir
args:
executable: cmd
register: myfiles
Each task is independent, so you have to change the working directory on each win_shell invocation. One way to do this is to use module_defaults on a block:
- module_defaults:
win_shell:
executable: cmd
chdir: 'E:\mydirectory'
block:
- name: list files
win_shell: dir
register: myfiles
Sometimes I have to wait very long during running ansible command. For example such command can be executed 30 minutes for my module:
- name: make project
shell: make -j4 install
args:
chdir: "{{ project_dir }}/build"
I would like to see the stdout of this command live, during runtime, not when the command has finished (I see the output when the command has finished, but I need responsiveness, so I'm not interested in something like -v, -vvv or -vvvv). Is it possible to force ansible to print the output during running command (not to buffer it and not to print it at the end)?
You can't print the output while the command runs, but you can print it after the command finished:
- name: make project
shell: make -j4 install
args:
chdir: "{{ project_dir }}/build"
register: out
ignore_errors: yes
- name: print output
debug:
msg: "{{ out.stdout }}"
- name: fail when make failed
fail:
msg: "make task failed"
when: out.rc != 0
The ignore_errors: yes and the fail-task are there, so the output will get printed before your play fails in case the make-task fails.
You should also consider using the make module instead of running make in a shell.
I would like to simply reboot the VCSA using Ansible as part of a development workflow. Does anybody have any ideas as to how to do so?
https://kb.vmware.com/s/article/2147152
This would normally be done by hitting 'shell' and then turning off/on the service and/or 'reboot'
I've been playing around with ansible.raw, but it seems to hang indefinitely.
A few of the attempts I have tried:
tasks:
- name: 'get into the shell'
raw: 'shell'
register: shell
- debug: msg="{{ shell }}"
- name: 'reboot'
raw: 'reboot'
register: reboot
- debug: msg="{{ reboot }}"
- name: Unconditionally reboot the machine with all defaults
reboot:
I resolved this by manually going into every system and changing the default shell to /bin/bash
https://communities.vmware.com/t5/vCenter-Server-Discussions/Access-to-VCSA-through-SSH-and-or-local-console-is-NOT-working/td-p/1816293
############
~# vi /etc/passwd
and change the appliancesh to /bin/bash
root:x:0:0:root:/root:/bin/bash
Also make sure that ssh access for root is enabled from /etc/ssh/sshd_config
Before running a patching playbook, I ran the playbook with the "--check" option as a dry run. However, one of the plays within the playbook to check whether the group needs a reboot, doesn't register the "reboot_hint" variable as intended
- name: check for reboot
shell: needs-restarting -r
register: reboot_hint
failed_when: reboot_hint.rc > 1
- name: debug, show the reboot hint variable
debug:
var:
- reboot_hint.rc
- reboot_hint
I get the message: ""VARIABLE IS NOT DEFINED!"" for the run.
What could be causing this? I am expect a return value of "1" or "0". I do get that when I go into the command line, run the "needs-restarting -r" and "echo $?
No core libraries or services have been updated.
Reboot is probably not necessary.
> echo $?
0
You're running --check and thus the shell command doesn't run. Because the shell command doesn't run there's nothing to register.
You can read more about this in https://docs.ansible.com/ansible/latest/user_guide/playbooks_checkmode.html#enabling-or-disabling-check-mode-for-tasks
A simple fix is adding check_mode: no, e.g.
- name: check for reboot
check_mode: no
shell: needs-restarting -r
register: reboot_hint
failed_when: reboot_hint.rc > 1
This forces the task to run even when check-mode is enabled.
hi I want to execute "java -jar xxxxxxx.war 172.xx.xxx.xx" command on a host.
I am able to execute in a single system through Ansible, but I want to execute that command in a multiple hosts and that execution needs to be done in their respective system IP's.
eg:
java -jar xxxxxxx.war 172.xx.xxx.01 needs to be executed on the system with the IP 172.xx.xxx.01
java -jar xxxxxxx.war 172.xx.xxx.02 needs to be executed on the system with the IP 172.xx.xxx.02
- name: Executing the jar file
win_shell: "CMD /C \"java -jar generateKeyStoreFiles.jar storeipTest.csv\""
args:
chdir: C:/Keystore_Pem_FileAutoGenerationOracle_allgoodthings/
- name: Copying of Xstore POS Installer
win_copy:
src: /root/Installers/Softwares/Xstore
dest: C:/Installers/Softwares/
- name: Copying of xstore_mobile.keystore file
win_copy:
src: C:/Keystore_Pem_FileAutoGenerationOracle_allgoodthings/finalcerts/cert/xstore_mobile.keystore
dest: C:/Installers/Softwares/Xstore/
remote_src: yes
- name: install Xstore POS
win_shell: "CMD /C \"java -jar xstore-18.0.0.0.654-0.0.0-0.0-XST-pos-install.jar\""
args:
chdir: C:/Installers/Softwares/Xstore/
how is that possible ?, I need to execute that on windows hosts.
I guess you could create an inventory and a playbook to achieve that.
inventory
[all]
13.48.139.217
13.48.139.218
playbook.yml
- hosts: all
tasks:
- name: run java command
command: java -jar xxxxxxx.war {{ansible_host}}
{{ansible_host}} is an Asnsible variable that will be replaced with an IP address mentioned in inventory.
Then execute it ansible-playbook -i inventory playbook.yml.