Why Ansible skips hosts group and does nothing - apache-spark

trying to run my Ansible and have a problem.
here is my hosts file:
[dse]
node1 192.168.56.10
node2 192.168.56.20
node3 192.168.56.30
[opscenter]
opscenter 192.168.56.40
[sparkmasters]
node3 192.168.56.30
[sparkworkers]
node1 192.168.56.10
node2 192.168.56.20
node3 192.168.56.30
Here is a part of my yml:
---
- hosts: [ "node1", "node2", "node3", "node4" ]
user: vagrant
sudo: True
vars:
username: spark
tasks:
- include: some tasks
roles:
- some roles
- hosts: sparkmasters
vars:
spark.version: 1.2
roles:
- spark_master
- hosts: sparkworkers
vars:
spark.version: 1.2
roles:
- spark_workers
I see that [ "node1", "node2", "node3", "node4" ] runs, but hosts: sparkmasters and hosts: sparkworkers are skipped with the message:
PLAY [sparkmasters]
*********************************************************** skipping: no hosts matched
PLAY [sparkworkers]
*********************************************************** skipping: no hosts matched
PLAY RECAP
******************************************************************** node1 : ok=21 changed=12 unreachable=0
failed=0
==> node1: Running provisioner: ansible...
Don't have any idea why. If i change group name to array of hosts, then it start to work...
What do I do wrong?
So I've added debug:
- name: Display hostvars
debug: var=hostvars
And see this:
"hostvars": {
"node1": {
"ansible_all_ipv4_addresses": [
"10.0.2.15",
"192.168.56.10"
],
and this huge json has much more metadata related to my host
I didn't get idea, what should I see there? I didn't find any info related to current host group info

It's ansible+vagrant specialities: https://serverfault.com/questions/585722/ansible-not-executing-host-specific-playbook-in-vagrant-multi-machine-provisioni
So I've added gropus declaration to Vagrant file and it started to work:
config.vm.provision "ansible" do |ansible|
ansible.playbook = "deploy.yml"
ansible.groups = {
"dse" => ["dsenode01","dsenode02","dsenode03"],
"opscenter" => ["dsenode03"],
"sparkmasters" => ["dsenode01"],
"sparkworkers" => ["dsenode01","dsenode02","dsenode03"]
}
end

Related

Conditionals against variables

I am building a snapshot management playbook and that checks for an existing snap before taking a snap. I am having trouble testing a conditional when the playbook finds an existing snap. Here is a sample code:
---
- name: Test Snapshot
hosts: axwayT
gather_facts: false
vars_files:
- vault/creds.yml
vars:
mail_body_file: "/tmp/ansible_mail"
pre_tasks:
- name: Delete mail body file
file:
state: absent
path: "{{ mail_body_file }}"
delegate_to: localhost
run_once: true
- name: Create mail body file
file:
state: touch
path: "{{ mail_body_file }}"
delegate_to: localhost
run_once: true
tasks:
- name: find guest's folder using name
vmware_guest_find:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
name: "{{ vcenter_hostgroup_name }}"
datacenter: "{{ datacenter_name }}"
register: vm_folder
delegate_to: localhost
- name: Check for existing snapshots
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
name: "{{ vcenter_hostgroup_name }}"
datacenter: "{{ datacenter_name }}"
folder: "{{vm_folder.folders[0]}}"
name: "{{ vcenter_hostgroup_name }}"
state: present
register: vm_state
delegate_to: localhost
- name: debug
debug:
var: vm_state.instance.current_snapshot
- name: Test
shell: echo "Found!"
when: vm_state.instance.current_snapshot !=""
I am working with an inventory of two servers for testing, nothing special. Here is the ouput I want to use a conditional against:
TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [swipe901.test.com] => {
"vm_state.instance.current_snapshot": ""
}
ok: [swipe902.test.com] => {
"vm_state.instance.current_snapshot": {
"creation_time": "2023-02-07T21:14:06.812901+00:00",
"description": "",
"id": 13,
"name": "VM Snapshot 2%2f7%2f2023, 3:14:05 PM",
"state": "poweredOn"
}
}
I tried two when statements:
when: vm_state.instance.current_snapshot =""
when: vm_state.instance.current_snapshot is defined
My logic is way off and I am seeing my own limitations with programing logic, which I plan to fix soon.
My plan for the logic is to, skip a step, in my playbook if vm_state.instance.current_snapshot is "". How would this be handled?
Q: "I would like the condition to evaluate false if there is anything populated in current_snapshot."
A: For example, the playbook
shell> cat pb.yml
- name: Evaluate false if there is anything populated in current_snapshot
hosts: localhost
tasks:
- debug:
msg: The variable current_snapshot is either empty or does not exist.
when: current_snapshot|d('', true)|length == 0
gives
shell> ansible-playbook pb.yml
PLAY [Evaluate false if there is anything populated in current_snapshot] *************************************************************
TASK [debug] *************************************************************************************************************************
ok: [localhost] =>
msg: The variable current_snapshot is either empty or does not exist.
PLAY RECAP ***************************************************************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
shell> ansible-playbook pb.yml -e current_snapshot=''
PLAY [Evaluate false if there is anything populated in current_snapshot] *************************************************************
TASK [debug] *************************************************************************************************************************
ok: [localhost] =>
msg: The variable current_snapshot is either empty or does not exist.
PLAY RECAP ***************************************************************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
shell> ansible-playbook pb.yml -e current_snapshot='anything'
PLAY [Evaluate false if there is anything populated in current_snapshot] *************************************************************
TASK [debug] *************************************************************************************************************************
skipping: [localhost]
PLAY RECAP ***************************************************************************************************************************
localhost: ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
You can also use assert. For example,
shell> cat pb.yml
- name: Evaluate false if there is anything populated in current_snapshot
hosts: localhost
tasks:
- assert:
that: current_snapshot|d('', true)|length == 0
success_msg: The variable current_snapshot is either empty or does not exist.
fail_msg: The variable current_snapshot is populated.

filtering the ansible playbook output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have output of an ansible playbook for which i need to apply fileters in such a way to proceed with next tasks. Please find the below output.
ok: [localhost] => {
"results": [
{
"actions": {
"namespaces": {},
"state": "present",
"xpath": "/Storage/SSL/KeyStorePath"
},
"ansible_loop_var": "item",
"changed": false,
"pod": 1,
"failed": false,
"invocation": {
"module_args": {
"mount": true,
"input_type": "yaml",
}
},
"item": "100.108.22.102",
"msg": "found 1 nodes"
},
{
"actions": {
"namespaces": {},
"state": "present",
"xpath": "/Storage/SSL/KeyStorePath"
},
"ansible_loop_var": "item",
"changed": false,
"pod": 0,
"failed": false,
"invocation": {
"module_args": {
"mount": true,
"input_type": "yaml",
}
},
"item": "100.108.22.103",
"msg": "found 0 nodes"
}
]
}
Here, i want the next task to be executed when the node respective pod value is 1 if the pod value of the node is 0 then the next task should not run on the respective node ip.
Please assist ..
Q: "Execute next task when the node's respective pod value is 1"
A: There are many ways how to proceed with the next task. For example, given the variable results
Use delegate_to and loop selected items
- hosts: localhost
tasks:
- debug:
msg: 'Task is running on {{ item.item }}'
loop: "{{ results|selectattr('pod', 'eq', 1)|list }}"
loop_control:
label: "{{ item.item }}"
delegate_to: "{{ item.item }}"
gives (abridged)
TASK [debug] ****
ok: [localhost -> 100.108.22.102] => (item=100.108.22.102) =>
msg: Task is running on 100.108.22.102
Create a list of selected hosts and test the host is in the list
- hosts: 100.108.22.102,100.108.22.103
tasks:
- debug:
msg: "Task is running on {{ inventory_hostname }}"
when: inventory_hostname in pods
vars:
pods: "{{ results|
selectattr('pod', 'eq', 1)|
map(attribute='item')|
list }}"
gives (abridged)
TASK [debug] ****
ok: [100.108.22.102] =>
msg: Task is running on 100.108.22.102
skipping: [100.108.22.103]
Use add_host to create an inventory group in the first play and use it in the second one
- hosts: localhost
tasks:
- add_host:
name: '{{ item }}'
groups: pods_enabled
loop: "{{ results|
selectattr('pod', 'eq', 1)|
map(attribute='item')|
list }}"
- hosts: pods_enabled
tasks:
- debug:
msg: "Task is running on {{ inventory_hostname }}"
gives (abridged)
PLAY [localhost] ****
TASK [add_host] ****
changed: [localhost] => (item=100.108.22.102)
PLAY [pods_enabled] ****
TASK [debug] ****
ok: [100.108.22.102] =>
msg: Task is running on 100.108.22.102
PLAY RECAP ****
100.108.22.102: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
You could use with_items and when in your next task as below
- name: validate pod value
debug:
msg: "The pod value is 1"
with_items: "{{ your_var.results }}"
when: item.pod == "1"

