How to correctly define subelements in Ansible jinja2 template(with subelements)?How to get the host name of the current machine as defined in the Ansible hosts file?How to create a directory using AnsibleHow can I test jinja2 templates in ansible?ansible/jinja2 get unique subelementsInsert line in nginx configuration file ansible inside http Add up Ansible variable in Jinja2 templateAnsible/jinja2: Use filter result in if conditionI want to include another Jinja2 template in an Ansible context in Jinja2Jinja2/Ansible Comparing HostVars Values In A TemplateFiltering Array of Vars in Ansible
Left align, center align and right align a line of three parts respectively
What is this "opened" cube called?
Understanding data transmission rates over copper wire
Why do IR remotes influence AM radios?
Get contents before a colon
'Horseshoes' for Deer?
Can copper pour be used as an alternative to large traces?
Is it good practice to speed up and slow down where not written in a song?
Match blank lines before a word awk
Where should I draw the line on follow up questions from previous employer
What is the sound/audio equivalent of "unsightly"?
Distance between two points - by ID in QGIS
Unexpected behavior after assignment of function object to function wrapper
Was it illegal to blaspheme God in Antioch in 360.-410.?
Do universities maintain secret textbooks?
How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?
Are sweatpants frowned upon on flights?
Can authors email you PDFs of their textbook for free?
Don't look at what I did there
How were US credit cards verified in-store in the 1980's?
How to understand payment due date for credit card?
Can UV radiation be safe for the skin?
Eshet Chayil in the Tunisian service
Why do motor drives have multiple bus capacitors of small value capacitance instead of a single bus capacitor of large value?
How to correctly define subelements in Ansible jinja2 template(with subelements)?
How to get the host name of the current machine as defined in the Ansible hosts file?How to create a directory using AnsibleHow can I test jinja2 templates in ansible?ansible/jinja2 get unique subelementsInsert line in nginx configuration file ansible inside http Add up Ansible variable in Jinja2 templateAnsible/jinja2: Use filter result in if conditionI want to include another Jinja2 template in an Ansible context in Jinja2Jinja2/Ansible Comparing HostVars Values In A TemplateFiltering Array of Vars in Ansible
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm writing ansible-playbook to divide nginx.conf with includes. In my opinion it would be more comfortable to use nginx.conf with such option cause I can include or exclude some config block at playbook vars.
At current time I have trouble with part: name: 2. Copy nginx.conf config.
playbook.yml:
- name: "setup_nginx"
hosts: "TEST_HOST"
gather_facts: yes
remote_user: root
vars:
nginx_worker_processes: " ansible_processor_cores "
nginx_worker_connections: "32768"
nginx_worker_rlimit_nofile: "abs "
nginx_directories:
- directory: inc
nginx_files:
- file: "gzip.inc"
- file: "logs.inc"
- file: "mime.types"
- file: "tuning.inc"
- file: "proxy.inc"
- file: "ssl.inc"
- directory: sites
nginx_files:
- file: "mysite1"
- file: "mysite2"
- tasks:
- name: 1. Create nginx directories
file:
path: "/etc/nginx/ item.directory "
state: directory
owner: nginx
group: nginx
with_items:
- " nginx_directories "
- name: 2. Copy nginx.conf config.
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: 0640
owner: nginx
group: nginx
backup: yes
with_subelements:
- " nginx_directories "
- nginx_files
nginx.conf.j2:
user nginx;
worker_processes ansible_processor_cores ;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile int;
http
% for val in nginx_directories %
include /etc/nginx/ item.0.directory / item.1.file ;
% endfor %
I expect result:
user nginx;
worker_processes 1;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile 65536;
http
include /etc/nginx/inc/gzip.inc;
include /etc/nginx/inc/logs.inc;
include /etc/nginx/inc/mime.types;
include /etc/nginx/inc/tuning.inc;
include /etc/nginx/inc/proxy.inc;
include /etc/nginx/inc/ssl.inc;
include /etc/nginx/sites/mysite1;
include /etc/nginx/sites/mysite2;
But actual result:
user nginx;
worker_processes 1;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile 65536;
http
include /etc/nginx/sites/mysite2.j2;
include /etc/nginx/sites/mysite2.j2;
I think the trouble is that I'm not correctly define subelements at template nginx.conf.j2.
Regards
ansible ansible-template
add a comment |
I'm writing ansible-playbook to divide nginx.conf with includes. In my opinion it would be more comfortable to use nginx.conf with such option cause I can include or exclude some config block at playbook vars.
At current time I have trouble with part: name: 2. Copy nginx.conf config.
playbook.yml:
- name: "setup_nginx"
hosts: "TEST_HOST"
gather_facts: yes
remote_user: root
vars:
nginx_worker_processes: " ansible_processor_cores "
nginx_worker_connections: "32768"
nginx_worker_rlimit_nofile: "abs "
nginx_directories:
- directory: inc
nginx_files:
- file: "gzip.inc"
- file: "logs.inc"
- file: "mime.types"
- file: "tuning.inc"
- file: "proxy.inc"
- file: "ssl.inc"
- directory: sites
nginx_files:
- file: "mysite1"
- file: "mysite2"
- tasks:
- name: 1. Create nginx directories
file:
path: "/etc/nginx/ item.directory "
state: directory
owner: nginx
group: nginx
with_items:
- " nginx_directories "
- name: 2. Copy nginx.conf config.
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: 0640
owner: nginx
group: nginx
backup: yes
with_subelements:
- " nginx_directories "
- nginx_files
nginx.conf.j2:
user nginx;
worker_processes ansible_processor_cores ;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile int;
http
% for val in nginx_directories %
include /etc/nginx/ item.0.directory / item.1.file ;
% endfor %
I expect result:
user nginx;
worker_processes 1;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile 65536;
http
include /etc/nginx/inc/gzip.inc;
include /etc/nginx/inc/logs.inc;
include /etc/nginx/inc/mime.types;
include /etc/nginx/inc/tuning.inc;
include /etc/nginx/inc/proxy.inc;
include /etc/nginx/inc/ssl.inc;
include /etc/nginx/sites/mysite1;
include /etc/nginx/sites/mysite2;
But actual result:
user nginx;
worker_processes 1;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile 65536;
http
include /etc/nginx/sites/mysite2.j2;
include /etc/nginx/sites/mysite2.j2;
I think the trouble is that I'm not correctly define subelements at template nginx.conf.j2.
Regards
ansible ansible-template
add a comment |
I'm writing ansible-playbook to divide nginx.conf with includes. In my opinion it would be more comfortable to use nginx.conf with such option cause I can include or exclude some config block at playbook vars.
At current time I have trouble with part: name: 2. Copy nginx.conf config.
playbook.yml:
- name: "setup_nginx"
hosts: "TEST_HOST"
gather_facts: yes
remote_user: root
vars:
nginx_worker_processes: " ansible_processor_cores "
nginx_worker_connections: "32768"
nginx_worker_rlimit_nofile: "abs "
nginx_directories:
- directory: inc
nginx_files:
- file: "gzip.inc"
- file: "logs.inc"
- file: "mime.types"
- file: "tuning.inc"
- file: "proxy.inc"
- file: "ssl.inc"
- directory: sites
nginx_files:
- file: "mysite1"
- file: "mysite2"
- tasks:
- name: 1. Create nginx directories
file:
path: "/etc/nginx/ item.directory "
state: directory
owner: nginx
group: nginx
with_items:
- " nginx_directories "
- name: 2. Copy nginx.conf config.
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: 0640
owner: nginx
group: nginx
backup: yes
with_subelements:
- " nginx_directories "
- nginx_files
nginx.conf.j2:
user nginx;
worker_processes ansible_processor_cores ;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile int;
http
% for val in nginx_directories %
include /etc/nginx/ item.0.directory / item.1.file ;
% endfor %
I expect result:
user nginx;
worker_processes 1;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile 65536;
http
include /etc/nginx/inc/gzip.inc;
include /etc/nginx/inc/logs.inc;
include /etc/nginx/inc/mime.types;
include /etc/nginx/inc/tuning.inc;
include /etc/nginx/inc/proxy.inc;
include /etc/nginx/inc/ssl.inc;
include /etc/nginx/sites/mysite1;
include /etc/nginx/sites/mysite2;
But actual result:
user nginx;
worker_processes 1;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile 65536;
http
include /etc/nginx/sites/mysite2.j2;
include /etc/nginx/sites/mysite2.j2;
I think the trouble is that I'm not correctly define subelements at template nginx.conf.j2.
Regards
ansible ansible-template
I'm writing ansible-playbook to divide nginx.conf with includes. In my opinion it would be more comfortable to use nginx.conf with such option cause I can include or exclude some config block at playbook vars.
At current time I have trouble with part: name: 2. Copy nginx.conf config.
playbook.yml:
- name: "setup_nginx"
hosts: "TEST_HOST"
gather_facts: yes
remote_user: root
vars:
nginx_worker_processes: " ansible_processor_cores "
nginx_worker_connections: "32768"
nginx_worker_rlimit_nofile: "abs "
nginx_directories:
- directory: inc
nginx_files:
- file: "gzip.inc"
- file: "logs.inc"
- file: "mime.types"
- file: "tuning.inc"
- file: "proxy.inc"
- file: "ssl.inc"
- directory: sites
nginx_files:
- file: "mysite1"
- file: "mysite2"
- tasks:
- name: 1. Create nginx directories
file:
path: "/etc/nginx/ item.directory "
state: directory
owner: nginx
group: nginx
with_items:
- " nginx_directories "
- name: 2. Copy nginx.conf config.
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: 0640
owner: nginx
group: nginx
backup: yes
with_subelements:
- " nginx_directories "
- nginx_files
nginx.conf.j2:
user nginx;
worker_processes ansible_processor_cores ;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile int;
http
% for val in nginx_directories %
include /etc/nginx/ item.0.directory / item.1.file ;
% endfor %
I expect result:
user nginx;
worker_processes 1;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile 65536;
http
include /etc/nginx/inc/gzip.inc;
include /etc/nginx/inc/logs.inc;
include /etc/nginx/inc/mime.types;
include /etc/nginx/inc/tuning.inc;
include /etc/nginx/inc/proxy.inc;
include /etc/nginx/inc/ssl.inc;
include /etc/nginx/sites/mysite1;
include /etc/nginx/sites/mysite2;
But actual result:
user nginx;
worker_processes 1;
events
worker_connections 32768;
use epoll;
multi_accept on;
worker_rlimit_nofile 65536;
http
include /etc/nginx/sites/mysite2.j2;
include /etc/nginx/sites/mysite2.j2;
I think the trouble is that I'm not correctly define subelements at template nginx.conf.j2.
Regards
ansible ansible-template
ansible ansible-template
asked Mar 27 at 22:56
KeinKein
326 bronze badges
326 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Remove the loop in you template task and in your template use
% for val in nginx_directories
include /etc/nginx/ val.0.directory / val.1.file ;
% endfor %
FWIW. There is an elegant solution with config_encoder_filters which encodes YAML data
my_nginx_vhost_config:
- server:
- listen 8080
- server_name www.example.com
- "location /":
- root /usr/local/www/nginx-dist/
- index index.html
with simple template
# ansible_managed
encode_nginx
to nginx configuration
# cat /usr/local/etc/nginx/conf.d/default.conf
# Ansible managed
server
listen 8080;
server_name www.example.com;
location /
root /usr/local/www/nginx-dist/;
index index.html;
Details are available in the nginx role.
Thanks, nice example. But with Your solutionloop: " nginx_directories "get error(item=u'directory': u'sites', u'nginx_files': [u'file': u'mysite1.j2', u'file': u'mysite2.j2']) => "changed": false, "item": "directory": "sites", "nginx_files": ["file": "mysite1.j2", "file": "mysite2.j2"], "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'nginx_directories'"
– Kein
Mar 28 at 2:06
Right. The loop in the template should be "% for val in item.nginx_files %". I've fixed the answer. Sorry for the noise.
– Vladimir Botka
Mar 28 at 6:08
Thanks, but this doesn't works as expected: it's works only with latest element in listitem.directoryso I get as result only this two lines:include /etc/nginx/sites/mysite1.j2; include /etc/nginx/sites/mysite2.j2;
– Kein
Mar 28 at 7:55
1
@vladimir-botka, @olivier-clavel Thanks a lot for all of You! Just remove loop from task and add Your line(for val in nginx_directories | subelements('nginx_files'))in template and all works as expected.
– Kein
Mar 28 at 8:25
1
I just edited the answer with the final solution. It should be accepted by @VladimirBotka to be visible to all.
– Zeitounator
Mar 28 at 8:27
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387719%2fhow-to-correctly-define-subelements-in-ansible-jinja2-templatewith-subelements%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Remove the loop in you template task and in your template use
% for val in nginx_directories
include /etc/nginx/ val.0.directory / val.1.file ;
% endfor %
FWIW. There is an elegant solution with config_encoder_filters which encodes YAML data
my_nginx_vhost_config:
- server:
- listen 8080
- server_name www.example.com
- "location /":
- root /usr/local/www/nginx-dist/
- index index.html
with simple template
# ansible_managed
encode_nginx
to nginx configuration
# cat /usr/local/etc/nginx/conf.d/default.conf
# Ansible managed
server
listen 8080;
server_name www.example.com;
location /
root /usr/local/www/nginx-dist/;
index index.html;
Details are available in the nginx role.
Thanks, nice example. But with Your solutionloop: " nginx_directories "get error(item=u'directory': u'sites', u'nginx_files': [u'file': u'mysite1.j2', u'file': u'mysite2.j2']) => "changed": false, "item": "directory": "sites", "nginx_files": ["file": "mysite1.j2", "file": "mysite2.j2"], "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'nginx_directories'"
– Kein
Mar 28 at 2:06
Right. The loop in the template should be "% for val in item.nginx_files %". I've fixed the answer. Sorry for the noise.
– Vladimir Botka
Mar 28 at 6:08
Thanks, but this doesn't works as expected: it's works only with latest element in listitem.directoryso I get as result only this two lines:include /etc/nginx/sites/mysite1.j2; include /etc/nginx/sites/mysite2.j2;
– Kein
Mar 28 at 7:55
1
@vladimir-botka, @olivier-clavel Thanks a lot for all of You! Just remove loop from task and add Your line(for val in nginx_directories | subelements('nginx_files'))in template and all works as expected.
– Kein
Mar 28 at 8:25
1
I just edited the answer with the final solution. It should be accepted by @VladimirBotka to be visible to all.
– Zeitounator
Mar 28 at 8:27
|
show 4 more comments
Remove the loop in you template task and in your template use
% for val in nginx_directories
include /etc/nginx/ val.0.directory / val.1.file ;
% endfor %
FWIW. There is an elegant solution with config_encoder_filters which encodes YAML data
my_nginx_vhost_config:
- server:
- listen 8080
- server_name www.example.com
- "location /":
- root /usr/local/www/nginx-dist/
- index index.html
with simple template
# ansible_managed
encode_nginx
to nginx configuration
# cat /usr/local/etc/nginx/conf.d/default.conf
# Ansible managed
server
listen 8080;
server_name www.example.com;
location /
root /usr/local/www/nginx-dist/;
index index.html;
Details are available in the nginx role.
Thanks, nice example. But with Your solutionloop: " nginx_directories "get error(item=u'directory': u'sites', u'nginx_files': [u'file': u'mysite1.j2', u'file': u'mysite2.j2']) => "changed": false, "item": "directory": "sites", "nginx_files": ["file": "mysite1.j2", "file": "mysite2.j2"], "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'nginx_directories'"
– Kein
Mar 28 at 2:06
Right. The loop in the template should be "% for val in item.nginx_files %". I've fixed the answer. Sorry for the noise.
– Vladimir Botka
Mar 28 at 6:08
Thanks, but this doesn't works as expected: it's works only with latest element in listitem.directoryso I get as result only this two lines:include /etc/nginx/sites/mysite1.j2; include /etc/nginx/sites/mysite2.j2;
– Kein
Mar 28 at 7:55
1
@vladimir-botka, @olivier-clavel Thanks a lot for all of You! Just remove loop from task and add Your line(for val in nginx_directories | subelements('nginx_files'))in template and all works as expected.
– Kein
Mar 28 at 8:25
1
I just edited the answer with the final solution. It should be accepted by @VladimirBotka to be visible to all.
– Zeitounator
Mar 28 at 8:27
|
show 4 more comments
Remove the loop in you template task and in your template use
% for val in nginx_directories
include /etc/nginx/ val.0.directory / val.1.file ;
% endfor %
FWIW. There is an elegant solution with config_encoder_filters which encodes YAML data
my_nginx_vhost_config:
- server:
- listen 8080
- server_name www.example.com
- "location /":
- root /usr/local/www/nginx-dist/
- index index.html
with simple template
# ansible_managed
encode_nginx
to nginx configuration
# cat /usr/local/etc/nginx/conf.d/default.conf
# Ansible managed
server
listen 8080;
server_name www.example.com;
location /
root /usr/local/www/nginx-dist/;
index index.html;
Details are available in the nginx role.
Remove the loop in you template task and in your template use
% for val in nginx_directories
include /etc/nginx/ val.0.directory / val.1.file ;
% endfor %
FWIW. There is an elegant solution with config_encoder_filters which encodes YAML data
my_nginx_vhost_config:
- server:
- listen 8080
- server_name www.example.com
- "location /":
- root /usr/local/www/nginx-dist/
- index index.html
with simple template
# ansible_managed
encode_nginx
to nginx configuration
# cat /usr/local/etc/nginx/conf.d/default.conf
# Ansible managed
server
listen 8080;
server_name www.example.com;
location /
root /usr/local/www/nginx-dist/;
index index.html;
Details are available in the nginx role.
edited Mar 28 at 8:37
Zeitounator
2,4531 gold badge4 silver badges19 bronze badges
2,4531 gold badge4 silver badges19 bronze badges
answered Mar 28 at 1:28
Vladimir BotkaVladimir Botka
6,6982 gold badges6 silver badges19 bronze badges
6,6982 gold badges6 silver badges19 bronze badges
Thanks, nice example. But with Your solutionloop: " nginx_directories "get error(item=u'directory': u'sites', u'nginx_files': [u'file': u'mysite1.j2', u'file': u'mysite2.j2']) => "changed": false, "item": "directory": "sites", "nginx_files": ["file": "mysite1.j2", "file": "mysite2.j2"], "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'nginx_directories'"
– Kein
Mar 28 at 2:06
Right. The loop in the template should be "% for val in item.nginx_files %". I've fixed the answer. Sorry for the noise.
– Vladimir Botka
Mar 28 at 6:08
Thanks, but this doesn't works as expected: it's works only with latest element in listitem.directoryso I get as result only this two lines:include /etc/nginx/sites/mysite1.j2; include /etc/nginx/sites/mysite2.j2;
– Kein
Mar 28 at 7:55
1
@vladimir-botka, @olivier-clavel Thanks a lot for all of You! Just remove loop from task and add Your line(for val in nginx_directories | subelements('nginx_files'))in template and all works as expected.
– Kein
Mar 28 at 8:25
1
I just edited the answer with the final solution. It should be accepted by @VladimirBotka to be visible to all.
– Zeitounator
Mar 28 at 8:27
|
show 4 more comments
Thanks, nice example. But with Your solutionloop: " nginx_directories "get error(item=u'directory': u'sites', u'nginx_files': [u'file': u'mysite1.j2', u'file': u'mysite2.j2']) => "changed": false, "item": "directory": "sites", "nginx_files": ["file": "mysite1.j2", "file": "mysite2.j2"], "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'nginx_directories'"
– Kein
Mar 28 at 2:06
Right. The loop in the template should be "% for val in item.nginx_files %". I've fixed the answer. Sorry for the noise.
– Vladimir Botka
Mar 28 at 6:08
Thanks, but this doesn't works as expected: it's works only with latest element in listitem.directoryso I get as result only this two lines:include /etc/nginx/sites/mysite1.j2; include /etc/nginx/sites/mysite2.j2;
– Kein
Mar 28 at 7:55
1
@vladimir-botka, @olivier-clavel Thanks a lot for all of You! Just remove loop from task and add Your line(for val in nginx_directories | subelements('nginx_files'))in template and all works as expected.
– Kein
Mar 28 at 8:25
1
I just edited the answer with the final solution. It should be accepted by @VladimirBotka to be visible to all.
– Zeitounator
Mar 28 at 8:27
Thanks, nice example. But with Your solution
loop: " nginx_directories " get error (item=u'directory': u'sites', u'nginx_files': [u'file': u'mysite1.j2', u'file': u'mysite2.j2']) => "changed": false, "item": "directory": "sites", "nginx_files": ["file": "mysite1.j2", "file": "mysite2.j2"], "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'nginx_directories'"– Kein
Mar 28 at 2:06
Thanks, nice example. But with Your solution
loop: " nginx_directories " get error (item=u'directory': u'sites', u'nginx_files': [u'file': u'mysite1.j2', u'file': u'mysite2.j2']) => "changed": false, "item": "directory": "sites", "nginx_files": ["file": "mysite1.j2", "file": "mysite2.j2"], "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'nginx_directories'"– Kein
Mar 28 at 2:06
Right. The loop in the template should be "% for val in item.nginx_files %". I've fixed the answer. Sorry for the noise.
– Vladimir Botka
Mar 28 at 6:08
Right. The loop in the template should be "% for val in item.nginx_files %". I've fixed the answer. Sorry for the noise.
– Vladimir Botka
Mar 28 at 6:08
Thanks, but this doesn't works as expected: it's works only with latest element in list
item.directory so I get as result only this two lines: include /etc/nginx/sites/mysite1.j2; include /etc/nginx/sites/mysite2.j2;– Kein
Mar 28 at 7:55
Thanks, but this doesn't works as expected: it's works only with latest element in list
item.directory so I get as result only this two lines: include /etc/nginx/sites/mysite1.j2; include /etc/nginx/sites/mysite2.j2;– Kein
Mar 28 at 7:55
1
1
@vladimir-botka, @olivier-clavel Thanks a lot for all of You! Just remove loop from task and add Your line
(for val in nginx_directories | subelements('nginx_files')) in template and all works as expected.– Kein
Mar 28 at 8:25
@vladimir-botka, @olivier-clavel Thanks a lot for all of You! Just remove loop from task and add Your line
(for val in nginx_directories | subelements('nginx_files')) in template and all works as expected.– Kein
Mar 28 at 8:25
1
1
I just edited the answer with the final solution. It should be accepted by @VladimirBotka to be visible to all.
– Zeitounator
Mar 28 at 8:27
I just edited the answer with the final solution. It should be accepted by @VladimirBotka to be visible to all.
– Zeitounator
Mar 28 at 8:27
|
show 4 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387719%2fhow-to-correctly-define-subelements-in-ansible-jinja2-templatewith-subelements%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown