Ansible - Best way to perform a local copy (in pre_tasks)? - ansible

I have a playbook that requires to copy some files from a local directory into another local directory (on the ansible host). What is the correct way to achieve this?
I found the copy module but it seems it is only for copying files to a remote host. I also found local_action but I'am very unsure how to use it.
The playbook looks something like:
---
- hosts: all
vars:
proprietary_files: "/some/files/not/managed/by/vcs"
filesToWorkOnLater: "config_files"
pre_tasks:
- name "Copy from {{proprietary_files}} to {{filesToWorkOnLater}}"
# What to enter here to perform the local copy?
roles:
...
...

Ansible now supports import_role so pre_tasks aren't needed anymore.
Something like this should fix you issue:
- hosts: all
tasks:
- command: cp file1 file2
delegate_to: 127.0.0.1
- import_role:
name: ...

Have you tried delegate_to localhost? PLease try as below. (I have not tested)
- name: copying to local
copy:
src: {{proprietary_files}}
dest: {{filesToWorkOnLater}}
delegate_to: localhost

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/

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 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.

last modified files only moved ansible?

my requirement is moving files to remote host using ansible playbook.
my ansible script
---
- hosts: webservers
remote_user: root
tasks:
- copy: src=/home/bu/Bilal/site dest=/tmp owner=root group=root mode=777
when run playbook it has moved the file to remote.
when I have ran playbook again it will overwrite the whole folder again. I am looking, what are the files I have modified that files only get overwrite because my folder size is too large its taking so much time even single file change.
Take a look at the Synchronize module:
Uses rsync to make synchronizing file paths in your playbooks quick and easy.
Example:
- name: Sync files
synchronize:
src: "{{ conf.dev_path }}/"
dest: "{{ conf.host_path }}"
delete: yes
rsync_opts:
- "--exclude=.*"

How to create a file locally with ansible templates on the development machine

I'm starting out with ansible and I'm looking for a way to create a boilerplate project on the server and on the local environment with ansible playbooks.
I want to use ansible templates locally to create some generic files.
But how would i take ansible to execute something locally?
I read something with local_action but i guess i did not get this right.
This is for the webbserver...but how do i take this and create some files locally?
- hosts: webservers
remote_user: someuser
- name: create some file
template: src=~/workspace/ansible_templates/somefile_template.j2 dest=/etc/somefile/apps-available/someproject.ini
You can delegate tasks with the param delegate_to to any host you like, for example:
- name: create some file
template: src=~/workspace/ansible_templates/somefile_template.j2 dest=/etc/somefile/apps-available/someproject.ini
delegate_to: localhost
See Playbook Delegation in the docs.
If your playbook should in general run locally and no external hosts are involved though, you can simply create a group which contains localhost and then run the playbook against this group. In your inventory:
[local]
localhost ansible_connection=local
and then in your playbook:
hosts: local
Ansible has a local_action directive to support these scenarios which avoids the localhost and/or ansible_connection workarounds and is covered in the Delegation docs.
To modify your original example to use local_action:
- name: create some file
local_action: template src=~/workspace/ansible_templates/somefile_template.j2 dest=/etc/somefile/apps-available/someproject.ini
which looks cleaner.
If you cannot do/allow localhost SSH, you can split the playbook on local actions and remote actions.
The connection: local says to not use SSH for a playbook, as shown here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#local-playbooks
Example:
# myplaybook.yml
- hosts: remote_machines
tasks:
- debug: msg="do stuff in the remote machines"
- hosts: 127.0.0.1
connection: local
tasks:
- debug: msg="ran in local ansible machine"
- hosts: remote_machines
tasks:
- debug: msg="do more stuff in remote machines"

Resources