Ansible to create users and assign them to a particular group based on vars

Gurus, I'm learning ansible and trying to understand how the variables can be used, i have come across writing below playbook but i'm not understanding How to assign a particular group to particular users based on the Variables i have defined in the play for both Users and Groups under vars
I have below play where i want to create Users anika and rigved and want to assign them to a docker group while ayush and eshant should have the test group assigned.
I'm not getting the idea to achieve this so far. However as of now while running it creates users and assigns both group to all users.
$ cat nested_playbook-1.yml
---
- name: testing nested play
hosts: localhost
vars:
users:
- anika
- rigved
- ayush
- eshant
grps:
- docker
- test
tasks:
- name: make users members of groups
user:
name: "{{ item[0] }}"
state: present
groups: "{{ item[0] }}"
with_nested:
- "{{ users }}"
- "{{ grps }}"
The way you've structured your data doesn't show any relationship between users and groups. One option would be to structure it like this:
grps:
- name: docker
users:
- anika
- rigved
- name: test
users:
- ayush
- eshant
With this structure, you can loop over grps using the subelements filter, like this:
---
- name: testing nested play
gather_facts: false
hosts: localhost
vars:
grps:
- name: docker
users:
- anika
- rigved
- name: test
users:
- ayush
- eshant
tasks:
- debug:
msg:
user:
name: "{{ item.1 }}"
state: present
groups: "{{ item.0.name }}"
loop: "{{ grps|subelements('users') }}"
loop_control:
label: "{{ item.1 }}"
The subelements filter is a way of creating a "nested loop": it iterates over each member of the 'users' key for each group in grps. During each loop iteration, item is a 2-tuple in which the first item is the corresponding element from grps and the second item iterates over the users key for that element. In other words, you end up iterating over this list:
[{'name': 'docker', 'users': ['anika', 'rigved']}, anika}
[{'name': 'docker', 'users': ['anika', 'rigved']}, rigved}
[{'name': 'test', 'users': ['ayush', 'eshant']}, ayush}
[{'name': 'test', 'users': ['ayush', 'eshant']}, eshant}
So within the loop, item.0 refers to the group entry itself (and thus item.0.name is the group name), and item.1 refers to the user.
PLAY [testing nested play] *******************************************************************
TASK [debug] *********************************************************************************
ok: [localhost] => (item=anika) => {
"msg": {
"user": {
"groups": "docker",
"name": "anika",
"state": "present"
}
}
}
ok: [localhost] => (item=rigved) => {
"msg": {
"user": {
"groups": "docker",
"name": "rigved",
"state": "present"
}
}
}
ok: [localhost] => (item=ayush) => {
"msg": {
"user": {
"groups": "test",
"name": "ayush",
"state": "present"
}
}
}
ok: [localhost] => (item=eshant) => {
"msg": {
"user": {
"groups": "test",
"name": "eshant",
"state": "present"
}
}
}
PLAY RECAP ***********************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Alternately, you could structure your data like this:
users:
- name: anika
group: docker
- name: rigved
group: docker
- name: ayush
group: docker
- name: eshant
group: docker
You can just use a simple loop to process this structure:
---
- name: testing nested play
gather_facts: false
hosts: localhost
vars:
users:
- name: anika
group: docker
- name: rigved
group: docker
- name: ayush
group: docker
- name: eshant
group: docker
tasks:
- debug:
msg:
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.group }}"
loop: "{{ users }}"
loop_control:
label: "{{ item.name }}"
Note that in both the above examples I'm using loop_control on the tasks just to set the loop label and make the output look nicer. You could remove loop_control from the above tasks with no impact on how they operate.

