help on dynamic with_dict xxxx.results[0].stat.exists == false

118 views
Skip to first unread message

JohnP

unread,
Jun 11, 2015, 6:09:33 AM6/11/15
to ansible...@googlegroups.com
Hi Group,

I am trying to establish a logic on a creating web server self signed certificates.
(please note that this issue could be related any other task like depending on files 
in directories, etc.)

Logic is that I would like to create subsequent files (as cert request and the newly signed key 
this instance).

I am doing stat on the private key earlier in the role. If the private key does NOT 
exists, I am going to create csr and eventually signed pem file.

Please note that my entire logic worked when I did one vhost in a time, perfect.
However, when I wanted to do them in a "loop" which more professional and tidy, it hit
brick wall since with_dict alters to stat results explained in documentation (hint, hint)
After a bit of debugging I have managed to locate location of stat values, but now
I am stuck on 
when:  server_private_key.results[0].stat.exists == false
issue. How can I match the item number dynamically on results[0] rather than hard coding it to zero?
(as can easily be done on any primitive programming language)

Best Regards: JP

- stat: path={{item.value.ssl_dir}}/{{item.value.ssl_name}}.key
  register: "server_private_key"
  with_dict: "{{ssl_certs}}"

---snip---

- name:  SSL 'generate-cert-request'
  shell: "/usr/bin/openssl req -new -subj \"{{item.value.subject}}\" -key {{item.value.ssl_dir}}/{{item.value.ssl_name}}.key -out {{item.value.ssl_dir}}/{{item.value.ssl_name}}.csr"
  when:  server_private_key.results[0].stat.exists == false
  with_dict: "{{ssl_certs}}"
  tags: ['generate-server-key-Step-2']


benno joy

unread,
Jun 11, 2015, 6:26:40 AM6/11/15
to ansible...@googlegroups.com
Hi,

i think this can be solved if you can make you ssl_certs a list of dicts and use with_indexed_list 




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e629b83c-3a43-4510-8213-948fc5b7d3c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

JohnP

unread,
Jun 11, 2015, 6:36:58 AM6/11/15
to ansible...@googlegroups.com
I think my question is more "fundamental" as 

How can I capture and use the array member number of with_dict in results[0].stat.exists like on any primitive language as an example below.


#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done


Sample outputs:

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

Best Regards: JP

Brian Coca

unread,
Jun 11, 2015, 9:34:32 AM6/11/15
to ansible...@googlegroups.com
you can use sequence, start at 0 and end at list length
> https://groups.google.com/d/msgid/ansible-project/1d12c4b8-3a52-4cbc-8e7a-b44a5e95e834%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages