Dynamic string/list manipulation

86 views
Skip to first unread message

Strahinja Kustudić

unread,
Jul 1, 2015, 10:20:17 AM7/1/15
to ansible...@googlegroups.com
I'm not even sure how to name this thread, but hopefully from the example you will understand what I mean.

I was reading this article https://medium.com/@jmarhee/deploying-a-glusterfs-storage-cluster-with-ansible-on-digitalocean-14dacd721e23 and in the middle of the article below the "Creating the cluster" title there is a playbook with 3 plays, one for each host in the cluster, where in each play the current host IP address is excluded. The reason why I hate this solution, is because if tomorrow you would like to add one more node to the cluster, besides adding the new node to the appropriate group, you would need to edit every task of that playbook and add a new play for the new node, which is anything but simple.

On IRC in the #ansible channel halberom gave a nice solution to this problem https://gist.github.com/halberom/72b70a8cba934b9d58b9, but it feels sort of like a hack and that it should be easier to accomplish something like this in Ansible.

Is there an easier solution (e.g. using with_items/when), and if not maybe there should be?

Brian Coca

unread,
Jul 1, 2015, 2:06:12 PM7/1/15
to ansible...@googlegroups.com
you could

set_fact: mypeers={{ groups['gluster']|difference([inventory_hostname])}}
> --
> 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/d1c4f82e-38cd-4f30-a443-a1683ccdaf94%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Strahinja Kustudić

unread,
Jul 1, 2015, 3:30:41 PM7/1/15
to ansible...@googlegroups.com
I knew about that part, but from there how do you extract IP addresses from that list of hosts and put them in a string concatenated by a comma?

Brian Coca

unread,
Jul 1, 2015, 4:05:07 PM7/1/15
to ansible...@googlegroups.com
something like:

with _items: otherservers
set_fact:
otherips: "{{otherips}}, " + hostvars[item]['ansible_' +
rhel7_interface in hostvars[host]|ternary(['an['ansible_' +
rhel7_interface, 'ansible_' + default_interface) ....

or I would try using the jinja2 map filter. its going to be ugly in any case.


--
Brian Coca

Strahinja Kustudić

unread,
Jul 2, 2015, 7:16:05 AM7/2/15
to ansible...@googlegroups.com
That doesn't work for me. Here is a simple playbook for testing:

---
- hosts: localhost
  gather_facts
: no
  vars
:
    otherips
: ""
  tasks
:
   
- set_fact:
        otherips
: "{{ otherips }},{{ item }}"
      with_items
: groups['web_servers']

   
- debug: var=otherips

On 1.9 it sets otherips to the last item:

$ ansible-playbook -i test_hosts loop_hosts.yml

PLAY
[localhost] **************************************************************

TASK
: [set_fact ] *************************************************************
ok
: [localhost] => (item=web-01)
ok
: [localhost] => (item=web-02)
ok
: [localhost] => (item=web-03)

TASK
: [debug var=otherips] ****************************************************
ok
: [localhost] => {
   
"var": {
       
"otherips": ",web-03"
   
}
}

PLAY RECAP
********************************************************************
localhost                  
: ok=2    changed=0    unreachable=0    failed=0

While on 2.0 it throws a recursion error:

ansible-playbook -i test_hosts loop_hosts.yml    

PLAY
: ***************************************************************************
ERROR
! recursive loop detected in template string: {{ otherips }},{{ item }}

So I'm not sure if it's possible to do it this way.
Reply all
Reply to author
Forward
0 new messages