variables

Adding strings to an array in Ansible

From time to time, I need to dynamically build a list of strings (or a list of other things) using Ansible's set_fact module.

Since set_fact is a module like any other, you can use a with_items loop to loop over an existing list, and pull out a value from that list to add to another list.

For example, today I needed to retrieve a list of all the AWS EC2 security groups in a region, then loop through them, building a list of all the security group names. Here's the playbook I used:

How to set complex string variables with Drush vset

I recently ran into an issue where drush vset was not setting a string variable (in this case, a time period that would be used in strtotime()) correctly:

<br />
# Didn't work:<br />
$ drush vset custom_past_time '-1 day'<br />
Unknown options: --0, --w, --e, --k.  See `drush help variable-set`      [error]<br />
for available options. To suppress this error, add the option<br />
--strict=0.<br />

Using the --strict=0 option resulted in the variable being set to a value of "1".

After scratching my head a bit, trying different ways of escaping the string value, using single and double quotes, etc., I finally realized I could just use variable_set() with drush's php-eval command (shortcut ev):

Adding a class to <body> in a module in Drupal 7

In Drupal 6, I would often resort to using my theme's template.php file to implement template_preprocess(), and add in the class to Drupal's $body_classes array. In Drupal 7, some new hooks were introduced that allow me to do this more easily, and inside my .module files.

Introducing hook_preprocess_HOOK()

<?php
 
<br /
?>
/**
* Implements hook_preprocess_HOOK().
*/
function custom_preprocess_html(&$vars) {
$vars['classes_array'][] = 'my-class-here';
}
?>