I am writing a playbook to synchronize a number of files. If the files have changed then I need to back them up and overwrite them with the source files. Not sure how to do this. Right now It is not overwriting the the destination files.
- name: "Copies files over"
synchronize:
src: "{{ item.source }}"
dest: "{{ item.dest }}"
recursive: yes
notify:
restart freeswitch
with_items:
- { source: '/Users/User1/Documents/Ansible/files/sca/dialplan/', dest: '/etc/freeswitch' }
- { source: '/Users/User1/Documents/Ansible/files/sca/sip_profiles/', dest: '/etc/freeswitch' }
- { source: '/Users/User1/Documents/Ansible/files/sca/scripts/', dest: '/usr/share/freeswitch/scripts' }
are you doing it on local ansible server ?
because synchronize module only running on local ansible server. can not excute on remote server.
Related
I am trying to take a .zip file and copy it to a directory and extract the .zip file afterwards on a Windows target using an Ansible playbook. I was wondering if there's a module or task that can be written to do this nice and clean.
The .zip file itself is stored on another server so the playbook task would be pulling from this location to get the file to copy to the target.
I use these two modules to accomplish that task. win_copy: could be used if the file is on your ansible control host.
- name: download zlib source
win_get_url:
url: "{{ zlib_url }}"
dest: "{{ boost_workdir }}"
register: download_zlib
until: download_zlib is succeeded
retries: 10
delay: 2
- name: unzip zlib source
win_unzip:
src: "{{ download_zlib.dest }}"
dest: "{{ boost_prefix }}"
delete_archive: true
creates: "{{ boost_prefix }}/zlib-{{ zlib_version }}/contrib"
copy: false
I would like to copy few files on remote host , I used copy module as following but it copy all files under 'app' variable (dir) to 'backup_conf' variable (dir).
please advice here.
- name: backup configuration files
copy:
src: '{{ app }}'
dest: '{{ backup_conf }}'
remote_src: true
with_items:
- /bin/setenv.sh
- /conf/server.xml
Thank you, I fixed my issue:
- name: backup configuration files
copy:
src: "{{ item }}"
dest: "{{ backup_conf }}"
remote_src: true
with_items:
- /bin/setenv.sh
- /conf/server.xml
This is the ansible script. Its not copying all the files /etc/yum.respos.d to the other hosts.
- name: Sync System Files
hosts: all
tasks:
- name: Sync System Files
tags: sync-push
synchronize:
src: "{{ item }}"
dest: "/"
mode: push
recursive: yes
register: syncfile
with_items:
- "/etc/yum.repos.d"
I have some multiple war files which I am downloading from nexus and creating the respective app folder on my remote host and copying the files.
Now there are 2 files which have a different name as war file but need to create a directory with different name. What I am doing right now is this -
- name: Ensuring the web deployment folder is created on tomcat nodes for each web application
file:
path: "{{tomcat_server_release_location}}/Release{{my_release_version}}/{{item}}"
state: directory
mode: 0777
with_items:
- "{{ apps }}"
- name: Copying the release from Admin server to tomcat nodes
copy: src={{admin_server_release_location}}/{{my_release_version}}/{{item}}-{{my_release_version}}.war dest={{tomcat_server_release_location}}/Release{{my_release_version}}/{{item}}
with_items:
- "{{ apps }}"
apps variable is defined like this -
webapps: ['test1','test2','test3','test4'].
Now test2 has a different name on the nexus it says test2-web.war but on the remote node I have to create a folder called just test2 and copy the war file in there.
Is there a way to override some variables at run time of playbook by using some condition or anything
If you want to override at runtime use extra vars.
From the docs --extra-vars "version=1.23.45"
Don't use conditions if you know a specific host should always be overwritten. I think placing it as a host var is best.
hosts:
myhost:
apps:
- test1
- trst2
See https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable for all of the various locations you can put variables and how to determine which will be used
It is possible to override variables by doing something like this:
- name: copy war
copy:
src: "{{ src_war | default('/path/to/my/war') }}"
dest: "{{ dest_war | default(src_war) }}"
Then at runtime you can set the variables src_war and dest_war either with extra vars, host/group_vars or any other way documented here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
In your case I think it would be helpful to set a src & dest mapping:
- set_fact:
apps:
- {src: "test1"}
- {src: "test2", dest: "test2-web"}
- {src: "test3"}
- {src: "test4"}
Then you can use this mapping to achieve your goal:
- name: Ensuring the web deployment...
file:
path: {{ tomcat_server_release_location }}/Release{{ my_release_version }}/{{ item.src }}
state: directory
mode: 0777
with_items: "{{ apps }}"
- name: Copying the release...
copy:
src: "{{ admin_server_release_location }}/{{ my_release_version }}/{{ item.src }}-{{ my_release_version }}.war"
dest: "{{ tomcat_server_release_location }}/Release{{ my_release_version }}/{{ item.dest | default(item.src) }}"
with_items: "{{ apps }}"
I’m setting up a new setup for tomcat.
I wanna download and extract apache tomcat using ansible to several nodes of linux, but the thing is unzip file should be seen or rename as apache only. It doesn't matter what would be a version of tomcat?
- name: variable check
hosts: all
become: yes
vars:
tomcat_url: 'http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.45/bin/apache-tomcat-8.5.45.tar.gz'
tasks:
- name:
get_url:
url: "{{tomcat_url}}"
dest: /opt
Here's complete working example:
---
- hosts: all
become: yes
vars:
tomcat_url: 'http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.45/bin/apache-tomcat-8.5.45.tar.gz'
tasks:
- name: Download and Extract apache tomcat
unarchive:
src: "{{ tomcat_url }}"
dest: "/opt/"
remote_src: yes
- name: Synchronize the "{{ tomcat_url.split('/')[-1].rstrip('.tar.gz') }}" directory to apache on one remote host
synchronize:
src: "/opt/{{ tomcat_url.split('/')[-1].rstrip('.tar.gz') }}"
dest: "/opt/apache"
delegate_to: "{{ inventory_hostname }}"
- name: Remove the {{ tomcat_url.split('/')[-1].rstrip('.tar.gz') }} directory
file:
path: "/opt/{{ tomcat_url.split('/')[-1].rstrip('.tar.gz') }}"
state: absent