I am new to Ansible and trying to run a playbook from my Ansible Controller (Linux box ) against Windows 10 (target machine). The playbook copies a PowerShell script (sample.ps1) from the controller to the target and runs it afterward. The PS script tries to modify an INI file in the target machine. The PS script runs fine if I RDP the target machine and run it from the Powershell window. But when I run it through Ansible Playbook, it does show that the file is changed but actually nothing changed. Here are the scripts and playbook.
Ansible Playbook: sample.yml
'''
name: "Ansible"
tasks:
- name: Copy Powershell script
win_copy:
src: /root/templates/sample.ps1
dest: "C:\\Temp\\"
force: yes
- name: Ensure the file is present on Remote Host
win_stat:
path: "C:\\Temp\\sample.ps1"
become: yes
become_method: runas
become_user: "{{host_name}}\\<<userId>>"
vars:
ansible_become_password: "<<password>>"
ansible_become_user: <<user>>
ansible_become: yes
- name: Run Powershell script
win_command: powershell.exe -
args:
stdin: '.\sample.ps1 -iniFile c:\temp\sample.ini'
chdir: "C:\\Temp\\"
become_method: runas
become_flags: logon_type=new_credentials logon_flags=netcredentials_only
become_user: "{{host_name}}\\<<userId>>"
vars:
ansible_become_password: <<password>>
ansible_become_user: <<user>>
ansible_become: yes
'''
Powershell script: sample.ps1
param ($iniFile)
$prop = Get-Content ($iniFile)
$prop = $prop -replace "xyz", "abc"
Set-Content -Path $iniFile -Value $prop
Run playbook like this:
ansible-playbook sample.yml --extra-vars "host_name=<<machineName>>" -vvvv
And here is the playbook output:
When I RDP the machine and see the content of the sample.ini file, it is unchanged.
Your help is appreciated.
Related
I am trying to use hostname -f command in variable with ansible-playbook. After I set the variable, I will use it in sed command. When manually execute the commands it works but with Ansible, variable does not work.
When I echo $hostn output is empty.
---
- hosts: test
become: true
become_user: root
tasks:
- name: test
shell: "{{ item }}"
with_items:
- hostn=`hostname -f` <<<<<< not working
- echo $hostn <<<<<< not working
- sed -i "s/test/$hostn/g" /file <<<< manually works
Can you help me?
My advise would be that you should not try to fit all your commands in Ansible shell this way, but rather translate them into the corresponding Ansible modules.
What you are looking to achieve can be done with the replace module — in place of sed — and the Ansible fact ansible_hostname — in place of hostname -f.
This would be the equivalent playbook:
- hosts: test
become: true
become_user: root
tasks:
- replace:
path: /file
regexp: test
replace: "{{ ansible_hostname }}"
So i'm running an ansible playbook, which creates a server (using terraform) and gives saves the ip-address of the server into a variable. i'd like to execute another task on the given ip-address. How do i declare the new host?
I've tried:
- hosts: "{{ remotehost }}"
tasks:
- name: test
lineinfile:
path: /etc/environment
line: test1234
I run the playbook with: ansible-playbook variable.yaml --extra-vars='playbook=ip-address'
If you just want to execute a single task you can use delegate_to
For example:
tasks:
- name: another host execute
command: ls -ltr
delegate_to: "{{ remotehost }}"
The server should have the ssh connection working with the new hosts
I am new to ansible script, I am running ansible script from root user inside the ansible playbook I want to execute a script in another user(user12). Below is my ansibe playbook
---
- name: agent installation Script
hosts: <hostname>
gather_facts: False
#Disabling gathering facts because playbook not getting executed on server
tasks:
- name: Copy the creating script to Managed node
copy:
src: Createuser.sh
dest: ~/
mode: 0777
become: true
become_user: root
- name: Copy the agent zip to Managed node
copy:
src:13.2.0.0.0.zip
dest: ~/
mode: 0777
become: true
become_user: root
- name: Copy the agent response file to Managed node
copy:
src: agent.rsp
dest: ~/
mode: 0777
become: true
become_user: root
- name: Execute the script
shell: sh ~/Createuser.sh
become: true
become_user: root
- name: Execute the installation script
shell: sh ~/Agentinstallation.sh
become: true
become_user: user12
The Agentinstallation.sh needs to be run by user12. For user12 password is user12 how to pass this password to execute the above script.
With option -k, this will ask for a connection password.
From the docs:
-k, --ask-pass
ask for connection password
-K, --ask-become-pass
ask for privilege escalation password
Note the difference..
How to turn off IE enhanced security using ansible
Is there any way I can configure user account in Windows server by using ansible YAML
You can do this with powershell:
myscript.ps1:
$AdminKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}"
$UserKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}"
Set-ItemProperty -Path $AdminKey -Name "IsInstalled" -Value 0
Set-ItemProperty -Path $UserKey -Name "IsInstalled" -Value 0
Stop-Process -Name Explorer
Ansible playbook:
- name: example copying file with owner and permissions
copy:
src: myscript.ps1
dest: myscript.ps1
- name: Run ps1 script in privileged mode
hosts: "{{ my_hosts }}"
become_method: runas
vars:
ansible_become_password: passwordhere
tasks:
- win_shell: '.\myscript.ps1'
become: yes
become_user: Administrator
- name: Create new user
win_user:
name: someuser
password: somepassword
state: present
groups:
- Users
Host is Ubuntu 16.04
I'm trying to set environment variable for user, with:
- hosts: all
remote_user: user1
tasks:
- name: Adding the path in the bashrc files
lineinfile: dest=/home/user1/.bashrc line='export MY_VAR=TEST' insertafter='EOF' state=present
- name: Source the bashrc file
shell: . /home/user1/.bashrc
- debug: msg={{lookup('env','MY_VAR')}}
Unfortunately it outputs:
TASK [debug] *******************************************************************
ok: [xxxxx.xxx] => {
"msg": ""
}
How can I export variable so next time I run some tasks on this machine I can use {{ lookup('env', 'MY_VAR') }} to get value of this variable?
Because lookups happen locally, and because each task runs in it's own process, you need to do something a bit different.
- hosts: all
remote_user: user1
tasks:
- name: Adding the path in the bashrc files
lineinfile: dest=/home/user1/.bashrc line='export MY_VAR=TEST' insertafter='EOF' state=present
- shell: . /home/user1/.bashrc && echo $MY_VAR
args:
executable: /bin/bash
register: myvar
- debug: var=myvar.stdout
In this example I am sourcing the .bashrc and checking the var in the same command, and storing the value with register
All lookups in Ansible are local. See documentation for details:
Note
Lookups occur on the local computer, not on the remote computer.