ansible-playbook extra vars passing from command line

My playbook looks like:
---
- name: Install and configure AD authentication
hosts: test
become: yes
become_user: root
vars:
hostname: "{{ host_name }}"
vars_prompt:
- name: "bind_password"
prompt: "Password for xxx.local\\user"
private: yes
tasks:
- name: Ansible prompt example.
debug:
msg: "{{ bind_password }}"
- name: Ansible prompt example.
debug:
msg: "{{ host_name }}"
and i am using below command to pass the variable
ansible-playbook hostname_set.yml --extra-vars "host_name= 'xxx.xxx.local'"
but i am not getting exact variable value what i am using for setting up hostname.
Password for xxx.xxx\user:
PLAY [Install and configure AD authentication]
TASK [Gathering Facts]
ok: [x.x.x.x]
TASK [Ansible prompt example.]
ok: [x.x.x.x] => {
"msg": "wel"
}
TASK [Ansible prompt example.]
ok: [x.x.x.x] => {
"msg": ""
}
TASK [Setup the hostname]
changed: [x.x.x.x]
PLAY RECAP
x.x.x.x : ok=4 changed=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
You have an extra space in your command line that ruins how ansible is interpreting the extra vars. Just remove it:
--extra-vars "host_name='xxx.xxx.local'"
Note that you don't even need all those quotes. The following should also work as expected:
--extra-vars host_name=xxx.xxx.local

