Synchronize directories on remote host - ansible

I want to make a copy of a directory on my remote host. The task I tried to use is:
- name: copy old core configs to new Solr
become: yes
become_user: root
synchronize:
src="/usr/local/solr/example/solr/collection1/"
dest="/usr/local/solr-4.10.4/example/solr/collection1"
recursive=yes
delegate_to: "{{ inventory_hostname }}"
But it seemed like it was hanging forever and looking in iotop it didn't seem like anything was being copied. What I expect this to do is SSH into the remote host, and do an rsync from one directory to the other. Am I missing something?

You are mixing YAML syntax styles by using = instead of :. In my experience this can cause misleading errors.
Try:
- name: copy old core configs to new Solr
become: yes
become_user: root
synchronize:
src: "/usr/local/solr/example/solr/collection1/"
dest: "/usr/local/solr-4.10.4/example/solr/collection1"
recursive: yes
delegate_to: "{{ inventory_hostname }}"

Related

how could I install a snap package via Ansible on an air-gapped system

Installing snap packages via Ansible on systems that are connected to internet is rather simple. EG:
- name: Install microk8s
become: yes
snap:
name: microk8s
classic: yes
channel: "{{ microk8s_version }}"
Now I would need to do the same on a set of nodes that are air-gapped (no direct connection to internet).
I can do a 'snap download' for the required packages, and move them to the target machine(s).
But then how to do this in Ansible? Is there any support for this? Or do I have to use the shell/command module ?
thx
I have not tested this, but this method works with other modules.
- name: install microk8s, file on local disk
become: yes
snap:
name: /path/to/file
using the hint of #Kevin C I was able to solve the problem using the following playbook
- name: copy microk8s snap to remote
copy:
src: "{{ item }}"
dest: "~/microk8s/"
remote_src: no
with_fileglob:
- "../files/microk8s/*"
- name: snap ack the new package
become: yes
shell: |
snap ack ~/microk8s/microk8s_1910.assert
snap ack ~/microk8s/core_10583.assert
- name: install microk8s, file on local disk
become: yes
snap:
name: "~/microk8s/core_10583.snap"
- name: install microk8s, file on local disk
become: yes
snap:
name: "~/microk8s/microk8s_1910.snap"
classic: yes
I hope this helps others also.
Would be nice to see this documented.
If you can get the packages onto the Ansible server, the following code will copy the file to the target(s). Something like the the following code should work.
- name: copy files/local/microk8s.deb
copy:
src: "files/local/microk8s.deb"
dest: "~/microk8s.deb"
remote_src: no
Where files/ is at the same level as the playbook.

Install rpm after copy, with ansible

