ansible synchronize with excludes file role - laravel

Im creating an ansible role for deploying laravel5 project,
now I do that with a "synchronize" (rsync)
I have my excludes file for the rsync, and the files structured like this:
the: "deploy-laravel5" role:
files
excludes
tasks
main.yml
now here is the tasks in main.yml:
- name: deploy laravel projects
synchronize:
src: "{{item.src}}"
dest: "{{item.dest}}"
rsync_opts:
- "--exclude-from=excludes"
with_items: "{{projects}}"
some playbook:
---
- hosts: php
gather_facts: no
vars:
projects:
- {src: "../../twitter/", dest: "/web/boom/", envFile: "twitter.env"}
roles:
- deploy-laravel5
now when I run that, ansible says it can't find the "excludes" file
msg: rsync: failed to open exclude file excludes: No such file or directory (2)
I tried many different paths but nothing, any ideas how to point to the excludes file?

After I browsed the web and the docs quite extensively, I found that you can't define othe files in the "role" only files for templating or copying.
But you can define the file inside a playbook, so now the file structure is like this:
roles/
deploy/ (NOT "deploy-laravel5" as before)
playbooks/
deploy-laravel5/
excludes-file
deploy-playbook.yml
and the playbook looks quite the same:
---
- hosts: php
gather_facts: no
vars:
projects:
- {src: "../../twitter/",
dest: "/web/boom/",
excludes-file: "path/to/excludes/file",
envFile: "twitter.env"}
roles:
- deploy

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 can't run when vars are used in include_tasks

I have two playbooks driver.yaml invoking another play after converting from template called main_driver.yml
Main playbook driver.yml
---
- name: Setup Services
hosts: all
gather_facts: no
vars_files:
- var_input_driver.yaml
tasks:
- name: Converting template to playbook
template:
src: template_for_driver.yaml.j2
dest: main_driver.yaml
delegate_to: localhost
- name: run roles
include_tasks: main_driver.yaml
Content of main_driver.yaml
---
- name: Setup Services
vars:
java_package: java-11-openjdk
dock_version: 18.09.0
- name: Setup services
include_role:
name: configure-java
Receiving error as below:
FAILED! => {"reason": "no module/action detected in task.\n\nThe error appears to be in 'main_driver.yaml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: Setup Services\n ^ here\n"}
vars is not a module name but a task option. You should fix your included file like so:
---
- name: Setup services
include_role:
name: configure-java
vars:
java_package: java-11-openjdk
dock_version: 18.09.0
An other possible way would be:
---
- name: Set variables
set_fact:
java_package: java-11-openjdk
dock_version: 18.09.0
- name: Setup services
include_role:
name: configure-java
couple of things:
main_driver.yaml seems to include itself (main_driver) - that probably won't work.
the task file used with "include_tasks" should be a "pure" task file, not a playbook. In other words, it should not contain a playbook header (hosts, name, etc), but simply a list of tasks.
Im struggling to understand the logic in your example, maybe double-check that the file contents are correct?

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

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

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

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