# Conditions and configure options

**URL:** <https://forum.ansible.com/t/conditions-and-configure-options/13912>\
**Category:** Ansible Project\
**Created:** [November 15, 2013, 7:35am UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912 "2013-11-15T07:35:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Droogmans](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@Peter\_Droogmans](https://forum.ansible.com/u/Peter_Droogmans)\
**Post date:** [November 15, 2013, 7:35am UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/1 "2013-11-15T07:35:13Z")

</div>

I started using ansible a week a go and I’m loving it, but I ran into a problem with conditions. I read that concatenation isn’t supported so I tried the following

This works if all options are set to true, but with the example below it fails because one of the options is false and the variable is defined but contains no stdout. I tried adding the following snippet, but apparently the variable is always set even if the condition is not met.

- name: Nginx | Set null option  
command: echo  
register: nginx\_cache\_purge\_option  
when: not nginx\_cache\_purge

I guess there’s probably a better way to handle this. Keep in mind that there will be more options

settings.yml:  
\_NGINX\_VERSION: ‘1.5.6’  
nginx\_spdy: true  
nginx\_upload\_progress: true  
nginx\_cache\_purge: false

roles/nginx/tasks/build\_from\_source.yml:

- name: Nginx | Update apt cache  
apt: update\_cache=yes cache\_valid\_time=3600

- name: Nginx | Dependencies  
apt: pkg={{ item }} state=present  
with\_items:

- libpcre3

- libpcre3-dev

- libgeoip-dev

- name: Nginx | Remove nginx dir  
command: chdir=/var/opt removes=/var/opt/nginx\* rm -rf /var/opt/nginx\*

# nginx

- name: Nginx | Get archive  
get\_url: url=[Under Construction](http://files.aegir.cc/dev/src/nginx-){{ \_NGINX\_VERSION }}.tar.gz dest=/var/opt/nginx-{{ \_NGINX\_VERSION }}.tar.gz

- name: Nginx | Extract archive  
command: chdir=/var/opt creates=/var/opt/nginx-{{ \_NGINX\_VERSION }} tar xvf nginx-{{ \_NGINX\_VERSION }}.tar.gz

- name: Nginx | Remove archive  
command: removes=/var/opt/nginx-{{ \_NGINX\_VERSION }}.tar.gz rm /var/opt/nginx-{{ \_NGINX\_VERSION }}.tar.gz

# nginx-upload-progress-module

- name: Nginx | Get archive  
get\_url: url=[http://files.aegir.cc/dev/src/nginx-upload-progress-module.tar.gz](http://files.aegir.cc/dev/src/nginx-upload-progress-module.tar.gz) dest=/var/opt/nginx-upload-progress-module.tar.gz  
when: nginx\_upload\_progress

- name: Nginx | Extract archive  
command: chdir=/var/opt creates=/var/opt/nginx-upload-progress-module tar xvf nginx-upload-progress-module.tar.gz  
when: nginx\_upload\_progress

- name: Nginx | Remove archive  
command: removes=/var/opt/nginx-upload-progress-module.tar.gz rm /var/opt/nginx-upload-progress-module.tar.gz  
when: nginx\_upload\_progress

- name: Nginx | Set option  
command: echo --add-module=/var/opt/nginx-upload-progress-module/  
register: nginx\_upload\_progress\_option  
when: nginx\_upload\_progress

# ngx-cache-purge

- name: Nginx | Get archive  
get\_url: url=[http://files.aegir.cc/dev/src/ngx-cache-purge.tar.gz](http://files.aegir.cc/dev/src/ngx-cache-purge.tar.gz) dest=/var/opt/ngx-cache-purge.tar.gz  
when: nginx\_cache\_purge

- name: Nginx | Extract archive  
command: chdir=/var/opt creates=/var/opt/ngx-cache-purge tar xvf ngx-cache-purge.tar.gz  
when: nginx\_cache\_purge

- name: Nginx | Remove archive  
command: removes=/var/opt/ngx-cache-purge.tar.gz rm /var/opt/ngx-cache-purge.tar.gz  
when: nginx\_cache\_purge

- name: Nginx | Set option  
command: echo --add-module=/var/opt/ngx-cache-purge/  
register: nginx\_cache\_purge\_option  
when: nginx\_cache\_purge

# spdy - nginx\_spdy

- name: Nginx | Set option  
command: echo --with-http\_spdy\_module  
register: nginx\_spdy\_option  
when: nginx\_spdy

# compile time

- name: Nginx | Configure  
command: chdir=/var/opt/nginx-{{ \_NGINX\_VERSION }} sh ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=www-data --group=www-data --with-http\_realip\_module --with-http\_gzip\_static\_module --with-http\_stub\_status\_module --with-http\_ssl\_module --with-http\_dav\_module --with-http\_flv\_module --with-http\_mp4\_module --without-mail\_pop3\_module --without-mail\_imap\_module --without-mail\_smtp\_module --without-http\_scgi\_module --without-http\_uwsgi\_module --with-ipv6 --with-http\_geoip\_module --with-debug {{ nginx\_spdy\_option.stdout }} {{ nginx\_cache\_purge\_option.stdout }} {{ nginx\_upload\_progress\_option.stdout }}
- name: Nginx | Make  
command: chdir=/var/opt/nginx-{{ \_NGINX\_VERSION }} make --quiet
- name: Nginx | Install  
command: chdir=/var/opt/nginx-{{ \_NGINX\_VERSION }} make --quiet install

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 20, 2013, 4:02am UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/2 "2013-11-20T04:02:41Z")

</div>

“I read that concatenation isn’t supported so I tried the following”

Be careful what you read on the internet 🙂 I’d be curious on specifically what you think is not possible if you are referring to strings

“but with the example below it fails because one of the options is false and the variable is defined but contains no stdout”

Which step fails? That’s a lot of copy/paste there…

---

<div class="post-metadata">

**Author:** ![Peter\_Droogmans](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@Peter\_Droogmans](https://forum.ansible.com/u/Peter_Droogmans)\
**Post date:** [November 22, 2013, 1:29pm UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/3 "2013-11-22T13:29:16Z")

</div>

Sorry about the wall of text, here’s a short example

# spdy - nginx\_spdy

- name: Nginx | Set option  
command: echo --with-http\_spdy\_module  
register: nginx\_spdy\_option  
when: nginx\_spdy

- name: Nginx | Configure  
command: chdir=/var/opt/nginx-{{ \_NGINX\_VERSION }} sh ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=www-data --group=www-data --with-http\_realip\_module --with-http\_gzip\_static\_module --with-http\_stub\_status\_module --with-http\_ssl\_module --with-http\_dav\_module --with-http\_flv\_module --with-http\_mp4\_module --without-mail\_pop3\_module --without-mail\_imap\_module --without-mail\_smtp\_module --without-http\_scgi\_module --without-http\_uwsgi\_module --with-ipv6 --with-http\_geoip\_module --with-debug {{ nginx\_spdy\_option.stdout }}

When nginx\_spdy == true this works, but when I set nginx\_spdy to false I get

TASK: [nginx | debug msg=“nginx\_spdy\_option is {{nginx\_spdy\_option}}”] \*\*\*\*\*\*\*\*  
ok: [vagrant1] =\> {  
“item”: “”,  
“msg”: “nginx\_spdy\_option is {u’skipped’: True, u’changed’: False}”  
}

TASK: [nginx | Nginx | Configure] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [vagrant1] =\> One or more undefined variables: ‘dict object’ has no attribute ‘stdout’

---

<div class="post-metadata">

**Author:** ![Peter\_Droogmans](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@Peter\_Droogmans](https://forum.ansible.com/u/Peter_Droogmans)\
**Post date:** [November 22, 2013, 1:41pm UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/4 "2013-11-22T13:41:07Z")

</div>

If concatenate is possible, what is the syntax to do something like this

- name: Nginx | Set option  
command: “–with-http\_spdy\_module” \> {{ nginx\_build\_options }}  
when: nginx\_spdy

- name: Nginx | Configure  
command: chdir=/var/opt/nginx-{{ \_NGINX\_VERSION }} … {{ nginx\_build\_options }}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 22, 2013, 9:08pm UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/5 "2013-11-22T21:08:49Z")

</div>

Looks like you should also apply the conditional on the second task and maybe have another version of the task that uses the “not false” vs “false” criteria and doesn’t use that variable.

---

<div class="post-metadata">

**Author:** ![Peter\_Droogmans](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@Peter\_Droogmans](https://forum.ansible.com/u/Peter_Droogmans)\
**Post date:** [November 23, 2013, 10:31am UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/6 "2013-11-23T10:31:18Z")

</div>

This will work for 1 option, but I have like 7 of them, if I use that approach it means I have to add a task for any possible combination 😕

Is there a way to concatenate strings, because then I could just add all options to one string variable?

Cheers,  
Peter

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [November 23, 2013, 12:38pm UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/7 "2013-11-23T12:38:24Z")

</div>

I'm not sure I follow your example, but concatenation in jinja is just this  
syntax:

&nbsp;&nbsp;{{ "some string " ~ var1 ~ " " ~ var2 }}

While Imho cleaner, it's the same as

&nbsp;&nbsp;"some string {{ var1 }} {{ var2 }}"

or

&nbsp;&nbsp;"some string {{ var1 ~' '~ var2 }}"

Using + for concatenation also works, but I the Jinja2 docs somewhere  
mention ~ is preferred for string concatenation.

Now, if you have a default setting for nginx\_build\_options, you can do:

&nbsp;&nbsp;set\_fact: nginx\_build\_options={{ nginx\_build\_options ~ '  
--with-http\_spdy\_module'  
}}  
&nbsp;&nbsp;when: nginx\_spdy

HTH,

Serge

---

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [November 23, 2013, 1:52pm UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/8 "2013-11-23T13:52:04Z")

</div>

Hello,

I did not know the set\_fact plugin it is worth investigating thanks.  
Here are 2 other options :

(1) concatenate via echo

- hosts: localhost  
gather\_facts: no  
tasks:
- name: initialize nginx build options  
command: echo “./configure”

register: nginx\_build\_options

- name: nginx\_spdy option  
command: echo “{{ nginx\_build\_options.stdout ~ ’ --with-http\_spdy\_module’}}”

register: nginx\_build\_options  
when: nginx\_spdy

- name: nginx\_upload\_progress option  
command: echo “{{ nginx\_build\_options.stdout ~ ’ --add-module=/var/opt/nginx-upload-progress-module/'}}”  
register: nginx\_build\_options

when: nginx\_upload\_progress

- name: output  
debug: var=nginx\_build\_options

beware: this will make a lot of ssh round trips i guess i you have tons of options. maybe using local\_action: could help here.

(2) template: and command:

use the template: module to send to the remote a templated build script.  
in this template, you will be able to hide all the complexity of the configure options without making a long list of nearly useless tasks + you will send a lot less ssh commands

then execute your script with the command: module

Hope this helps  
Jerome

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [November 23, 2013, 3:56pm UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/9 "2013-11-23T15:56:07Z")

</div>

Definitely, plus even with local action, you have an extra process spawned  
to execute the command​, and you have to use the more "ugly"  
registered.stdout aftwerwards.  
set\_fact is just an action plugin which stays local

---

<div class="post-metadata">

**Author:** ![Peter\_Droogmans](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@Peter\_Droogmans](https://forum.ansible.com/u/Peter_Droogmans)\
**Post date:** [November 23, 2013, 8:53pm UTC](https://forum.ansible.com/t/conditions-and-configure-options/13912/10 "2013-11-23T20:53:02Z")

</div>

Jerome, Serge,

Thanks for your assistance, I have a working version. In case somebody else is looking for something similar

in settings.yml  
nginx\_spdy: true  
nginx\_upload\_progress: true  
nginx\_cache\_purge: false  
nginx\_build\_options: ‘’

in the role.yml

- name: Nginx spdy | Set option  
set\_fact:  
nginx\_build\_options: “{{ nginx\_build\_options }} --with-http\_spdy\_module”  
when: nginx\_spdy

Using a separate build script is probably an option as well, I’ll keep it in mind.

Cheers and thanks for the help  
Peter