I have an ansible playbook which will copy a file into a location on a remote server. It works fine. In this case, the file is an rpm. This is the way it works:
---
- hosts: my_host
tasks:
- name: mkdir /tmp/RPMS
file: path=/tmp/RPMS state=directory
- name: copy RPMs to /tmp/RPMS
copy:
src: "{{ item }}"
dest: /tmp/RPMS
mode: 0755
with_items:
[any_rpm-x86_64.rpm]
register: rpms_copied
Now, with the file successfully on the remote server, I need to start some new logic that will install the rpm that sits in /tmp/RPMS. I have run many different versions of the below (So this code is added onto the above block):
- name: install rpm from file
yum:
name: /tmp/RPMS/any_rpm-x86_64.rpm
state: present
become: true
I don't know if the formatting is incorrect, or if this is not the way. Can anyone advise as to how I can get the rpm in the directory /tmp/RPMS installed using a new few lines in the existing playbook?
Thanks.
I did not find this anywhere else, and it genuinely took me all of my working day to get to this point. For anyone else struggling:
- name: Install my package from a file on server
shell: rpm -ivh /tmp/RPMS/*.rpm
async: 1800
poll: 0
become_method: sudo
become: yes
become_user: root

how to copy files and folders one folder to another on remote machine by using ansible-playbook?

i am new to ansible. i am trying to copy the files recursively ,got an error.
it does not support recursive.
host.ini
[web]
server1
server2
server3
ansible-playbook1:
tasks:
- name: copy files on remote machine
copy:
src: /home/anil/anilmainfolder
dest: /home/anil/anilbackup
remote_src: yes
i tried like this.but got error recursive problem.
tasks:
- name: copy files on remote machine
synchronize:
src: /home/anil/anilmainfolder
dest: /home/anil/anilbackup
deligate_to: {{ ansible_hostname }}
i also tried like this . but it copied folders and files on one of the server.
help me how to solve this plroblem/

Ansible Failed to set permissions on the temporary

I am using ansible to replace the ssh keys for a user on multiple RHEL6 & RHEL7 servers. The task I am running is:
- name: private key
copy:
src: /Users/me/Documents/keys/id_rsa
dest: ~/.ssh/
owner: unpriv
group: unpriv
mode: 0600
backup: yes
Two of the hosts that I'm trying to update are giving the following error:
fatal: [host1]: FAILED! => {"failed": true, "msg": "Failed to set
permissions on the temporary files Ansible needs to create when
becoming an unprivileged user (rc: 1, err: chown: changing ownership
of /tmp/ansible-tmp-19/': Operation not permitted\nchown: changing
ownership of/tmp/ansible-tmp-19/stat.py': Operation not
permitted\n). For information on working around this, see
https://docs.ansible.com/ansible/become.html#becoming-an-unprivileged-user"}
The thing is that these two that are getting the errors are clones of some that are updating just fine. I've compared the sudoers and sshd settings, as well as permissions and mount options on the /tmp directory. They are all the same between the problem hosts and the working ones. Any ideas on what I could check next?
I am running ansible 2.3.1.0 on Mac OS Sierra, if that helps.
Update:
#techraf
I have no idea why this worked on all hosts except for two. Here is the original playbook:
- name: ssh_keys
hosts: my_hosts
remote_user: my_user
tasks:
- include: ./roles/common/tasks/keys.yml
become: yes
become_method: sudo
and original keys.yml:
- name: public key
copy:
src: /Users/me/Documents/keys/id_rsab
dest: ~/.ssh/
owner: unpriv
group: unpriv
mode: 060
backup: yes
I changed the playbook to:
- name: ssh_keys
hosts: my_hosts
remote_user: my_user
tasks:
- include: ./roles/common/tasks/keys.yml
become: yes
become_method: sudo
become_user: root
And keys.yml to:
- name: public key
copy:
src: /Users/me/Documents/keys/id_rsab
dest: /home/unpriv/.ssh/
owner: unpriv
group: unpriv
mode: 0600
backup: yes
And it worked across all hosts.
Try to install ACL on remote host, after that execute ansible script
sudo apt-get install acl
You could try something like this:
- name: private key
become: true
become_user: root
copy:
src: /Users/me/Documents/keys/id_rsa
dest: ~/.ssh/
owner: unpriv
group: unpriv
mode: 0600
backup: yes
Notice the:
become: true
become_user: root
Check the "become" docs for more info
While installing the acl module works there is an alternative.
Add the line below to the defaults section of your ansible.cfg.
allow_world_readable_tmpfiles = True
Of better, just add it to the task that needs it with:
vars:
allow_world_readable_tmpfiles: true
A similar question with more details is Becoming non root user in ansible fails
I'm using ad-hoc and when I got into this problem, adding -b --become-user ANSIBLE_USER to my command fixes my problem.
example:
ansible all -m file -a "path=/etc/s.text state=touch" -b --become-user ansadmin
Of course, before this, I had given Sudo access to the user
If you give Sudo access to your user, you can write like this :
ansible all -m file -a "path=/var/s.text state=touch" -b --become-user root

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=.*"

Resources