Ansible win_file module register results - ansible

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.

Related

How can I manage groups in inventory?

I have a common playbook (task) but in my last task I want to do an action on a group of hosts and when it is finished, do the same action on another group (for my test I simply empty a folder):
- name: test clean folder on hosts
ansible.builtin.file:
path: /testJO
when: inventory_hostname in groups['c_hosts']
- name: test clean folder on master
ansible.builtin.file:
path: /testJO
when: inventory_hostname in groups['master’]
I have issue with my inventory file :
/root/ansible_Folder/inventories/inventories.yml :
all:
hosts:
children:
master:
hosts:
dem-master:
c_hosts:
hosts:
dem-host:
Who knows how I can manage my inventories file please?
EDIT :
I tried your solution which seems to me coherent but i have this error message :
ERROR! conflicting action statements: hosts, tasks
The error appears to be in '/root/ansible-cortex-lab/playbooks/roles/cortex/tasks/main.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- hosts: c_hosts
^ here
I just copy past both config file you gave to me.
Do you know why I have this behaviour please ?
Separate the plays, e.g.
- hosts: c_hosts
tasks:
- name: test remove folder
ansible.builtin.file:
path: /testJO
state: absent
- name: test create folder
ansible.builtin.file:
path: /testJO
state: directory
- hosts: master
tasks:
- name: test remove folder
ansible.builtin.file:
path: /testJO
state: absent
- name: test create folder
ansible.builtin.file:
path: /testJO
state: directory
The module file can't simply clean a folder. Instead, remove a folder and create it again.
To fix the inventory, remove the 2nd line hosts:. See Inventory basics: formats, hosts, and groups
all:
children:
master:
hosts:
dem-master:
c_hosts:
hosts:
dem-host:

Ansible playbook example code fail to run

I'm started to try Ansible, and using example code from Ansible Documentation. After I try several examples, I get error at the beginning of the code. It says
- name: Change the hostname to Windows_Ansible
^ here(Point at name)"
Any advice would be appreciate.
I tried this one
https://docs.ansible.com/ansible/latest/modules/win_hostname_module.html#win-hostname-module
---
- name: Change the hostname to Windows_Ansible
win_hostname:
name: "Windows_Ansible"
register: res
- name: Reboot
win_reboot:
when: res.reboot_required
The below task will change the hostname of the server. Make sure you run on a test server so that it wont create issues. If you just wanted to test some playbook, use the second playbook with win_command
---
- hosts: <remote server name which needs to be added in the inventory>
tasks:
- name: Change the hostname to Windows_Ansible
win_hostname:
name: "Windows_Ansible"
register: res
- name: Reboot
win_reboot:
when: res.reboot_required
---
- hosts: <remote server name which needs to be added in the inventory>
tasks:
- name: Test
win_command: whoami
register: res

roles overides tasks in playbook

I have ansible playbook which look similar to the code below :
---
- hosts: localhost
connection: local
tasks:
- name: "Create custom fact directory
file:
path: "/etc/ansible/facts.d"
state: "directory"
- name: "Insert custom fact file"
copy:
src: custom_fact.fact
dest: /etc/ansible/facts.d/custom_fact.fact
mode: 0755
roles:
- role1
- role2
once i am running the playbook with ansible-playbook command
only the roles is running ,but the tasks is not getting ran
if i am remarking the roles from the playbook,the task gets ran
how can i make the task to run before the roles ?
Put the tasks in a section pre_tasks which are run before roles.
You may also find post_tasks useful which run tasks after roles.
Correct the indentation
- hosts: localhost
connection: local
tasks:
- name: "Create custom fact directory
file:
path: ...

How to delete local directory with ansible

I have to make delete some directory and create them on local before copy to the remote. Is there anyway to delete and create locally?
Currently I'm using 'command'
command: rm -r directory
But warning shows as
Consider using file module with state=absent rather than running rm
Is there any options we can use for local folder changes?
You can use diffrent delegation methods or use the local_action:
- local_action: file path=directory state=absent
If you're running this in a playbook, you can use a section of the playbook that uses a local connection to make changes on the command machine, then copies files to the remote:
---
- hosts: 127.0.0.1
connection: local
tasks:
- name: Delete local directory
file: path=/directory state=absent
- hosts: myhosts
tasks:
copy: src=/directory dest=/foo/directory
Update:
Current Ansible (2.10) does not like - local_action: , instead use delegate_to:
- name: Remove directory 'dir1'
file:
path: "path/to/dir1"
state: absent
delegate_to: localhost

Ansible playbook for remote copy and script execution

I wish to write a playbook in ansible which will first transfer my package to remote hosts and then run a script. In detail, let's say I have apache package in local machine and need to scp/rsync it to remote nodes A & B. Then I have my script to install the package on A & B both, check whether it was installed properly followed by scrutinizing the config file etc. This script should run only if the transfer is successful.
Have written the below playbook which should meet above requirement. Please confirm if it needs further improvement. Thanks in advance !
Playbook:
---
- hosts: droplets
remote_user: root
tasks:
- name: Copy package to target machines
synchronize: src=/home/luckee/apache.rpm dest=/var/tmp/
- name: Run installation and verification script
script: /home/luckee/apache_install.sh
register: result
- name: Show result
debug: msg="{{ result.stdout }}"
...
This way the installation script will only run if the copy tasks changed (was execuded in the process) and exited successfully:
---
- hosts: droplets
remote_user: root
tasks:
- name: Copy package to target machines
synchronize: src=/home/luckee/apache.rpm dest=/var/tmp/
register: result_copy
- name: Run installation and verification script
script: /home/luckee/apache_install.sh
register: result_run
when: result_copy.changed
- name: Show result
debug: msg="{{ result_run.stdout }}"
...

Resources