2

I have a variable yaml file:

---
apps:
  - client
  - node
  - gateway
  - ic
  - consul

and this task:

- name: register if apps exists
  stat: path="/etc/init.d/{{ item }}"
  with_items: apps
  register: "{{ item.stat.exists }}exists"

I need to end up with a variable for each app with a value of true or false whether the file exists or not:

clientexists = true
nodeexists = true
gatewayexists = false
icexists = true
consulexists = false

For some reason, the item and exists concat is not working.

How can I achieve that??

0

1 Answer 1

6

Try this hope this will help you out. While looping in Stats.yml then msg field will contain your desired output.

Variables.yml Here we are defining variables

---
apps:
  - client
  - node
  - gateway
  - ic
  - consul

Stats.yml

---
- hosts: localhost
  name: Gathering facts
  vars_files:
  - /path/to/variables.yml
  tasks:
  - name: "Here we are printing variables"
    debug:
     msg: "{{apps}}"

  - name: "Here we are gathering stats and registering it"
    stat:
     path: "/etc/init.d/{{item}}"
    register: folder_stats
    with_items:
    - "{{apps}}"

  - name: "Looping over registered variables, Its msg field will contain desired output"
    debug:
     msg: "{{item.invocation.module_args.path}}: {{item.stat.exists}}"
    with_items:
    - "{{folder_stats.results}}"

...

folder_stats will contain whole result, for individually referring to single - single result You can use it like this in you playbook.

folder_stats.results[0].stat.exists 
folder_stats.results[1].stat.exists 
folder_stats.results[2].stat.exists 
folder_stats.results[3].stat.exists
Sign up to request clarification or add additional context in comments.

10 Comments

How can I use folder_stats later on as a varible? I need to see for each app if it exists, how can I do that?
@Moshe Through out this playbook, you can use it like this with_items: - "{{folder_stats.results}}" for looping
While looping you will get such results in loop ok: ,... "changed": false, "invocation": { "module_args": { "checksum_algorithm": "sha1", "follow": false, "get_checksum": true, "get_md5": true, "mime": false, "path": "/etc/init.d/consul" }, "module_name": "stat" }, "item": "consul", "stat": { "exists": false } }, "msg": "/etc/init.d/consul: False" }
and msg field will contain you desired output you can have a look on it stackoverflow.com/questions/42186301/…
But I need to be able to access each app individually, not with a loop. I need to be able to access the nodeexists on different places and I can't loop each time.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.