roles overides tasks in playbook - ansible

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

Related

How to run tasks file as a playbook and as an include_file

I have a generic tasks, like update a DNS records based on my machine -> server
Which I use it in my playbooks with include_tasks like so:
- name: (include) Update DNS
include_tasks: task_update_dns.yml
I have many of these "generic tasks" simply acting as a "shared task" in many playbooks.
But in some cases, I just want to run one of these generic tasks on a server, but running the following gives the below error:
ansible-playbook playbooks/task_update_dns.yml --limit $myserver
# ERROR
ERROR! 'set_fact' is not a valid attribute for a Play
Simply because it's not a "playbook" they are just "tasks"
File: playbooks/task_update_dns.yml
---
- name: dig mydomain.com
set_fact:
my_hosts: '{{ lookup("dig", "mydomain.com").split(",") }}'
tags: always
- name: Set entries
blockinfile:
....
I know I can write a playbook, empty, that only "include" the task file, but I don't want to create a shallow playbook now for each task.
Is there a way to configure the task file in such way that I'll be able to run it for both include_tasks and as a "stand alone"/play command line ?
Have you tried to use custom tags for those tasks?
Create a playbook with all the tasks
---
- name: Update web servers
hosts: webservers
remote_user: root
tasks:
- name: dig mydomain.com
set_fact:
my_hosts: '{{ lookup("dig", "mydomain.com").split(",") }}'
tags: always
- name: Set entries
blockinfile:
tags:
- set_entries
- name: (include) Update DNS
include_tasks: task_update_dns_2.yml
tags:
- dns
...
when you need to run only that specific task, you just need to add the --tag parameter in the execution with the task name
ansible-playbook playbooks/task_update_dns.yml --limit $myserver --tag set_entries

Second ansible role not running in playbook

I am experimenting with using roles in a playbook. My simple playbook is as follows:
---
- name: Simple playbook
hosts: all
tasks:
- name: Role1
include_role:
name: role1
vars:
debugmode : true
- name: Role2
include_role:
name: role2
vars:
debugmode : true
I run the playbook with -vvv and I see all of my role1 tasks run. However, role2 tasks don't run, I just see this output (excerpt):
TASK [Role2] *********************************************************************************************************************************************************************************************************************************************************
task path: /myplaybook.yml:10
META: ran handlers
META: ran handlers
my my role2/tasks/main.yml file is:
- debug:
msg: "In create_vms role"
Although the cause of the problem is rather silly, I suspect this may help someone else.
The problem was that my file was accidentally named
role2/tasks/mail.yml
(not main.yml).
So ansible had no problem with this, it just ignored the file and did nothing. Seems like ansible should raise some kind of warning if no files are found in a role, so check for a typo :)

Ansible roles and tags

I have some issues tagging tasks in roles.
I need to put some sql in sync mode, update some for patching with other hosts, and after etc
When i run my playbook i pass the tags different ways. I tried all sort of ways
Ans in the roles tasks i tag the tasks in the files with the tags i want to associate
When i use a main.yml, it tried to run all the tasks in the main regardless of the tagging, for the first host on the first line
When i dont use a main.yml or its empty the hosts tagging works but it does not run the playbook, only gather facts
here is my playbook:
---
- name: put sql in sync mode
hosts: v1sql02d
roles:
- role: winupdates
tags: [ sync ]
- name: update sql not on primary
hosts: v3sqlax01d, v3sql01d, v1sql03d
roles:
- role: winupdates
tags: [ updates ]
here is my main.yml
- name: run sql ag sync tasks
include: winagsync.yml tags=sync
- name: update sql on secondary
include: winupdates.yml tags=updates
here is my first task in tasks
win_shell: |
Set-ExecutionPolicy RemoteSigned
Powershell lines
tags:
- sync
and my winupdate one
win_updates:
state: searched
server_selection: default
register: update_count
debug: var=update_count
log_path: c:\ansible_win_updates.log
tags:
- updates
- name: Install Updates, Security and Critical
win_updates:
state: installed
server_selection: default
register: update_count
debug: var=update_count
log_path: c:\ansible_win_updates.log
reboot: yes
tags:
- updates
Please enlight me or help me
Thanks
Theoneakta

How to run a task only once during entire Ansible Playbook?

I have a Ansible playbook which does multiple things as below -
Download artifacts fron nexus into local server (Ansible Master).
Copy those artifacts onto multiple remote machines let's say server1/2/3 etc..
And I have used roles in my playbook and the role (repodownload) which downloads the artifacts I want to run it only once because why would i want to download the same thing again. I have tried to use run_once: true but i guess that won't work because that only works for one playbook run but my playbook is running multiple times for multiple hosts.
---
- name: Deploy my Application to tomcat nodes
hosts: '{{ target_env }}'
serial: 1
roles:
- role: repodownload
tags:
- repodownload
- role: copyrepo
tags:
- copyrepo
- role: stoptomcat
tags:
- stoptomcat
- role: deploy
tags:
- deploy
Here target_env is being passed from the command line and it's the remote host group.
Any help is appreciated.
Below is the code from main.yml from repodownload role -
- connection: local
name: Downloading files from Nexus to local server
get_url: url="{{ nexus_url }}/{{item}}/{{ myvm_release_version }}/{{item}}-{{ release_ver }}.war" dest={{ local_server_location }}
with_items:
- "{{ temps }}"
This is a really simple one that I battled with too.
Try this:
- connection: local
name: Downloading files from Nexus to local server
get_url:
url: "{{ nexus_url }}/{{item}}/{{ myvm_release_version }}/{{item}}-{{ release_ver }}.war"
dest: "{{ local_server_location }}"
with_items:
- "{{ temps }}"
run_once: true
Just something else, unrelated to your main question;
When you run a module that has really long args, like in your example above, rather break the params into their own lines nested under the module. It makes for easier reading, and it makes it easier to spot any potential typos or syntax errors early.
Okay extending from your converstation with Zeitounator. The following workaround will work without changing your vars files. Just remember that this is a workaround, might not be the most efficient way to do the job.
---
- name: Download my repo to localhost
# Executes only for first host in target_env and has visibility to group vars of target_env
hosts: '{{ target_env }}[0]'
serial: 1
roles:
- role: repodownload
tags:
- repodownload
- name: Deploy my Application to tomcat nodes
# Executes for all hosts in target_env
hosts: '{{ target_env }}'
serial: 1
roles:
- role: copyrepo
tags:
- copyrepo
- role: stoptomcat
tags:
- stoptomcat
- role: deploy
tags:
- deploy

ansible synchronize with excludes file role

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

Resources