How to skip all other plays in ansible playbook if some condition is not met?

I have multiple plays in below playbook. I want to ignore all other plays if some condition is not met.
So for below example - If I cannot find any new file in Play1 then I don't want to execute Play2 and Play3 at all (it should skip it). How can I do that?
I have end_play in Play1 but it only skips Play1 and it still executes Play2 and Play3
---
- name: Play 1
hosts: 127.0.0.1
tasks:
- name: find the latest file
find: paths=/var/lib/jenkins/jobs/process/workspace/files
file_type=file
age=-1m
age_stamp=mtime
register: files
- meta: end_play
when: files.files|count == 0
- name: Copy file, if found
copy:
src: "some stuff here"
dest: "some other stuff here"
when: files.files|count > 0
- name: Play 2
hosts: all
serial: 5
tasks:
- name: copy latest file
copy: src=data_init/goldy.init.qa dest=/data01/admin/files/goldy.init.qa owner=golden group=golden
- name: copy latest file
copy: src=data_init/goldy.init.qa dest=/data02/admin/files/goldy.init.qa owner=golden group=golden
- name: Play 3
hosts: 127.0.0.1
tasks:
- name: execute command
shell: ./data_init --init_file ./goldy.init.qa
args:
chdir: /var/lib/jenkins/jobs/process/workspace/data_init/
Update:
So block module solution doesn't work looks like because you cannot use nested play in block like that..
There are two ways to approach your issue:
Although it is understandable that you tried defining a hosts: localhost to run some tasks on the controller, you don't really need this. Using delegate_to: localhost and run_once: true on the steps you want to run on your controller is a far better approach.
If you really want to go this way (using hosts: localhost), for some reason, then you'll need to save the return of your find, with set_fact in order to reuse it in other hosts with the help of the global hostvars
Using delegation
Any task in the play can be delegated to another host. This is quite simple to do and would just use one extra delegate_to options on your task:
- name: Delegate a find to localhost
find:
path: /test
file_type: any
register: localhost_find
delegate_to: localhost
run_once: true
When doing so, I would advice you to also use run_once: true, because, if you delegate the task to another host, there is no need to, let's say run it ten times if you have ten hosts in your host group.
Here is a little example about that
---
- hosts: hosts
become: true
gather_facts: false
tasks:
- name: Delegate directory creation to localhost
file:
path: /test
state: directory
delegate_to: localhost
run_once: true
- name: Create directory on hosts
file:
path: /test
state: directory
- name: Delegate file creation to localhost
file:
path: /test/localhost.txt
state: touch
delegate_to: localhost
run_once: true
- name: Create file on hosts
file:
path: /test/host.txt
state: touch
- name: Delegate a find to localhost
find:
path: /test
file_type: any
register: localhost_find
delegate_to: localhost
run_once: true
- name: Find in the hosts for comparison
find:
path: /test
file_type: any
register: host_find
- name: List /test of localhost
debug:
msg: "{{ localhost_find.files | map(attribute='path') | list }}"
- name: List /test of host
debug:
msg: "{{ host_find.files | map(attribute='path') | list }}"
- name: Remove /test folder on localhost
file:
path: /test
state: absent
delegate_to: localhost
run_once: true
- name: Delegate an empty find to localhost
find:
path: /test
file_type: any
register: empty_find
delegate_to: localhost
run_once: true
- name: Here are our hostnames from the inventory
debug:
msg: "{{ inventory_hostname }}"
- name: I am the evil host killer
meta: end_host
when: empty_find.files | count == 0 and inventory_hostname != 'host1'
- debug:
msg: "I am a sad message, because I will never display :'( But hopefully host1 likes me :')"
- name: I am the evil playbook killer
meta: end_play
when: empty_find.files | count == 0
- debug:
msg: "I am a sad message, because I will never display :'( No one likes me..."
Where you can see, that, I am skipping the very last debug message by ending the play, when I ended two out of the three hosts I have in my hosts group on the step before.
Output of that playbook:
PLAY [hosts] **************************************************************************************************************************
TASK [Delegate directory creation to localhost] ***************************************************************************************
ok: [host1 -> localhost]
TASK [Create directory on hosts] ****************************************************************************************************
ok: [host3]
ok: [host2]
ok: [host1]
TASK [Delegate file creation to localhost] ********************************************************************************************
changed: [host1 -> localhost]
TASK [Create file on hosts] ****************************************************************************************************
changed: [host2]
changed: [host1]
changed: [host3]
TASK [Delegate a find to localhost] ***************************************************************************************************
ok: [host1 -> localhost]
TASK [Find in the host for comparison] ************************************************************************************************
ok: [host1]
ok: [host3]
ok: [host2]
TASK [List /test of localhost] ********************************************************************************************************
ok: [host1] => {
"msg": [
"/test/localhost.txt"
]
}
ok: [host2] => {
"msg": [
"/test/localhost.txt"
]
}
ok: [host3] => {
"msg": [
"/test/localhost.txt"
]
}
TASK [List /test of host] *************************************************************************************************************
ok: [host1] => {
"msg": [
"/test/host.txt"
]
}
ok: [host2] => {
"msg": [
"/test/host.txt"
]
}
ok: [host3] => {
"msg": [
"/test/host.txt"
]
}
TASK [Remove /test folder on localhost] ***********************************************************************************************
changed: [host1 -> localhost]
TASK [Delegate an empty find to localhost] ********************************************************************************************
ok: [host1 -> localhost]
TASK [Here are our hostnames from the inventory] **************************************************************************************
ok: [host1] => {
"msg": "host1"
}
ok: [host2] => {
"msg": "host2"
}
ok: [host3] => {
"msg": "host3"
}
TASK [debug] **************************************************************************************************************************
ok: [host1] => {
"msg": "I am a sad message, because I will never display :'( But hopefully host1 likes me :')"
}
PLAY RECAP ****************************************************************************************************************************
host1 : ok=12 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host2 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host3 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
In the output, you can easily spot the delegation, as is will output
changed: [host1 -> localhost]
When a non-delegated task would just go
changed: [host1]
Using hostvars
I would say the approach is a little bit less state of the art, but could become handy if you would like to delegate on edge cases. I have used it before, so it is not like there is no situation where it could not end up has being the right solution.
Here is the example
---
- hosts: localhost
gather_facts: false
tasks:
- name: Find in localhost
find:
path: /not/existing/folder
file_type: any
register: find
- name: This will register the list under find.files as a variable on the host, making it accessible via hostvars
set_fact:
find_result: "{{ find.files }}"
- meta: end_play
when: find.files | count == 0
- debug:
msg: I am the sanity check message, proving the end_play did happen
- hosts: hosts
gather_facts: false
tasks:
- name: Just to show we are not cheating with an empty host group, we display the hosts names
debug:
msg: "{{ inventory_hostname }}"
- name: To show it is really our empty list and not an empty string or a null variable
debug:
msg: "{{ hostvars['localhost']['find_result'] }}"
- meta: end_play
when: "hostvars['localhost']['find_result'] | count == 0"
- debug:
msg: I am a first sanity check message, proving the end_play did happen
- debug:
msg: I am a second sanity check message, proving the end_play did happen
- hosts: localhost
gather_facts: false
tasks:
- name: To show it is really our empty list and not an empty string or a null variable
debug:
msg: "{{ hostvars['localhost']['find_result'] }}"
- meta: end_play
when: "hostvars['localhost']['find_result'] | count == 0"
- debug:
msg: I am a first sanity check message, proving the end_play did happen
- debug:
msg: I am a second sanity check message, proving the end_play did happen
Where you can see that, at the beginning of each new tasks group for a host, I just run the meta to end the play, based on the variable find_result registered out of the find result
Here is the output for this one
PLAY [localhost] **********************************************************************************************************************
TASK [Find in localhost] **************************************************************************************************************
ok: [localhost]
TASK [This will register the list under find.files as a variable on the host, making it accessible via hostvars] *********************
ok: [localhost]
PLAY [hosts] **************************************************************************************************************************
TASK [Just to show we are not cheating with an empty host group, we display the hosts names] ******************************************
ok: [host1] => {
"msg": "host1"
}
ok: [host2] => {
"msg": "host2"
}
ok: [host3] => {
"msg": "host3"
}
TASK [To show it is really our empty list and not an empty string or a null variable] ************************************************
ok: [host1] => {
"msg": []
}
ok: [host2] => {
"msg": []
}
ok: [host3] => {
"msg": []
}
PLAY [localhost] **********************************************************************************************************************
TASK [To show it is really our empty list and not an empty string or a null variable] ************************************************
ok: [localhost] => {
"msg": []
}
PLAY RECAP ****************************************************************************************************************************
host1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host3 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Try using a block that only triggers your second two tasks after you do your first:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html
---
- name: Play 1
hosts: 127.0.0.1
tasks:
- name: find the latest file
find: paths=/var/lib/jenkins/jobs/process/workspace/files
file_type=file
age=-1m
age_stamp=mtime
register: files
- name: Play 2 & 3 if Play 1 has a file
block:
- name: Play 2
hosts: all
serial: 5
tasks:
- name: copy latest file
copy: src=data_init/goldy.init.qa dest=/data01/admin/files/goldy.init.qa owner=golden group=golden
- name: copy latest file
copy: src=data_init/goldy.init.qa dest=/data02/admin/files/goldy.init.qa owner=golden group=golden
- name: Play 3
hosts: 127.0.0.1
tasks:
- name: execute command
shell: ./data_init --init_file ./goldy.init.qa
when: files != ""
...
Using Variables in hosts value
One trick you can use is set the value of target hosts for the other plays in Play1 this way you can set it to a value that doesn't exist in your inventory (DONOTEXECUTE in the case shown below) the other plays will simply be skipped
- name: Play 1
hosts: 127.0.0.1
tasks:
- name: find the latest file
find: paths=/var/lib/jenkins/jobs/process/workspace/files
file_type=file
age=-1m
age_stamp=mtime
register: files
- name: Copy file, if found
copy:
src: "some stuff here"
dest: "some other stuff here"
when: files.files|count > 0
- name: end_plays
set_fact:
play2_hosts: DONOTEXECUTE
play3_hosts: DONOTEXECUTE
when: files.files|count == 0
- name: continue_plays
set_fact:
play2_hosts: all
play3_hosts: 127.0.0.1
when: files.files|count > 0
- name: Play 2
hosts: "{{hostvars['localhost']['play2_hosts']}}"
serial: 5
tasks:
- name: copy latest file
copy: src=data_init/goldy.init.qa dest=/data01/admin/files/goldy.init.qa owner=golden group=golden
- name: copy latest file
copy: src=data_init/goldy.init.qa dest=/data02/admin/files/goldy.init.qa owner=golden group=golden
- name: Play 3
hosts: "{{hostvars['localhost']['play3_hosts']}}"
tasks:
- name: execute command
shell: ./data_init --init_file ./goldy.init.qa
args:
chdir: /var/lib/jenkins/jobs/process/workspace/data_init/

Resources