Working with with_items and some filter

254 views
Skip to first unread message

Bastian Bringenberg

unread,
Aug 25, 2014, 4:46:30 AM8/25/14
to ansible...@googlegroups.com
Hey ho,

I would like to deploy an application to several vhosts is item.useApplication exists. I am possible to do that, download files and so on and so on. But I get a problem as soon as I want to do an action if the application is never installed.

Normally:

- stat: path=/etc/.git
  register: exists_git
  ignore_errors: yes

- lineinfile: dest=/etc/rkhunter.conf line='ALLOWHIDDENDIR="/etc/.git"' owner=root group=root mode=0644
  when: exists_git.stat.exists

But how is it possible for me to do this combined with "with_items"? One approach, that (untested) will not work in my opinion:

 - stat: path=/etc/.git
  register: exists_git_{{ item.name }}
  with_items: hosts
  when: item.useApplication
  ignore_errors: yes

- lineinfile: dest=/etc/rkhunter.conf line='ALLOWHIDDENDIR="/etc/.git"' owner=root group=root mode=0644
  with_items: hosts
  when: item.useApplication
  when: exists_git_{{ item.name }}.stat.exists

Is someone able to help me here?

Have a nice week,
Greetings from Bochum, RuhrArea, Germany,

Bastian


PS: rkhunter is just a demonstration how I do this normally^^.

Michael DeHaan

unread,
Aug 25, 2014, 10:06:31 AM8/25/14
to ansible...@googlegroups.com
You can't say "when:" twice, but you can feed it a list.

when:
   - item.useApplication
   - other_conditional

What you are doing with building a variable name in a conditional is a little weird, and this doesn't work.

Rather, register when used with "with_items" will return an array, which you can easily debug:

 - stat: path=/etc/.git
   register: stats 
   with_items: hosts
   when: item.useApplication

- debug: var=stats

Though ultimately, it looks like you should drop the lineinfile and just include your logic in a template, and it will be much cleaner and easier.






--
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/2ced1119-8fda-4ace-a27b-db3879280f55%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bastian Bringenberg

unread,
Aug 25, 2014, 10:55:50 AM8/25/14
to ansible...@googlegroups.com
Hey ho,


You can't say "when:" twice, but you can feed it a list.

when:
   - item.useApplication
   - other_conditional

perfect!

What you are doing with building a variable name in a conditional is a little weird, and this doesn't work.

Rather, register when used with "with_items" will return an array, which you can easily debug:

 - stat: path=/etc/.git
   register: stats 
   with_items: hosts
   when: item.useApplication

- debug: var=stats

Though ultimately, it looks like you should drop the lineinfile and just include your logic in a template, and it will be much cleaner and easier.

I do that with templates but I want to unzip, move and copy some files to create a perfect application environment. Perhaps your return of an array will solve my problem^^.

As soon as I tried this solution I will give you an answer! Thanks for the moment!


You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/ZP5szDnH0iI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Bastian Bringenberg

unread,
Aug 25, 2014, 2:58:39 PM8/25/14
to ansible...@googlegroups.com
I am not able to figure out which array position is important for the current item as:


hosts:
* 1 - noApplication ( ID = 3 )
* 2 - Application ( ID = 2 )
* 3 - Application ( ID = 42 )

would be 

* 1 ( ID = 2 )
* 2 ( ID = 42 )

And I see no way to match those... Any Idea? Or is it possible without a lot of problems to create a plugin in ansible to solve this problem?

Michael DeHaan

unread,
Aug 25, 2014, 6:26:27 PM8/25/14
to ansible...@googlegroups.com
Sorry, I'm no following the above.

Can we step back a moment?   

This appears to be a playbook that fixes various things in particular git directories, if found, for a very large number of git directories?

Knowing what you are trying to do might help suggest a more idiomatic solution.




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

Bastian Bringenberg

unread,
Aug 26, 2014, 2:17:50 AM8/26/14
to ansible...@googlegroups.com
Okay =).

I could write it down in some kind of PHP so you get my point.

I have got:

hosts:
  - name: "domain.tld"
    database:
      name: test
      user: test
      pw: test
    systemUser: domain
    useApplication: true

This will create me /var/www/domain.tld with everything I normally need. No I add "useApplication" for example ownCloud.
The steps I need to do are:

* Download Zip File
* Extract Zip File
* Move Content of ZipFile Around ( important as owncloud.zip contains a folder named owncloud/ )
* Install a Template for the Settings
* Run a Database Dummy
* Remove WebDav
* Restart Apache

But only do that if there is no locking file. I do not want to have all of this done on each run and need some kind of if( ! file_exists(lockfile) ) doEverything();
And all of this should of course only happen if useApplication is set to true^^.

foreach($hosts as $item) {
if( $item->useApplication ) {
if( ! file_exsists( $item->getLockfile() ) ) {
doAllAbove();
}
}
}

Is this more clear?

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/ZP5szDnH0iI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Michael DeHaan

unread,
Aug 26, 2014, 8:43:04 AM8/26/14
to ansible...@googlegroups.com
Not really.

Is the issue you have a delegated host action that you only want to run once?

If so you could factor this out to an action that happens last in the playbook.




Bastian Bringenberg

unread,
Aug 26, 2014, 6:14:30 PM8/26/14
to ansible...@googlegroups.com
Not only once a run, I want to have it run only to install it^^.

So if the vhost Application is already installed I don't want to have the package to unzip anymore, move files around or run the database dummy. I am able to control this behavior without "with_items" but as soon as I a have an array of informations to be checked for each I am not able to 

 - name: "domain.tld"
    database:
      name: test
      user: test
      pw: test
    systemUser: domain
    useApplication: true


Another option would be to register the stats informations into my "item" from the config array so I am able to say:

 - name: whatever
   whatever command
   with_items: hosts
   when: item.myFile.stats.exists

Easy what I need to do:

foreach ( hosts as item){
if( item.hasApplication ) {
temp = stat( item.filedir + 'myLockFile' )
if( ! temp.exists) {
/**
 * Download a Script
 * unzip it
 * move files around
 * Create templates ( config files ) with informations from single host
 * Change Owner of certain files
 * Create myLockFile
 */
}
}


Reply all
Reply to author
Forward
0 new messages