ansible-template

Escape or differentiate between jinja template variables in Ansible and <service>.

岁酱吖の 提交于 2019-12-02 16:19:30
问题 History: We've been using Ansible to deploy our services and config files over the last few months and we've been making use of the Ansible variables. The variables get placed in our (config_name).yml.j2 files which allows us to easily make changes without having to hard code all of the config. For example in Ansibles group_vars we may have: metric_port_var: "9100" And the (config_name).yml.j2 will contain a line somewhat: EXPOSE_METRIC_PORT={{ metric_port_var}} When the config is deployed

ansible - delete unmanaged files from directory?

风格不统一 提交于 2019-12-02 15:29:18
I want to recursively copy over a directory and render all .j2 files in there as templates. For this I am currently using the following lines: - template: > src=/src/conf.d/{{ item }} dest=/dest/conf.d/{{ item|replace('.j2','') }} with_lines: find /src/conf.d/ -type f -printf "%P\n" Now I'm looking for a way to remove unmanaged files from this directory. For example if I remove a file/template from /src/conf.d/ I want Ansible to remove it from /dest/conf.d/ as well. Is there some way to do this? I tried fiddling around with rsync --delete , but there I got a problem with the templates which

Ansible iterate over hosts in inventory group set by variable

风格不统一 提交于 2019-12-02 10:34:06
I have the next snippet in my role template: upstream portal { {% set nodes = groups["my_dev_cluster"] %} {% for node in nodes %} ...do something with nodes... {% endfor %} } And it works well. But when I try to parametrize inventory group name like this: upstream portal { {% set nodes = groups["{{cluster_name}}"] %} {% for node in nodes %} ...do something with nodes... {% endfor %} } I get an exception like: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute '{{cluster_name}}'"} Here, cluster_name - is a simple string variable

Escape or differentiate between jinja template variables in Ansible and <service>.

送分小仙女□ 提交于 2019-12-02 07:43:29
History: We've been using Ansible to deploy our services and config files over the last few months and we've been making use of the Ansible variables. The variables get placed in our (config_name).yml.j2 files which allows us to easily make changes without having to hard code all of the config. For example in Ansibles group_vars we may have: metric_port_var: "9100" And the (config_name).yml.j2 will contain a line somewhat: EXPOSE_METRIC_PORT={{ metric_port_var}} When the config is deployed the config on the box is now: EXPOSE_METRIC_PORT=9100 Problem: Now we're deploying configuration for

Ansible and Jinja2 logic for loops

时间秒杀一切 提交于 2019-12-02 04:17:49
Variable file createuser : userslist: - da_cel_upload - da_tag_upload Ansible logic: - include_vars: group_vars/createuser - name: Create custom file /etc/ssh/shhd_config for user configuration and restart sshd service template: src=sshconfig.j2 dest=/etc/ssh/sshd_config with_items: '{{userslist}}' notify: restart ssh Contents of sshconfig.j2 : Match User {{ item }} {% raw %}ChrootDirectory /home/{% endraw %}{{ item }} X11Forwarding no AllowTcpForwarding no ForceCommand internal-sftp Output I get in /etc/ssh/sshd_config : Match User da_tag_upload ChrootDirectory /home/da_tag_upload

How to read a particular part of file in ansible

大城市里の小女人 提交于 2019-12-01 14:12:40
I have a file which has the following data ! multiply 4 and 5 multiply 5 and 6 ! add 3 to 4 add 8 to 4 ! sub 3 from 6 sub 9 from 5 ! ! div 6 by 2 div 8 by 1 I want to read only add commands from the file. Using lookup plugin I was able to read the data of the entire file. But I don't know how to read only the specific add commands from the file Here's the code that I've written. --- - name: Extracting the Add commands from the File hosts: 127.0.0.1 connection: local vars: contents: "{{lookup('file', 'file1.txt')}}" tasks: - name: Printing the Add commands of the File debug: var: contents I am

How to read a particular part of file in ansible

半世苍凉 提交于 2019-12-01 13:10:25
问题 I have a file which has the following data ! multiply 4 and 5 multiply 5 and 6 ! add 3 to 4 add 8 to 4 ! sub 3 from 6 sub 9 from 5 ! ! div 6 by 2 div 8 by 1 I want to read only add commands from the file. Using lookup plugin I was able to read the data of the entire file. But I don't know how to read only the specific add commands from the file Here's the code that I've written. --- - name: Extracting the Add commands from the File hosts: 127.0.0.1 connection: local vars: contents: "{{lookup(

I want to include another Jinja2 template in an Ansible context in Jinja2

谁说胖子不能爱 提交于 2019-11-30 04:37:11
问题 I have an Ansible playbook that sets a lot of variables. One the playbooks has this task: - name: create config file template: src: 'templates/main_config.j2' dest: "{{ tmp_dir }}/main_config.json" The template main_config.j2 writes strings that are defined as variables in the parent Ansible playbooks and tasks. I want to include another Jinja2 template based on a value of an Ansible variable. {% include "./templates/configurations.j2" %}, {% include "./templates/security.j2" %}, {% include '

How can I test jinja2 templates in ansible?

為{幸葍}努か 提交于 2019-11-29 20:20:40
Sometimes I need to test some jinja2 templates that I use in my ansible roles. What is the simplest way for doing this? For example, I have a template (test.j2): {% if users is defined and users %} {% for user in users %}{{ user }} {% endfor %} {% endif %} and vars (in group_vars/all): --- users: - Mike - Smith - Klara - Alex At this time exists 4 different variants: 1_Online (using https://cryptic-cliffs-32040.herokuapp.com/ ) Based on jinja2-live-parser code. 2_Interactive (using python and library jinja2, PyYaml) import yaml from jinja2 import Template >>> template = Template(""" ... {% if

How to use template module with different set of variables?

旧街凉风 提交于 2019-11-28 16:56:25
My use case is the following : I have a template file, and I would like to create 2 different files from that template, with the variables being filled by a different set of variables for each file. For example, lets say I want to template the file containing the line: mkdir -p {{myTemplateVariable}} I would like to find a proper way to get this variable filled by "File1" and "File2". Something like : - name: template test 1 template: src=myTemplateFile dest=result1 - name: template test 2 template: src=myTemplateFile dest=result2 where I could specify for the first templating that the