From 77b13caf40e4770639b22abb9bdd1b715bc1612c Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Thu, 27 Jul 2017 17:36:26 +0100 Subject: [PATCH 1/8] fixed generating single quote erlang config file --- .../config-encoder-macros/macros/erlang_encode_macro.j2 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/templates/config-encoder-macros/macros/erlang_encode_macro.j2 b/templates/config-encoder-macros/macros/erlang_encode_macro.j2 index c15825a..d094f73 100644 --- a/templates/config-encoder-macros/macros/erlang_encode_macro.j2 +++ b/templates/config-encoder-macros/macros/erlang_encode_macro.j2 @@ -51,7 +51,11 @@ {%- elif item is string %} {#- The item is a string -#} - {{ '"' ~ item ~ '"' }} + {%- if item[0] == ':' -%} + {{ item[1:] }} + {%- else -%} + {{ '"' ~ item ~ '"' }} + {%- endif -%} {%- else %} {#- The item is a list -#} From 7cb32e454270eeaaba0eac4e35ad0ec635f2ab23 Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Tue, 1 Aug 2017 10:36:06 +0100 Subject: [PATCH 2/8] updated task name string about Debian family replacing `rpm` with `apt` --- tasks/Debian/install.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/Debian/install.yml b/tasks/Debian/install.yml index 6bfabd7..298cc36 100644 --- a/tasks/Debian/install.yml +++ b/tasks/Debian/install.yml @@ -1,5 +1,5 @@ --- -- name: import erlang rpm key +- name: import erlang apt key apt_key: url: "https://packages.erlang-solutions.com/debian/erlang_solutions.asc" @@ -11,7 +11,7 @@ apt: name: erlang -- name: import rabbitmq rpm key +- name: import rabbitmq apt key apt_key: url: https://www.rabbitmq.com/rabbitmq-release-signing-key.asc From 042c2c91ce6c8c700c2eba741707b8eac73dd6c7 Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Tue, 1 Aug 2017 13:20:04 +0100 Subject: [PATCH 3/8] added .gitignore file --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13bbe5b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ + From 7c7b00d3bbd608bdd0aff63c64ff22cb8e05cea4 Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Tue, 1 Aug 2017 13:21:44 +0100 Subject: [PATCH 4/8] linted files. reorganized tasks. wip fixing cluster functionality --- defaults/main.yml | 1 + handlers/main.yml | 4 ++++ meta/main.yml | 1 + tasks/Debian/install.yml | 1 + tasks/RedHat/install.yml | 1 + tasks/cluster.yml | 9 +++++++-- tasks/configure-cluster.yml | 8 ++++++++ tasks/configure.yml | 11 +++++++++++ tasks/main.yml | 12 +++++++++--- tasks/node_start.yml | 10 ++++++++++ tasks/plugins.yml | 1 + tasks/rabbitmq.yml | 23 ----------------------- tasks/users.yml | 1 + tasks/vhosts.yml | 1 + 14 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 handlers/main.yml create mode 100644 tasks/node_start.yml delete mode 100644 tasks/rabbitmq.yml diff --git a/defaults/main.yml b/defaults/main.yml index 959b785..37496a3 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,4 +1,5 @@ --- + rabbitmq_cluster: False rabbitmq_cluster_master: "rabbit@{{ hostvars[ansible_play_hosts.0].ansible_hostname }}" diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..0152705 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,4 @@ +--- + +- name: rabbitmq restart + service: name=rabbitmq-server state=restarted diff --git a/meta/main.yml b/meta/main.yml index 27bd567..61fc181 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,4 +1,5 @@ --- + galaxy_info: author: jasonroyle description: RabbitMQ diff --git a/tasks/Debian/install.yml b/tasks/Debian/install.yml index 298cc36..97b9efb 100644 --- a/tasks/Debian/install.yml +++ b/tasks/Debian/install.yml @@ -1,4 +1,5 @@ --- + - name: import erlang apt key apt_key: url: "https://packages.erlang-solutions.com/debian/erlang_solutions.asc" diff --git a/tasks/RedHat/install.yml b/tasks/RedHat/install.yml index 698e79a..0f57308 100644 --- a/tasks/RedHat/install.yml +++ b/tasks/RedHat/install.yml @@ -1,4 +1,5 @@ --- + - name: import erlang rpm key rpm_key: key: https://packages.erlang-solutions.com/rpm/erlang_solutions.asc diff --git a/tasks/cluster.yml b/tasks/cluster.yml index 6965682..d755a0b 100644 --- a/tasks/cluster.yml +++ b/tasks/cluster.yml @@ -1,7 +1,11 @@ --- + - name: stop rabbitmq app shell: rabbitmqctl stop_app +- name: reset rabbitmq + shell: rabbitmqctl reset + - name: join rabbitmq cluster shell: "rabbitmqctl join_cluster {{ rabbitmq_cluster_master }}" register: rabbitmq_output @@ -11,5 +15,6 @@ fail: msg="Unable to join the cluster." when: ("'already_member' not in rabbitmq_output.stderr") and rabbitmq_output.rc != 0 -- name: start rabbitmq app - shell: rabbitmqctl start_app +#- name: start rabbitmq app +# shell: rabbitmqctl start_app +# when: rabbitmq_cluster and rabbitmq_nodename == rabbitmq_cluster_master diff --git a/tasks/configure-cluster.yml b/tasks/configure-cluster.yml index d01ccb0..bfdc0c4 100644 --- a/tasks/configure-cluster.yml +++ b/tasks/configure-cluster.yml @@ -1,4 +1,5 @@ --- + - name: add rabbitmq cluster hosts lineinfile: dest: /etc/hosts @@ -12,3 +13,10 @@ owner: rabbitmq group: rabbitmq mode: 0400 + notify: + - rabbitmq restart + + + +- include: cluster.yml + when: rabbitmq_cluster and rabbitmq_nodename != rabbitmq_cluster_master diff --git a/tasks/configure.yml b/tasks/configure.yml index 4cd2654..48ebb9a 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -1,6 +1,17 @@ --- + - name: configure rabbitmq template: src: rabbitmq.config.j2 dest: /etc/rabbitmq/rabbitmq.config when: rabbitmq_config is defined + notify: + - rabbitmq restart + +- include: node_start.yml + +- include: vhosts.yml + +- include: users.yml + +- include: plugins.yml diff --git a/tasks/main.yml b/tasks/main.yml index b17d621..2613cf5 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,4 +1,10 @@ --- -- include: rabbitmq.yml - tags: - - rabbitmq + +- name: include rabbitmq installation tasks + include: "{{ ansible_os_family }}/install.yml" + +- include: configure.yml + +- include: configure-cluster.yml + when: rabbitmq_cluster + diff --git a/tasks/node_start.yml b/tasks/node_start.yml new file mode 100644 index 0000000..81f1742 --- /dev/null +++ b/tasks/node_start.yml @@ -0,0 +1,10 @@ +--- + +- name: start rabbitmq server + service: + name: rabbitmq-server + enabled: true + state: started + +- name: start rabbitmq app + shell: rabbitmqctl start_app diff --git a/tasks/plugins.yml b/tasks/plugins.yml index 6a0cbf9..77119a3 100644 --- a/tasks/plugins.yml +++ b/tasks/plugins.yml @@ -1,4 +1,5 @@ --- + - name: install rabbitmq plugins get_url: url: "{{ item.url }}" diff --git a/tasks/rabbitmq.yml b/tasks/rabbitmq.yml deleted file mode 100644 index eec2f0d..0000000 --- a/tasks/rabbitmq.yml +++ /dev/null @@ -1,23 +0,0 @@ ---- -- name: include rabbitmq installation tasks - include: "{{ ansible_os_family }}/install.yml" - -- include: configure.yml - -- include: configure-cluster.yml - when: rabbitmq_cluster - -- name: start rabbitmq server - service: - name: rabbitmq-server - enabled: True - state: started - -- include: vhosts.yml - -- include: users.yml - -- include: plugins.yml - -- include: cluster.yml - when: rabbitmq_cluster and rabbitmq_nodename != rabbitmq_cluster_master diff --git a/tasks/users.yml b/tasks/users.yml index d799b1d..81ecf97 100644 --- a/tasks/users.yml +++ b/tasks/users.yml @@ -1,4 +1,5 @@ --- + - name: remove rabbitmq users rabbitmq_user: user: "{{ item }}" diff --git a/tasks/vhosts.yml b/tasks/vhosts.yml index 91d8709..e69b562 100644 --- a/tasks/vhosts.yml +++ b/tasks/vhosts.yml @@ -1,4 +1,5 @@ --- + - name: remove rabbitmq virtual hosts rabbitmq_vhost: name: "{{ item }}" From 470b0e8815e629a80050d0a5ae393e189bad226e Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Tue, 1 Aug 2017 13:29:24 +0100 Subject: [PATCH 5/8] added dependency for config_encoder_filter and ubuntu xenial version compatibility --- meta/main.yml | 6 +- templates/config-encoder-macros/.gitignore | 0 templates/config-encoder-macros/LICENSE.md | 20 - templates/config-encoder-macros/README.md | 672 ------------------ templates/config-encoder-macros/hiera.yaml | 12 - templates/config-encoder-macros/hosts | 1 - .../macros/apache_encode_macro.erb | 172 ----- .../macros/apache_encode_macro.j2 | 157 ---- .../macros/erlang_encode_macro.erb | 82 --- .../macros/erlang_encode_macro.j2 | 96 --- .../macros/ini_encode_macro.erb | 53 -- .../macros/ini_encode_macro.j2 | 58 -- .../macros/json_encode_macro.erb | 87 --- .../macros/json_encode_macro.j2 | 93 --- .../macros/logstash_encode_macro.erb | 4 - .../macros/logstash_encode_macro.j2 | 126 ---- .../macros/toml_encode_macro.erb | 175 ----- .../macros/toml_encode_macro.j2 | 148 ---- .../macros/xml_encode_macro.erb | 103 --- .../macros/xml_encode_macro.j2 | 80 --- .../macros/yaml_encode_macro.erb | 84 --- .../macros/yaml_encode_macro.j2 | 76 -- .../config-encoder-macros/puppet_apply.sh | 13 - templates/config-encoder-macros/site.pp | 71 -- templates/config-encoder-macros/site.yaml | 109 --- .../templates/test.apache.erb | 11 - .../templates/test.apache.j2 | 9 - .../templates/test.erlang.erb | 11 - .../templates/test.erlang.j2 | 9 - .../templates/test.ini.erb | 10 - .../templates/test.ini.j2 | 9 - .../templates/test.ini_simple.erb | 12 - .../templates/test.ini_simple.j2 | 9 - .../templates/test.json.erb | 5 - .../templates/test.json.j2 | 3 - .../templates/test.logstash.erb | 11 - .../templates/test.logstash.j2 | 9 - .../templates/test.toml.erb | 11 - .../templates/test.toml.j2 | 9 - .../templates/test.xml.erb | 15 - .../templates/test.xml.j2 | 13 - .../templates/test.yaml.erb | 11 - .../templates/test.yaml.j2 | 9 - .../vars/apache_test.yaml | 64 -- .../vars/erlang_test.yaml | 14 - .../config-encoder-macros/vars/ini_test.yaml | 15 - .../config-encoder-macros/vars/json_test.yaml | 16 - .../vars/logstash_test.yaml | 33 - .../config-encoder-macros/vars/toml_test.yaml | 33 - .../config-encoder-macros/vars/xml_test.yaml | 15 - .../config-encoder-macros/vars/yaml_test.yaml | 16 - .../config-encoder-macros/yaml_converter.py | 75 -- .../config-encoder-macros/yaml_converter.rb | 121 ---- templates/rabbitmq.config.j2 | 10 +- 54 files changed, 7 insertions(+), 3079 deletions(-) delete mode 100644 templates/config-encoder-macros/.gitignore delete mode 100644 templates/config-encoder-macros/LICENSE.md delete mode 100644 templates/config-encoder-macros/README.md delete mode 100644 templates/config-encoder-macros/hiera.yaml delete mode 100644 templates/config-encoder-macros/hosts delete mode 100644 templates/config-encoder-macros/macros/apache_encode_macro.erb delete mode 100644 templates/config-encoder-macros/macros/apache_encode_macro.j2 delete mode 100644 templates/config-encoder-macros/macros/erlang_encode_macro.erb delete mode 100644 templates/config-encoder-macros/macros/erlang_encode_macro.j2 delete mode 100644 templates/config-encoder-macros/macros/ini_encode_macro.erb delete mode 100644 templates/config-encoder-macros/macros/ini_encode_macro.j2 delete mode 100644 templates/config-encoder-macros/macros/json_encode_macro.erb delete mode 100644 templates/config-encoder-macros/macros/json_encode_macro.j2 delete mode 100644 templates/config-encoder-macros/macros/logstash_encode_macro.erb delete mode 100644 templates/config-encoder-macros/macros/logstash_encode_macro.j2 delete mode 100644 templates/config-encoder-macros/macros/toml_encode_macro.erb delete mode 100644 templates/config-encoder-macros/macros/toml_encode_macro.j2 delete mode 100644 templates/config-encoder-macros/macros/xml_encode_macro.erb delete mode 100644 templates/config-encoder-macros/macros/xml_encode_macro.j2 delete mode 100644 templates/config-encoder-macros/macros/yaml_encode_macro.erb delete mode 100644 templates/config-encoder-macros/macros/yaml_encode_macro.j2 delete mode 100755 templates/config-encoder-macros/puppet_apply.sh delete mode 100644 templates/config-encoder-macros/site.pp delete mode 100644 templates/config-encoder-macros/site.yaml delete mode 100644 templates/config-encoder-macros/templates/test.apache.erb delete mode 100644 templates/config-encoder-macros/templates/test.apache.j2 delete mode 100644 templates/config-encoder-macros/templates/test.erlang.erb delete mode 100644 templates/config-encoder-macros/templates/test.erlang.j2 delete mode 100644 templates/config-encoder-macros/templates/test.ini.erb delete mode 100644 templates/config-encoder-macros/templates/test.ini.j2 delete mode 100644 templates/config-encoder-macros/templates/test.ini_simple.erb delete mode 100644 templates/config-encoder-macros/templates/test.ini_simple.j2 delete mode 100644 templates/config-encoder-macros/templates/test.json.erb delete mode 100644 templates/config-encoder-macros/templates/test.json.j2 delete mode 100644 templates/config-encoder-macros/templates/test.logstash.erb delete mode 100644 templates/config-encoder-macros/templates/test.logstash.j2 delete mode 100644 templates/config-encoder-macros/templates/test.toml.erb delete mode 100644 templates/config-encoder-macros/templates/test.toml.j2 delete mode 100644 templates/config-encoder-macros/templates/test.xml.erb delete mode 100644 templates/config-encoder-macros/templates/test.xml.j2 delete mode 100644 templates/config-encoder-macros/templates/test.yaml.erb delete mode 100644 templates/config-encoder-macros/templates/test.yaml.j2 delete mode 100644 templates/config-encoder-macros/vars/apache_test.yaml delete mode 100644 templates/config-encoder-macros/vars/erlang_test.yaml delete mode 100644 templates/config-encoder-macros/vars/ini_test.yaml delete mode 100644 templates/config-encoder-macros/vars/json_test.yaml delete mode 100644 templates/config-encoder-macros/vars/logstash_test.yaml delete mode 100644 templates/config-encoder-macros/vars/toml_test.yaml delete mode 100644 templates/config-encoder-macros/vars/xml_test.yaml delete mode 100644 templates/config-encoder-macros/vars/yaml_test.yaml delete mode 100755 templates/config-encoder-macros/yaml_converter.py delete mode 100755 templates/config-encoder-macros/yaml_converter.rb diff --git a/meta/main.yml b/meta/main.yml index 61fc181..9dfa26e 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -14,10 +14,14 @@ galaxy_info: - name: Ubuntu versions: - trusty + - xenial galaxy_tags: - rabbitmq - amqp - plugin - cluster -dependencies: [] +dependencies: + - src: jtyr.config_encoder_filters + version: v1.3 + diff --git a/templates/config-encoder-macros/.gitignore b/templates/config-encoder-macros/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/templates/config-encoder-macros/LICENSE.md b/templates/config-encoder-macros/LICENSE.md deleted file mode 100644 index ce081ff..0000000 --- a/templates/config-encoder-macros/LICENSE.md +++ /dev/null @@ -1,20 +0,0 @@ -The MIT License (MIT) - -Copyright (c) Jiri Tyr 2015 - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/templates/config-encoder-macros/README.md b/templates/config-encoder-macros/README.md deleted file mode 100644 index 50253ac..0000000 --- a/templates/config-encoder-macros/README.md +++ /dev/null @@ -1,672 +0,0 @@ -Config Encoder Macros -===================== - -Set of Jinja2 and ERB macros which help to encode Python and Ruby data structure -into a different file format. - - ---- - -* [Motivation](#motivation) -* [Supported formats](#supported-formats) -* Examples - * [Ansible example](#ansible-example) - * [Puppet example](#puppet-example) -* [Macros parameters](#macros-parameters) -* [Limitations](#limitations) -* Programming support - * [Python example](#python-example) - * [Ruby example](#ruby-example) -* [License](#license) -* [Author](#author) - ---- - - -Motivation ----------- - -The motivation to create these macros was to be able to create an universal -description of configuration files in [Ansible](http://ansible.com). Ansible and -Puppet is using YAML format as the main data input. This format allows to -describe almost any structure of a configuration file. Ansible resp. Puppet -converts the YAML data into a Python resp. Ruby data structure using primitive -data types (e.g. `dict`, `list`, `string`, `number`, `boolean`, ...). That data -structure is then used in Jinja2 and ERB template files to produce the final -configuration file. The encoder macros are used to convert the Python or Ruby -data structure to another format. - - -Supported formats ------------------ - -- Apache config format -- Erlang config format -- INI -- JSON -- Logstash -- TOML -- XML -- YAML - - -Ansible example ---------------- - -Clone this repo into the `templates` directory in the playbook location: - -``` -$ git clone https://github.com/picotrading/config-encoder-macros ./templates/encoder -``` - -Then use the macros in your playbook (e.g. `site.yaml`) like this: - -``` -# Playbook example -- name: Play to test the encoder macros - hosts: all - vars: - ini_data: - var1: val1 - var2: val2 - section1: - aaa: asdf - bbb: 123 - ccc: 'true' - section2: - ddd: asdfasd - eee: 1234 - fff: 'false' - tasks: - - name: Create test.ini file - template: - src: templates/test.ini.j2 - dest: /tmp/test.ini -``` - -Where the `templates/test.ini.j2` contains: - -``` -# -# This file is managed by Ansible. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -{% from "templates/encoder/macros/ini_encode_macro.j2" import ini_encode with context -%} - -{{ ini_encode(ini_data) }} -``` - -And if you call the playbook: - -``` -$ ansible-playbook -i hosts ./site.yaml -``` - -You get the following in the `/tmp/test.ini`: - -``` -# -# This file is managed by Ansible. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -var1=val1 -var2=val2 - -[section1] -aaa=asdf -bbb=123 -ccc=true - -[section2] -ddd=asdfasd -eee=1234 -fff=false -``` - -Look into the `site.yaml` and `templates/*.j2` for more examples. - - -Puppet example --------------- - -Clone this repo to some directory (e.g. `/etc/puppet/encoder`): - -``` -$ git clone https://github.com/picotrading/config-encoder-macros /etc/puppet/encoder -``` - -Then use the macros from your Puppet manifest like this: - -``` -# Load the configuration from YAML file -$ini_data = hiera('ini_data') - -file { '/tmp/test.ini' : - ensure => present, - content => template('test.ini.erb'), -} -``` - -Where the Hiera YAML file contains: - -``` ---- - -ini_data: - var1: val1 - var2: val2 - section1: - aaa: - - asdf - - zxcv - bbb: 123 - ccc: 'true' - section2: - ddd: asdfasd - eee: 1234 - fff: 'false' -``` - -And the `templates/test.ini.erb` contains: - -``` -# -# This file is managed by Puppet. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -<%- - item = @ini_data || (ini_data.kind_of?(String) ? eval(ini_data) : ini_data) --%> -<%= ERB.new(IO.read('/etc/puppet/macros/ini_encode_macro.erb'), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> -``` - -And if you run Puppet you should get the following in the `/tmp/test.ini`: - -``` -# -# This file is managed by Puppet. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -var1=val1 -var2=val2 - -[section1] -aaa=asdf -bbb=123 -ccc=true - -[section2] -ddd=asdfasd -eee=1234 -fff=false -``` - -Look into the `site.pp`, `templates/*.erb` and `puppet_apply.sh` for more -examples. - - -Macros parameters ------------------ - -The first parameter passed to the macro is always the data structure. Any -following parameter should be addressed by a keyword (e.g. `indent=" "` where -`indent` is the keyword). - - -### `apache_encode()` - -- `item` - - > Variable holding the input data for the macro. - -- `convert_bools=false` - - > Indicates whether Boolean values presented as a string should be converted - > to a real Booblean value. For example `var1: 'True'` would be represented - > as a string but using the `convert_bools=true` will convert it to a Boolean - > like it would be defined like this: `var1: true`. - -- `convert_nums=false` - - > Indicates whether number presented as a string should be converted to - > number. For example `var1: '123'` would be represented as a string but - > using the `convert_nums=true` will convert it to a number like it would - > be defined like this: `var1: 123`. You can also use the YAML type casting - > to convert string to number (e.g. `!!int "1234"`, `!!float "3.14"`). - -- `indent=" "` - - > Defines the indentation unit. - -- `type="section"` - - > Defines the type of the `item` in the recursive macro calls. It's used only - > internally in the macro. - -- `level=0` - - > Indicates the initial level of the indentation. Value `0` starts indenting - > from the beginning of the line. Setting the value to higher than `0` - > indents the content by `indent * level`. - -- `macro_path='macros/apache_encode_macro.erb'` - - > Relative or absolute path to the macro. It's used only in ERB macros. - - -### `erlang_encode()` - -- `item` - - > Variable holding the input data for the macro. - -- `convert_bools=false` - - > Indicates whether Boolean values presented as a string should be converted - > to a real Booblean value. For example `var1: 'True'` would be represented - > as a string but using the `convert_bools=true` will convert it to a Boolean - > like it would be defined like this: `var1: true`. - -- `convert_nums=false` - - > Indicates whether number presented as a string should be converted to - > number. For example `var1: '123'` would be represented as a string but - > using the `convert_nums=true` will convert it to a number like it would - > be defined like this: `var1: 123`. You can also use the YAML type casting - > to convert string to number (e.g. `!!int "1234"`, `!!float "3.14"`). - -- `first=[]` - - > Indicates whether the item being processed is actually a first item. It's - > used only internally in the macro. - -- `indent=" "` - - > Defines the indentation unit. - -- `level=0` - - > Indicates the initial level of the indentation. Value `0` starts indenting - > from the beginning of the line. Setting the value to higher than `0` - > indents the content by `indent * level`. - -- `macro_path='macros/erlang_encode_macro.erb'` - - > Relative or absolute path to the macro. It's used only in ERB macros. - - -### `ini_encode()` -- `item` - - > Variable holding the input data for the macro. - -- `delimiter="="` - - > Sign separating the *property* and the *value*. By default it's set to `'='` - > but it can also be set to `' = '`. - -- `first=[]` - - > Indicates whether the item being processed is actually a first item. It's - > used only internally in the macro. - -- `macro_path='macros/ini_encode_macro.erb'` - - > Relative or absolute path to the macro. It's used only in ERB macros. - -- `quote=""` - - > Allows to set the *value* quoting. Use `quote="'"` or `quote='"'`. - -- `section_is_comment=false` - - > Prints sections as comments. - -- `ucase_prop=false` - - > Indicates whether the *property* should be made uppercase. - - -### `json_encode()` -- `item` - - > Variable holding the input data for the macro. - -- `convert_bools=false` - - > Indicates whether Boolean values presented as a string should be converted - > to a real Booblean value. For example `var1: 'True'` would be represented - > as a string but using the `convert_bools=true` will convert it to a Boolean - > like it would be defined like this: `var1: true`. - -- `convert_nums=false` - - > Indicates whether number presented as a string should be converted to - > number. For example `var1: '123'` would be represented as a string but - > using the `convert_nums=true` will convert it to a number like it would - > be defined like this: `var1: 123`. You can also use the YAML type casting - > to convert string to number (e.g. `!!int "1234"`, `!!float "3.14"`). - -- `indent=" "` - - > Defines the indentation unit. - -- `level=0` - - > Indicates the initial level of the indentation. Value `0` starts indenting - > from the beginning of the line. Setting the value to higher than `0` - > indents the content by `indent * level`. - -- `macro_path='macros/json_encode_macro.erb'` - - > Relative or absolute path to the macro. It's used only in ERB macros. - - -### `logstash_encode()` - -- `item` - - > Variable holding the input data for the macro. - -- `convert_bools=false` - - > Indicates whether Boolean values presented as a string should be converted - > to a real Booblean value. For example `var1: 'True'` would be represented - > as a string but using the `convert_bools=true` will convert it to a Boolean - > like it would be defined like this: `var1: true`. You can also use the YAML - > type casting by using `!!bool` in front of your value (e.g. - > `!!bool "true"`). - -- `convert_nums=false` - - > Indicates whether number presented as a string should be converted to - > number. For example `var1: '123'` would be represented as a string but - > using the `convert_nums=true` will convert it to a number like it would - > be defined like this: `var1: 123`. You can also use the YAML type casting - > to convert string to number (e.g. `!!int "1234"`, `!!float "3.14"`). - -- `indent=" "` - - > Defines the indentation unit. - -- `level=0` - - > Indicates the initial level of the indentation. Value `0` starts indenting - > from the beginning of the line. Setting the value to higher than `0` - > indents the content by `indent * level`. - -- `prevtype=""` - - > Defines the type of the previous item in the recursive macro calls. It's used - > only internally in the macro. - - -### `toml_encode()` - -- `item` - - > Variable holding the input data for the macro. - -- `convert_bools=false` - - > Indicates whether Boolean values presented as a string should be converted - > to a real Booblean value. For example `var1: 'True'` would be represented - > as a string but using the `convert_bools=true` will convert it to a Boolean - > like it would be defined like this: `var1: true`. You can also use the YAML - > type casting by using `!!bool` in front of your value (e.g. - > `!!bool "true"`). - -- `convert_nums=false` - - > Indicates whether number presented as a string should be converted to - > number. For example `var1: '123'` would be represented as a string but - > using the `convert_nums=true` will convert it to a number like it would - > be defined like this: `var1: 123`. You can also use the YAML type casting - > to convert string to number (e.g. `!!int "1234"`, `!!float "3.14"`). - -- `first=[]` - - > Indicates whether the item being processed is actually a first item. It's - > used only internally in the macro. - -- `indent=" "` - - > Defines the indentation unit. - -- `level=0` - - > Indicates the initial level of the indentation. Value `0` starts indenting - > from the beginning of the line. Setting the value to higher than `0` - > indents the content by `indent * level`. - -- `macro_path='macros/toml_encode_macro.erb'` - - > Relative or absolute path to the macro. It's used only in ERB macros. - -- `prevkey=""` - - > Defines the name of the previous key in the recursive macro calls. It's used - > only internally in the macro. - -- `prevtype=""` - - > Defines the type of the previous item in the recursive macro calls. It's used - > only internally in the macro. - -- `quote='"'` - - > Allows to set the *value* quoting. Use `quote="'"` or `quote='"'`. - - -### `xml_encode()` -- `item` - - > Variable holding the input data for the macro. - -- `first=[]` - - > Indicates whether the item being processed is actually a first item. It's - > used only internally in the macro. - -- `indent=" "` - - > Defines the indentation unit. - -- `level=0` - - > Indicates the initial level of the indentation. Value `0` starts indenting - > from the beginning of the line. Setting the value to higher than `0` - > indents the content by `indent * level`. - -- `macro_path='macros/xml_encode_macro.erb'` - - > Relative or absolute path to the macro. It's used only in ERB macros. - -- `prevkey=none` - - > Defines the name of the previous key in the recursive macro calls. It's used - > only internally in the macro. - - -### `yaml_encode()` -- `item` - - > Variable holding the input data for the macro. - -- `convert_bools=false` - - > Indicates whether Boolean values presented as a string should be converted - > to a real Booblean value. For example `var1: 'True'` would be represented - > as a string but using the `convert_bools=true` will convert it to a Boolean - > like it would be defined like this: `var1: true`. - -- `convert_nums=false` - - > Indicates whether number presented as a string should be converted to - > number. For example `var1: '123'` would be represented as a string but - > using the `convert_nums=true` will convert it to a number like it would - > be defined like this: `var1: 123`. You can also use the YAML type casting - > to convert string to number (e.g. `!!int "1234"`, `!!float "3.14"`). - -- `indent=" "` - - > Defines the indentation unit. - -- `level=0` - - > Indicates the initial level of the indentation. Value `0` starts indenting - > from the beginning of the line. Setting the value to higher than `0` - > indents the content by `indent * level`. - -- `macro_path='macros/yaml_encode_macro.erb'` - - > Relative or absolute path to the macro. It's used only in ERB macros. - -- `quote='"'` - - > Allows to set the *value* quoting. Use `quote="'"` or `quote='"'`. - -- `skip_indent=false` - - > Indicates whether the indentation should be skipped for the specific item. - > It's used only internally in the macro. - - -Limitations ------------ - -### Erlang, JSON, TOML and YAML macros - -In these type-sensitive file formats, the Boolean values represented as string -(`"true"`, `"True"`, `"false"` and `"False"`) are automatically converted to -Boolean value (`true` and `false`). The reason for this behavior is that if you -want to pass a Boolean variable in Ansible, you have to actually quote it (e.g. -`var1: "{{ my_boolean_var }}"`). Once the variable is quoted, it's not Boolean -any more. If you want to use Boolean as a string in your final configuration -file, try to use the value with a space (e.g. `' true'`). - - -### INI macro - -This macro doesn't respect the order in which the data was defined. It probably -need to be fixed as some config files might allow to overwrite previous -definitions. - - -### XML macro - -YAML doesn't allow to fully map XML data with attributes. This is why XML -attributes must be defined as part of the element name (e.g. `'elem attr1="val1" -attr2="val2"': Value of the element`). This is also the reason why elements of -the same name can only have the same set of attributes. For example the following YAML -input data: - -``` ---- -root: - 'elem attr1="val1" attr2="val2"': - - element value1 - - element value2 -``` - -Will produce the following XML file (`elem` has the same set of attributes): - -``` - - element value1 - element value2 - -``` - -Another limitation of the XML macro is that attributes can not have value -derivated from a Jinja2 variable in the Ansible context. This macro also doesn't -allow to set order of the elements so it's suitable only for order-insensitive -XML files. - - -Python example --------------- - -Although the macros are intended to be used primarily in Ansible, they can also -be used directly from Python: - -``` -from jinja2 import Template -from yaml import load -import sys - -# Load the YAML data to Python data structure -yaml_fh = open('vars/ini_test.yaml') -yaml_struct = load(yaml_fh) -yaml_fh.close() - -# Take only the content of the ini_data variable -yaml_struct = yaml_struct['ini_data'] - -# Read Jinja2 template as text -template_fh = open('macros/ini_encode_macro.j2') -template_text = template_fh.read() -template_fh.close() - -# Create Jinja2 template object -t = Template(template_text) -# Convert the YAML data to INI format -output = t.module.ini_encode(yaml_struct).encode('utf8') - -# Print the result -sys.stdout.write(output) -``` - -Or you can use the `yaml_converter.py`: - -``` -$ ./yaml_converter.py -f ini -v ini_data -y ./vars/ini_test.yaml -``` - - -Ruby example ------------- - -``` -require 'erb' -require 'ostruct' -require 'yaml' - -# Helper variables -yaml_file = './vars/ini_test.yaml' -var = 'ini_data' - -# Define variables used in the macro -item = YAML.load_file(yaml_file)[var] -macro_path = './macros/ini_encode_macro.erb' - -# Convert the YAML data to the final format -binding = OpenStruct.new.send(:binding) -print ERB.new(IO.read(macro_path), nil, '-', '_erbout0').result(binding) -``` - -Or you can use the `yaml_converter.rb`: - -``` -$ ./yaml_converter.rb -f ini -v ini_data -y ./vars/ini_test.yaml -``` - - -License -------- - -MIT - - -Author ------- - -Jiri Tyr diff --git a/templates/config-encoder-macros/hiera.yaml b/templates/config-encoder-macros/hiera.yaml deleted file mode 100644 index 6c4478b..0000000 --- a/templates/config-encoder-macros/hiera.yaml +++ /dev/null @@ -1,12 +0,0 @@ -:backends: - - yaml -:hierarchy: - - apache_test - - erlang_test - - ini_test - - json_test - - toml_test - - xml_test - - yaml_test -:yaml: - :datadir: ./vars diff --git a/templates/config-encoder-macros/hosts b/templates/config-encoder-macros/hosts deleted file mode 100644 index 8635629..0000000 --- a/templates/config-encoder-macros/hosts +++ /dev/null @@ -1 +0,0 @@ -localhost ansible_python_interpreter=/usr/bin/python2 ansible_connection=local diff --git a/templates/config-encoder-macros/macros/apache_encode_macro.erb b/templates/config-encoder-macros/macros/apache_encode_macro.erb deleted file mode 100644 index 1ad36fc..0000000 --- a/templates/config-encoder-macros/macros/apache_encode_macro.erb +++ /dev/null @@ -1,172 +0,0 @@ -<%-# - # ERB macro which converts Ruby data structure to Apache format - #-%> -<%- - _item ||= Array.new - _type ||= Array.new - _is_empty ||= Array.new - convert_bools ||= false - convert_nums ||= false - indent ||= ' ' - level ||= 0 - macro_path ||= 'macros/apache_encode_macro.erb' - quote_all_nums ||= false - quote_all_strings ||= false - type ||= 'sections' --%> -<%- if type == 'sections' -%> - <%- item['content'].each do |c| -%> - <%-# First check if this section has options -%> - <%- if c.include?('options') -%> - <%- - _item.push(item) - _type.push(type) - item = c['options'] - type = 'options' - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - item = _item.pop() - type = _type.pop() - level = level-1 - -%> - <%- end -%> - <%- is_empty = [] -%> - <%-# Check if this section has some sub-sections -%> - <%- if c.include?('sections') -%> - <%- c['sections'].each do |s| -%> - <%-# Check for empty sub-sections -%> - <%- s['content'].each do |i| -%> - <%- if (i.include?('options') and i['options'].length > 0) or - (i.include?('sections') and i['sections'].length > 0) -%> - <%- is_empty.push(1) -%> - <%- end -%> - <%- end -%> - <%- if is_empty.length > 0 -%> - <%--%><%= indent * level + '<' + s['name'] -%> - <%- if s.include?('param') -%> - <%--%><%= ' ' -%> - <%- - _item.push(item) - _type.push(type) - item = s['param'] - type = 'value' - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - item = _item.pop() - type = _type.pop() - level = level-1 - -%> - <%- end -%> - <%--%><%= ">\n" -%> - <%- - _item.push(item) - _type.push(type) - item = s - type = 'sections' - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - item = _item.pop() - type = _type.pop() - level = level-1 - -%> - <%--%><%= indent * level + '\n" -%> - <%- if c != item['content'].last -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- end -%> - <%- end -%> - <%- end -%> - <%- if c != item['content'].last and - ( - c.include?('options') and c['options'].length > 0 or - ( - c.include?('sections') and c['sections'].length > 0 and - is_empty.length > 0 - ) - ) - -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- end -%> -<%- elsif type == 'options' -%> - <%- item.each do |o| -%> - <%- o.sort.each do |key, val| -%> - <%--%><%= indent * (level-1) + key + ' ' -%> - <%- - _item.push(item) - _type.push(type) - item = val - type = 'value' - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - item = _item.pop() - type = _type.pop() - level = level-1 - -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- end -%> -<%- elsif type == 'value' -%> - <%- if item.kind_of?(TrueClass) or item.kind_of?(FalseClass) or - convert_bools and ['true', 'True', 'false', 'False'].include?(item) -%> - <%-# Value is a boolean -%> - <%--%><%= item.to_s.downcase -%> - <%- elsif item.kind_of?(Numeric) or - (convert_nums and ( - item.to_i.to_s == item or - item.to_f.to_s == item)) or - (convert_bools and ['true', 'True', 'false', 'False'].include?(item)) or - item.kind_of?(TrueClass) or item.kind_of?(FalseClass) -%> - <%-# Value is a number -%> - <%- if quote_all_nums -%> - <%--%><%= '"' + item.to_s + '"' -%> - <%- else -%> - <%--%><%= item -%> - <%- end -%> - <%- elsif item.kind_of?(String) -%> - <%-# Value is a string -%> - <%- if quote_all_strings or - item.include?(' ') or - item.include?("\t") or - item.include?("\n") or - item.include?("\r") or - item == '' -%> - <%--%><%= '"' + item.to_s.gsub('\\', '\\\\').gsub('"', '\\"') + '"' -%> - <%- else -%> - <%--%><%= item -%> - <%- end -%> - <%- elsif ! item.kind_of?(Hash) -%> - <%-# Value is an array -%> - <%- item.each do |v| -%> - <%- - _item.push(item) - _type.push(type) - item = v - type = 'value' - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - item = _item.pop() - type = _type.pop() - level = level-1 - -%> - <%- if v != item.last -%> - <%--%><%= ' ' -%> - <%- end -%> - <%- end -%> - <%- end -%> -<%- end -%> diff --git a/templates/config-encoder-macros/macros/apache_encode_macro.j2 b/templates/config-encoder-macros/macros/apache_encode_macro.j2 deleted file mode 100644 index 4f56d21..0000000 --- a/templates/config-encoder-macros/macros/apache_encode_macro.j2 +++ /dev/null @@ -1,157 +0,0 @@ -{# - # Jinja2 macro which converts Python data structure to Apache format - #} - -{%- macro apache_encode( - item, - convert_bools=false, - convert_nums=false, - indent=" ", - level=0, - quote_all_nums=false, - quote_all_strings=false, - type="sections") %} - - {%- if type == "sections" %} - {%- for c in item["content"] %} - {#- First check if this section has options #} - {%- if "options" in c -%} - {{ apache_encode( - c["options"], - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - quote_all_nums=quote_all_nums, - quote_all_strings=quote_all_strings, - type="options") }} - {%- endif %} - - {%- set is_empty = [] %} - - {#- Check if this section has some sub-sections #} - {%- if "sections" in c %} - {%- for s in c["sections"] %} - - {#- Check for empty sub-sections #} - {%- for i in s["content"] %} - {%- if ('options' in i and i['options'] | length > 0) or - ('sections' in i and i['sections'] | length > 0) %} - {%- if is_empty.append(1) %}{% endif %} - {%- endif %} - {%- endfor %} - - {%- if is_empty | length > 0 -%} - {{ indent * level ~ "<" ~ s["name"] ~ (" " ~ - apache_encode( - s["param"], - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - quote_all_nums=quote_all_nums, - quote_all_strings=quote_all_strings, - type="value") - if "param" in s else "") ~ ">\n" ~ - apache_encode( - s, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - quote_all_nums=quote_all_nums, - quote_all_strings=quote_all_strings, - type="sections") ~ - indent * level ~ "\n" }} - - {%- if not loop.last -%} - {{ "\n" }} - {%- endif %} - {%- endif %} - {%- endfor %} - {%- endif %} - - {%- if not loop.last and - ( - "options" in c and c["options"] | length > 0 or - ( - "sections" in c and c["sections"] | length > 0 and - is_empty | length > 0 - ) - ) - -%} - {{ "\n" }} - {%- endif %} - {%- endfor %} - - {%- elif type == "options" %} - {%- for o in item -%} - {%- for key, val in o.iteritems() | sort -%} - {{ indent * (level-1) ~ key ~ " " ~ - apache_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - quote_all_nums=quote_all_nums, - quote_all_strings=quote_all_strings, - type="value") ~ "\n" }} - {%- endfor %} - {%- endfor %} - - {%- elif type == "value" %} - {%- if item in [True, False] or - convert_bools and (item in ["true", "True", "false", "False"]) %} - {#- Value is a boolean -#} - {{ item | lower }} - - {%- elif item is number or - (convert_nums and ( - item | int | string == item or - item | float | string == item)) or - (convert_bools and item in ["true", "True", "false", "False"]) or - item in [True, False] %} - {#- Value is a number -#} - {%- if quote_all_nums -%} - "{{ item }}" - {%- else -%} - {{ item }} - {%- endif %} - - {%- elif item is string %} - {#- Value is a string #} - {%- if quote_all_strings or - " " in item or - "\t" in item or - "\n" in item or - "\r" in item or - item == "" -%} - - "{{ item | replace("\\", "\\\\") | replace('"', '\\"') }}" - {%- else -%} - {{ item }} - {%- endif %} - - {%- elif item is not mapping %} - {#- Value is a list #} - {%- for v in item -%} - {{ apache_encode( - v, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - quote_all_nums=quote_all_nums, - quote_all_strings=quote_all_strings, - type="value") }} - - {%- if not loop.last -%} - {{ " " }} - {%- endif %} - {%- endfor %} - {%- endif %} - - {%- endif %} - -{%- endmacro %} diff --git a/templates/config-encoder-macros/macros/erlang_encode_macro.erb b/templates/config-encoder-macros/macros/erlang_encode_macro.erb deleted file mode 100644 index f911ce3..0000000 --- a/templates/config-encoder-macros/macros/erlang_encode_macro.erb +++ /dev/null @@ -1,82 +0,0 @@ -<%-# - # ERB macro which converts Ruby data structure to Erlang config format - #-%> -<%- - _item ||= Array.new - convert_bools ||= false - convert_nums ||= false - first ||= Array.new - indent ||= ' ' - level ||= 0 - macro_path ||= 'macros/erlang_encode_macro.erb' --%> -<%- if item.kind_of?(Hash) -%> - <%-# The item is a hash #-%> - <%--%><%= "\n" -%> - <%- first.push(1) -%> - <%- item.sort.each do |key, val| -%> - <%--%><%= indent * level + '{' + key.to_s + ',' -%> - <%- if ! val.kind_of?(Hash) -%> - <%--%><%= ' ' -%> - <%- end -%> - <%- - _item.push(item) - - item = val - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - level = level-1 - item = _item.pop() - -%> - <%--%><%= '}' -%> - <%- if key != item.keys.sort.last -%> - <%--%><%= ",\n" -%> - <%- end -%> - <%- end -%> -<%- elsif item.nil? or item.kind_of?(Numeric) or - (convert_nums && ( - item.to_i.to_s == item or - item.to_f.to_s == item)) or - (convert_bools && ['true', 'True', 'false', 'False'].include?(item)) or - item.kind_of?(TrueClass) or item.kind_of?(FalseClass) -%> - <%-# Item is a value of a number, null or boolean #-%> - <%--%><%= item.to_s.downcase -%> -<%- elsif item.kind_of?(String) -%> - <%-# The item is a string #-%> - <%--%><%= '"' + item.to_s + '"' -%> -<%- else -%> - <%-# The item is an array #-%> - <%--%><%= '[' -%> - <%- item.each do |val| -%> - <%- if val.kind_of?(String) or val.kind_of?(Numeric) -%> - <%--%><%= "\n" + indent * (level) -%> - <%- end -%> - <%- - _item.push(item) - item = val - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - level = level-1 - item = _item.pop() - -%> - <%- if val == item.last -%> - <%--%><%= "\n" -%> - <%- else -%> - <%--%><%= ',' -%> - <%- end -%> - <%- end -%> - <%- if item.length > 0 -%> - <%--%><%= indent * (level-1 > 0 ? level-1 : 0) + ']' -%> - <%- else -%> - <%--%><%= ']' -%> - <%- end -%> - <%- if level == 0 -%> - <%--%><%= ".\n" -%> - <%- end -%> -<%- end -%> diff --git a/templates/config-encoder-macros/macros/erlang_encode_macro.j2 b/templates/config-encoder-macros/macros/erlang_encode_macro.j2 deleted file mode 100644 index d094f73..0000000 --- a/templates/config-encoder-macros/macros/erlang_encode_macro.j2 +++ /dev/null @@ -1,96 +0,0 @@ -{# - # Jinja2 macro which converts Python data structure to Erlang config format - #} - -{%- macro erlang_encode( - item, - convert_bools=false, - convert_nums=false, - first=[], - indent=" ", - level=0) %} - - {%- if item is mapping %} - {#- The item is a dict -#} - - {{ "\n" }} - - {%- if first.append(1) %}{% endif %} - - {%- for key, val in item.iteritems() | sort -%} - {{ indent * level ~ "{" ~ key ~ "," }} - - {%- if val is not mapping -%} - {{ " " }} - {%- endif -%} - - {{ erlang_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - first=first, - indent=indent, - level=level+1) }}{{ "}" }} - - {%- if not loop.last -%} - {{ ",\n" }} - {%- endif -%} - {%- endfor %} - - {%- elif item == "null" or item is number or - (convert_nums and ( - item | int | string == item or - item | float | string == item)) or - (convert_bools and item in ["true", "True", "false", "False"]) or - item in [True, False] %} - - {#- Item is a value of a number, null or boolean -#} - - {{ item | lower }} - - {%- elif item is string %} - {#- The item is a string -#} - - {%- if item[0] == ':' -%} - {{ item[1:] }} - {%- else -%} - {{ '"' ~ item ~ '"' }} - {%- endif -%} - - {%- else %} - {#- The item is a list -#} - - {{ "[" }} - - {%- for val in item -%} - {%- if val is string or val is number -%} - {{ "\n" ~ indent * (level) }} - {%- endif -%} - - {{ erlang_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - first=first, - indent=indent, - level=level+1) }} - - {%- if loop.last -%} - {{ "\n" }} - {%- else -%} - {{ "," }} - {%- endif %} - {%- endfor -%} - - {%- if item | length > 0 -%} - {{ indent * (level-1) ~ "]" }} - {%- else -%} - {{ "]" }} - {%- endif %} - - {%- if level == 0 -%} - {{ ".\n" }} - {%- endif %} - - {%- endif %} -{%- endmacro %} diff --git a/templates/config-encoder-macros/macros/ini_encode_macro.erb b/templates/config-encoder-macros/macros/ini_encode_macro.erb deleted file mode 100644 index d024652..0000000 --- a/templates/config-encoder-macros/macros/ini_encode_macro.erb +++ /dev/null @@ -1,53 +0,0 @@ -<%# - # ERB macro which converts Ruby data structure to INI format - #-%> -<%- - item ||= {} - delimiter ||= '=' - first ||= Array.new - quote ||= '' - section_is_comment ||= false - ucase_prop ||= false -%> -<%-# First process vars without a section -%> -<%- item.sort.each do |property, value| -%> - <%- if ! value.kind_of?(Hash) -%> - <%- if first.length == 0 -%> - <%- first.push(1) -%> - <%- end -%> - <%- if value.kind_of?(String) or value.kind_of?(Numeric) -%> - <%--%><%= (ucase_prop ? property.to_s.upcase : property) + delimiter + - quote + value.to_s + quote + "\n" -%> - <%- else -%> - <%- value.each do |i| -%> - <%--%><%= (ucase_prop ? property.to_s.upcase : property) + delimiter + - quote + i.to_s + quote + "\n" -%> - <%- end -%> - <%- end -%> - <%- end -%> -<%- end -%> -<%#- Then process vars with a section -%> -<%- item.sort.each do |section, options| -%> - <%- if options.kind_of?(Hash) -%> - <%- if first.length == 0 -%> - <%- first.push(1) -%> - <%- else -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- if section_is_comment -%> - <%--%><%= '# ' + section.to_s + "\n" -%> - <%- else -%> - <%--%><%= '[' + section.to_s + "]\n" -%> - <%- end -%> - <%- options.sort.each do |property, value| -%> - <%- if value.kind_of?(String) or value.kind_of?(Numeric) -%> - <%--%><%= (ucase_prop ? property.to_s.upcase : property) + delimiter + - quote + value.to_s + quote + "\n" -%> - <%- else -%> - <%- value.each do |v| -%> - <%--%><%= (ucase_prop ? property.to_s.upcase : property) + - delimiter + quote + v.to_s + quote + "\n" -%> - <%- end -%> - <%- end -%> - <%- end -%> - <%- end -%> -<%- end -%> diff --git a/templates/config-encoder-macros/macros/ini_encode_macro.j2 b/templates/config-encoder-macros/macros/ini_encode_macro.j2 deleted file mode 100644 index d525365..0000000 --- a/templates/config-encoder-macros/macros/ini_encode_macro.j2 +++ /dev/null @@ -1,58 +0,0 @@ -{# - # Jinja2 macro which converts Python data structure to INI format - #} - -{%- macro ini_encode( - item, - delimiter="=", - first=[], - quote="", - section_is_comment=false, - ucase_prop=false) %} - - {#- First process vars without a section #} - {%- for property, value in item.iteritems() | sort %} - {%- if value is not mapping %} - {%- if first | length == 0 -%} - {%- if first.append(1) %}{% endif %} - {%- endif -%} - - {%- if value is string or value is number -%} - {{ (property | upper if ucase_prop else property) ~ - delimiter ~ quote ~ value ~ quote ~ "\n" }} - {%- else %} - {%- for item in value -%} - {{ (property | upper if ucase_prop else property) ~ - delimiter ~ quote ~ item ~ quote ~ "\n" }} - {%- endfor %} - {%- endif %} - {%- endif %} - {%- endfor %} - - {#- Then process vars with a section #} - {%- for section, options in item.iteritems() | sort %} - {%- if options is mapping -%} - {%- if first | length == 0 -%} - {%- if first.append(1) %}{% endif %} - {%- else -%} - {{ "\n" }} - {%- endif %} - {%- if section_is_comment -%} - {{ "# " ~ section ~ "\n" }} - {%- else -%} - {{ "[" ~ section ~ "]\n" }} - {%- endif %} - {%- for property, value in options.iteritems() | sort -%} - {%- if value is string or value is number -%} - {{ (property | upper if ucase_prop else property) ~ - delimiter ~ quote ~ value ~ quote ~ "\n" }} - {%- else %} - {%- for v in value -%} - {{ (property | upper if ucase_prop else property) ~ - delimiter ~ quote ~ v ~ quote ~ "\n" }} - {%- endfor %} - {%- endif %} - {%- endfor %} - {%- endif %} - {%- endfor %} -{%- endmacro %} diff --git a/templates/config-encoder-macros/macros/json_encode_macro.erb b/templates/config-encoder-macros/macros/json_encode_macro.erb deleted file mode 100644 index db4e088..0000000 --- a/templates/config-encoder-macros/macros/json_encode_macro.erb +++ /dev/null @@ -1,87 +0,0 @@ -<%-# - # ERB macro which converts Ruby data structure to JSON format - #-%> -<%- - _item ||= Array.new - convert_bools ||= false - convert_nums ||= false - indent ||= ' ' - level ||= 0 - macro_path ||= 'macros/json_encode_macro.erb' --%> -<%- if item.kind_of?(Hash) -%> - <%-# The item is a hash -%> - <%--%><%= '{' -%> - <%- if item.length > 0 -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- item.sort.each do |key, val| -%> - <%--%><%= indent * (level+1) + '"' + key.to_s + '": ' -%> - <%- - _item.push(item) - - item = val - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - level = level-1 - item = _item.pop() - -%> - <%- if key == item.keys.sort.last -%> - <%--%><%= "\n" -%> - <%- else -%> - <%--%><%= ",\n" -%> - <%- end -%> - <%- end -%> - <%- if item.length > 0 -%> - <%--%><%= indent * level + '}' -%> - <%- else -%> - <%--%><%= '}' -%> - <%- end -%> - <%- if level == 0 -%> - <%--%><%= "\n" -%> - <%- end -%> -<%- elsif item.kind_of?(Numeric) or - (convert_nums and ( - item.to_i.to_s == item or - item.to_f.to_s == item)) or - (convert_bools and ['true', 'True', 'false', 'False'].include?(item)) or - item.kind_of?(TrueClass) or item.kind_of?(FalseClass) -%> - <%-# Item is a value of a number, null or boolean -%> - <%--%><%= item.to_s -%> -<%- elsif item.kind_of?(String) -%> - <%-# The item is a string -%> - <%--%><%= '"' + item.to_s + '"' -%> -<%- else -%> - <%-# The item is an array -%> - <%--%><%= '[' -%> - <%- if item.length > 0 -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- item.each do |val| -%> - <%--%><%= indent * (level+1) -%> - <%- - _item.push(item) - item = val - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - level = level-1 - item = _item.pop() - -%> - <%- if val == item.last -%> - <%--%><%= "\n" -%> - <%- else -%> - <%--%><%= ",\n" -%> - <%- end -%> - <%- end -%> - <%- if item.length > 0 -%> - <%--%><%= indent * level + ']' -%> - <%- else -%> - <%--%><%= ']' -%> - <%- end -%> -<%- end -%> diff --git a/templates/config-encoder-macros/macros/json_encode_macro.j2 b/templates/config-encoder-macros/macros/json_encode_macro.j2 deleted file mode 100644 index b523432..0000000 --- a/templates/config-encoder-macros/macros/json_encode_macro.j2 +++ /dev/null @@ -1,93 +0,0 @@ -{# - # Jinja2 macro which converts Python data structure to JSON format - #} - -{%- macro json_encode( - item, - convert_bools=false, - convert_nums=false, - indent=" ", - level=0) %} - - {%- if item is mapping %} - {#- The item is a dict -#} - - {{ "{" }} - - {%- if item | length > 0 -%} - {{ "\n" }} - {%- endif %} - - {%- for key, val in item.iteritems() | sort -%} - {{ indent * (level+1) ~ '"' ~ key ~ '": ' }}{{ json_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1) }} - - {%- if loop.last -%} - {{ "\n" }} - {%- else -%} - {{ ",\n" }} - {%- endif %} - {%- endfor -%} - - {%- if item | length > 0 -%} - {{ indent * level ~ "}" }} - {%- else -%} - {{ "}" }} - {%- endif %} - - {%- if level == 0 -%} - {{ "\n" }} - {%- endif %} - - {%- elif item == "null" or item is number or - (convert_nums and ( - item | int | string == item or - item | float | string == item)) or - (convert_bools and item in ["true", "True", "false", "False"]) or - item in [True, False] %} - - {#- Item is a value of a number, null or boolean -#} - - {{ item | lower }} - - {%- elif item is string %} - {#- The item is a string -#} - - {{ '"' ~ item ~ '"' }} - - {%- else %} - {#- The item is a list -#} - - {{ "[" }} - - {%- if item | length > 0 -%} - {{ "\n" }} - {%- endif %} - - {%- for val in item -%} - {{ indent * (level+1) }}{{ json_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1) }} - - {%- if loop.last -%} - {{ "\n" }} - {%- else -%} - {{ ",\n" }} - {%- endif %} - {%- endfor -%} - - {%- if item | length > 0 -%} - {{ indent * level ~ "]" }} - {%- else -%} - {{ "]" }} - {%- endif %} - - {%- endif %} -{%- endmacro %} diff --git a/templates/config-encoder-macros/macros/logstash_encode_macro.erb b/templates/config-encoder-macros/macros/logstash_encode_macro.erb deleted file mode 100644 index 299c837..0000000 --- a/templates/config-encoder-macros/macros/logstash_encode_macro.erb +++ /dev/null @@ -1,4 +0,0 @@ -<%-# - # ERB macro which converts Ruby data structure to Logstash format - #-%> -TODO diff --git a/templates/config-encoder-macros/macros/logstash_encode_macro.j2 b/templates/config-encoder-macros/macros/logstash_encode_macro.j2 deleted file mode 100644 index e3b863c..0000000 --- a/templates/config-encoder-macros/macros/logstash_encode_macro.j2 +++ /dev/null @@ -1,126 +0,0 @@ -{# - # Jinja2 macro which converts Python data structure to Logstash format - #} - -{%- macro logstash_encode( - item, - convert_bools=false, - convert_nums=false, - indent=" ", - level=0, - prevtype="") %} - - {%- if item is mapping %} - {#- The item is a dict -#} - - {%- if prevtype in ["value", "value_hash", "array"] -%} - {{ "{\n" }} - {%- endif -%} - - {%- for key, val in item.iteritems() | sort -%} - {%- if key[0] == ":" -%} - {{ indent * level ~ key[1:] ~ " {\n" }}{{ logstash_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - prevtype="block") }} - - {%- if loop.last -%} - {%- if val is string or - val is number or - val in [True, False] or - (val is mapping and val.keys()[0][0] != ":") -%} - {{ "\n" ~ indent * level ~ "}\n" }} - {%- else -%} - {{ indent * level ~ "}\n" }} - {%- endif %} - {%- endif %} - {%- else -%} - {{ indent * level }} - - {%- if prevtype == "value_hash" -%} - {{ '"' ~ key ~ '" => ' }} - {%- else -%} - {{ key ~ " => " }} - {%- endif -%} - - {{ logstash_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - prevtype=("value_hash" if val is mapping else "value")) }} - {%- endif %} - - {%- if not loop.last and - ( - val is string or - val is number or - item in [True, False]) -%} - {{ "\n" }} - {%- endif %} - {%- endfor %} - - {%- if prevtype in ["value", "value_hash", "array"] -%} - {{ "\n" ~ indent * (level-1) ~ "}" }} - - {%- if prevtype in ["value", "value_array"] -%} - {{ "\n" }} - {%- endif %} - {%- endif %} - - {%- elif item is number or - (convert_nums and ( - item | int | string == item or - item | float | string == item)) or - (convert_bools and item in ["true", "True", "false", "False"]) or - item in [True, False] %} - - {#- Item is a value of a number or boolean -#} - - {{ item | lower }} - - {%- elif item is string %} - {#- The item is a string -#} - - {{ '"' ~ item ~ '"' }} - - {%- else %} - {#- The item is a list -#} - - {%- for val in item %} - {%- if val is mapping and val.keys()[0][0] == ":" -%} - {#- value is a block -#} - - {{ logstash_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level, - prevtype="block") }} - {%- else %} - {%- if loop.first -%} - {{ "[\n" }} - {%- endif -%} - - {{ indent * level }}{{ logstash_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - prevtype="array") }} - - {%- if loop.last -%} - {{ "\n" ~ indent * (level-1) ~ "]\n" }} - {%- else -%} - {{ ",\n" }} - {%- endif %} - {%- endif %} - {%- endfor -%} - {%- endif %} -{%- endmacro %} diff --git a/templates/config-encoder-macros/macros/toml_encode_macro.erb b/templates/config-encoder-macros/macros/toml_encode_macro.erb deleted file mode 100644 index cfcb6fd..0000000 --- a/templates/config-encoder-macros/macros/toml_encode_macro.erb +++ /dev/null @@ -1,175 +0,0 @@ -<%-# - # ERB macro which converts Ruby data structure to TOML format - #-%> -<%- - _item ||= Array.new - _prevkey ||= Array.new - _prevtype ||= Array.new - _old_prevkey ||= Array.new - _old_level ||= Array.new - convert_bools ||= false - convert_nums ||= false - first ||= Array.new - indent ||= ' ' - level ||= 0 - macro_path ||= 'macros/toml_encode_macro.erb' - quote ||= '"' - prevkey ||= '' - prevtype ||= '' --%> -<%- if item.kind_of?(Hash) -%> - <%-# The item is a hash -%> - <%-# First process all strings, numbers, booleans and arrays -%> - <%- item.sort.each do |key, val| -%> - <%- if val.kind_of?(String) or - val.kind_of?(Numeric) or - val.kind_of?(TrueClass) or val.kind_of?(FalseClass) or - ( - ! val.kind_of?(Hash) and - val.length > 0 and - ! val[0].kind_of?(Hash)) -%> - <%-# The value is a string, a number, or an array -%> - <%--%><%= indent * level + key.to_s + ' = ' -%> - <%- - _item.push(item) - _prevkey.push(prevkey) - _prevtype.push(prevtype) - item = val - prevtype = '' - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - prevkey = _prevkey.pop() - prevtype = _prevtype.pop() - item = _item.pop() - -%> - <%- first.push(1) -%> - <%- end -%> - <%- end -%> - <%-# Then process all data structures -%> - <%- item.sort.each do |key, val| -%> - <%- if ! val.kind_of?(String) and - ! val.kind_of?(Numeric) and - ! val.kind_of?(TrueClass) and ! val.kind_of?(FalseClass) and - (val.kind_of?(Hash) or val[0].kind_of?(Hash)) -%> - <%- - old_prevkey = prevkey - old_level = level - -%> - <%- if val.kind_of?(Hash) -%> - <%-# The value is a hash -%> - <%- if prevkey != '' and prevkey != key -%> - <%- level = level + 1 -%> - <%- end -%> - <%- if prevkey == '' -%> - <%- prevkey = key -%> - <%- else -%> - <%- prevkey = prevkey + '.' + key -%> - <%- end -%> - <%- if first.length > 0 -%> - <%--%><%= "\n" -%> - <%- end -%> - <%--%><%= indent * level + '[' + prevkey + ']' + "\n" -%> - <%- elsif val[0].kind_of?(Hash) -%> - <%-# The value is a table -%> - <%- if prevkey == '' -%> - <%- prevkey = key -%> - <%- else -%> - <%- prevkey = prevkey + '.' + key.to_s -%> - <%- end -%> - <%- level = level + 1 -%> - <%- end -%> - <%- - _item.push(item) - _prevkey.push(prevkey) - _prevtype.push(prevtype) - _old_prevkey.push(old_prevkey) - _old_level.push(old_level) - item = val - prevtype = '' - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - prevkey = _prevkey.pop() - prevtype = _prevtype.pop() - old_prevkey = _old_prevkey.pop() - old_level = _old_level.pop() - item = _item.pop() - -%> - <%- if val.kind_of?(Hash) and prevkey != '' and prevkey != key or - val[0].kind_of?(Hash) -%> - <%- level = level - 1 -%> - <%- end -%> - <%- - prevkey = old_prevkey - level = old_level - -%> - <%- first.push(1) -%> - <%- end -%> - <%- end -%> -<%- elsif item.kind_of?(Numeric) or - (convert_nums and ( - item.to_i.to_s == item or - item.to_f.to_s == item)) or - (convert_bools and ['true', 'True', 'false', 'False'].include?(item)) or - item.kind_of?(TrueClass) or item.kind_of?(FalseClass) -%> - <%-# The item is a number or boolean -%> - <%--%><%= item.to_s.downcase -%> - <%- if prevtype != 'array' -%> - <%--%><%= "\n" -%> - <%- end -%> -<%- elsif item.kind_of?(String) -%> - <%-# The item is a string -%> - <%--%><%= quote + item.to_s.gsub("\n", "\\n").gsub("\t", "\\t") + quote -%> - <%- if prevtype != 'array' -%> - <%--%><%= "\n" -%> - <%- end -%> -<%- else -%> - <%-# The item is an array -%> - <%- if item[0].kind_of?(Hash) -%> - <%- item.each do |d| -%> - <%--%><%= "\n" + indent * level + '[[' + prevkey.to_s + ']]' + "\n" -%> - <%- - _item.push(item) - _prevkey.push(prevkey) - _prevtype.push(prevtype) - item = d - prevtype = 'array' - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - prevkey = _prevkey.pop() - prevtype = _prevtype.pop() - item = _item.pop() - -%> - <%- end -%> - <%- else -%> - <%--%><%= '[' -%> - <%- item.each do |d| -%> - <%- - _item.push(item) - _prevkey.push(prevkey) - _prevtype.push(prevtype) - item = d - prevtype = 'array' - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - prevkey = _prevkey.pop() - prevtype = _prevtype.pop() - item = _item.pop() - -%> - <%- if d != item.last -%> - <%--%><%= ', ' -%> - <%- end -%> - <%- end -%> - <%--%><%= ']' -%> - <%- if prevtype != 'array' -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- end -%> -<%- end -%> diff --git a/templates/config-encoder-macros/macros/toml_encode_macro.j2 b/templates/config-encoder-macros/macros/toml_encode_macro.j2 deleted file mode 100644 index c5cdfec..0000000 --- a/templates/config-encoder-macros/macros/toml_encode_macro.j2 +++ /dev/null @@ -1,148 +0,0 @@ -{# - # Jinja2 macro which converts Python data structure to TOML format - #} - -{%- macro toml_encode( - item, - convert_bools=false, - convert_nums=false, - first=[], - indent=" ", - level=0, - prevkey="", - prevtype="", - quote='"') %} - - {%- if item is mapping %} - {#- The item is a dict #} - - {#- First process all strings, numbers, booleans and lists #} - {%- for key, val in item.iteritems() | sort -%} - {%- if val is string or - val is number or - val in [True, False] or - ( - val is not mapping and - val | length > 0 and - val[0] is not mapping) %} - - {#- The value is a string, a number, or a list -#} - {{ indent * level }}{{ key }}{{ " = " }}{{ toml_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - first=first, - indent=indent, - level=level, - prevkey=prevkey) }} - {%- if first.append(1) %}{% endif %} - {%- endif %} - {%- endfor %} - - {#- Then process all data structures #} - {%- for key, val in item.iteritems() | sort %} - {%- if val is not string and - val is not number and - val not in [True, False] and - ( - val is mapping or - val[0] is mapping) %} - - {%- set old_prevkey = prevkey %} - {%- set old_level = level %} - - {%- if val is mapping %} - {#- The val is a dict #} - {%- if prevkey != "" and prevkey != key %} - {%- set level = level + 1 %} - {%- endif %} - - {%- set prevkey = (key if prevkey == "" else prevkey + "." + key) %} - - {%- if first | length > 0 -%} - {{ "\n" }} - {%- endif -%} - - {{ indent * level }}[{{ prevkey }}]{{ "\n" }} - {%- elif val[0] is mapping %} - {#- The val is a table #} - {%- set prevkey = (key if prevkey == "" else prevkey + "." + key) %} - {%- set level = level + 1 %} - {%- endif -%} - - {{ toml_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - first=first, - indent=indent, - level=level, - prevkey=prevkey) }} - - {%- set prevkey = old_prevkey %} - {%- set level = old_level %} - {%- if first.append(1) %}{% endif %} - {%- endif %} - {%- endfor %} - - {%- elif item is number or - (convert_nums and ( - item | int | string == item or - item | float | string == item)) or - (convert_bools and item in ["true", "True", "false", "False"]) or - item in [True, False] %} - - {#- The item is a number or boolean -#} - {{ item | lower }} - - {%- if prevtype != "list" -%} - {{ "\n" }} - {%- endif -%} - - {%- elif item is string %} - {#- The item is a string -#} - {{ quote ~ item | replace("\n", "\\n") | replace("\t", "\\t") ~ quote }} - - {%- if prevtype != "list" -%} - {{ "\n" }} - {%- endif -%} - - {%- else %} - {#- The item is a list #} - {%- if item[0] is mapping %} - {%- for d in item -%} - {{ "\n" }}{{ indent * level }}[[{{ prevkey }}]]{{ "\n" }}{{ toml_encode( - d, - convert_bools=convert_bools, - convert_nums=convert_nums, - first=first, - indent=indent, - level=level) }} - {%- endfor %} - {%- else -%} - {{ "[" }} - - {%- for d in item -%} - {{ toml_encode( - d, - convert_bools=convert_bools, - convert_nums=convert_nums, - first=first, - indent=indent, - level=level, - prevtype="list") }} - - {%- if not loop.last -%} - {{ ", " }} - {%- endif %} - {%- endfor -%} - - {{ "]" }} - - {%- if prevtype != "list" -%} - {{ "\n" }} - {%- endif -%} - - {%- endif %} - {%- endif %} -{%- endmacro %} diff --git a/templates/config-encoder-macros/macros/xml_encode_macro.erb b/templates/config-encoder-macros/macros/xml_encode_macro.erb deleted file mode 100644 index be8a534..0000000 --- a/templates/config-encoder-macros/macros/xml_encode_macro.erb +++ /dev/null @@ -1,103 +0,0 @@ -<%-# - # ERB macro which converts Ruby data structure to XML format - #-%> -<%- - _item ||= Array.new - _prev_list ||= Array.new - _prevkey ||= Array.new - first ||= Array.new - indent ||= ' ' - level ||= 0 - macro_path ||= 'macros/xml_encode_macro.erb' - prevkey ||= nil --%> -<%- if item.kind_of?(Hash) -%> - <%-# The item is a hash -%> - <%- prev_list = [0] -%> - <%- item.sort.each do |key, val| -%> - <%- if first.length > 0 and prev_list[0] == 0 -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- first.push(0) -%> - <%- - prev_list.insert(0, 0) - prev_list.delete_at(1) - -%> - <%- if val == nil -%> - <%--%><%= indent * level + '<' + key.to_s + '/>\n' -%> - <%- elsif val.kind_of?(Array) -%> - <%-# Only if val is an array -%> - <%- - _item.push(item) - _prevkey.push(prevkey) - _prev_list.push(prev_list) - prev_list = [0] - prevkey = key - item = val - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - prevkey = _prevkey.pop() - prev_list = _prev_list.pop() - item = _item.pop() - -%> - <%- - prev_list.insert(0, 1) - prev_list.delete_at(1) - -%> - <%- else -%> - <%--%><%= indent * level + '<' + key.to_s + '>' -%> - <%- - _item.push(item) - _prevkey.push(prevkey) - _prev_list.push(prev_list) - prev_list = [0] - prevkey = key - item = val - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - prevkey = _prevkey.pop() - prev_list = _prev_list.pop() - item = _item.pop() - level = level-1 - -%> - <%- if val.kind_of?(Hash) or val.kind_of?(Array) -%> - <%--%><%= indent * level -%> - <%- end -%> - <%--%><%= '' -%> - <%- if key == item.keys.sort.last -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- end -%> - <%- end -%> -<%- elsif item.kind_of?(Numeric) or item.kind_of?(String) or - item.kind_of?(TrueClass) or item.kind_of?(FalseClass) -%> - <%-# The item is a number, string or boolean -%> - <%--%><%= item -%> -<%- elsif item == nil -%> - <%-# Item is a value of an empty element -%> -<%- else -%> - <%-# The item is an array -%> - <%- item.each do |e| -%> - <%--%><%= indent * level + '<' + prevkey + '>' -%> - <%- - _item.push(item) - item = e - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - item = _item.pop() - level = level-1 - -%> - <%- if ! e.kind_of?(String) and ! e.kind_of?(Numeric) and e != nil -%> - <%--%><%= indent * level -%> - <%- end -%> - <%--%><%= '' + "\n" -%> - <%- end -%> -<%- end -%> diff --git a/templates/config-encoder-macros/macros/xml_encode_macro.j2 b/templates/config-encoder-macros/macros/xml_encode_macro.j2 deleted file mode 100644 index 11e1dc6..0000000 --- a/templates/config-encoder-macros/macros/xml_encode_macro.j2 +++ /dev/null @@ -1,80 +0,0 @@ -{# - # Jinja2 macro which converts Python data structure to XML format - #} - -{%- macro xml_encode( - item, - first=[], - indent=" ", - level=0, - prevkey=none) %} - - {%- if item is mapping %} - {#- The item is a dict #} - - {%- set prev_list = [0] %} - - {%- for key, val in item.iteritems() | sort -%} - {%- if first | length > 0 and prev_list[0] == 0 -%} - {{ "\n" }} - {%- endif -%} - - {%- if first.append(0) %}{% endif %} - {%- if prev_list.insert(0, 0) and prev_list.remove(1) %}{% endif %} - - {%- if val is none -%} - {{ indent * level ~ "<" ~ key ~ " />\n" }} - {%- elif val is sequence and val is not string and val is not mapping -%} - {#- Only if val is a list -#} - {{ xml_encode( - val, - first=first, - indent=indent, - level=level, - prevkey=key) }} - {%- if prev_list.insert(0, 1) and prev_list.remove(1) %}{% endif %} - {%- else -%} - {{ indent * level }}<{{ key }}>{{ xml_encode( - val, - indent=indent, - level=level+1) }} - - {%- if val is not string and val is not number and val is not none -%} - {{ indent * level }} - {%- endif -%} - - - - {%- if loop.last -%} - {{ "\n" }} - {%- endif %} - {%- endif %} - {%- endfor %} - - {%- elif item is number or item is string or item in [True, False] %} - {#- The item is a number, string or boolean -#} - - {{ item }} - - {%- elif item is none %} - {#- Item is a value of an empty element #} - - {%- else %} - {#- The item is a list #} - - {%- for e in item -%} - {{ indent * level }}<{{ prevkey }}>{{ xml_encode( - e, - first=first, - indent=indent, - level=level+1, - prevkey=prevkey) }} - - {%- if e is not string and e is not number and e is not none -%} - {{ indent * level }} - {%- endif -%} - - {{ "\n" }} - {%- endfor %} - {%- endif %} -{%- endmacro %} diff --git a/templates/config-encoder-macros/macros/yaml_encode_macro.erb b/templates/config-encoder-macros/macros/yaml_encode_macro.erb deleted file mode 100644 index 7cd49f8..0000000 --- a/templates/config-encoder-macros/macros/yaml_encode_macro.erb +++ /dev/null @@ -1,84 +0,0 @@ -<%-# - # ERB macro which converts Ruby data structure to YAML format - #-%> -<%- - _item ||= Array.new - _skip_indent ||= Array.new - convert_bools ||= false - convert_nums ||= false - indent ||= ' ' - level ||= 0 - macro_path ||= 'macros/yaml_encode_macro.erb' - quote ||= '"' - skip_indent ||= false --%> -<%- if item.kind_of?(Hash) -%> - <%-# The item is a hash -%> - <%- item.sort.each do |key, val| -%> - <%- if skip_indent and key == item.keys.sort.first -%> - <%--%><%= ' ' -%> - <%- else -%> - <%--%><%= indent * level -%> - <%- end -%> - <%--%><%= key.to_s + ':' -%> - <%- if ! val.kind_of?(String) and - ! val.kind_of?(Numeric) and - ! val.kind_of?(TrueClass) and - ! val.kind_of?(FalseClass)-%> - <%--%><%= "\n" -%> - <%- end -%> - <%- - _item.push(item) - item = val - level = level+1 - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - level = level-1 - item = _item.pop() - -%> - <%- end -%> - <%- if item.length == 0 -%> - <%--%><%= indent * level + "{ }\n" -%> - <%- end -%> -<%- elsif item.kind_of?(Numeric) or - (convert_nums and ( - item.to_i.to_s == item or - item.to_f.to_s == item)) or - (convert_bools and ['true', 'True', 'false', 'False'].include?(item)) or - item.kind_of?(TrueClass) or item.kind_of?(FalseClass) -%> - <%-# The item is a number or boolean -%> - <%--%><%= ' ' + item.to_s.downcase + "\n" -%> -<%- elsif item.kind_of?(String) -%> - <%-# The item is a string -%> - <%--%><%= ' ' + - quote + - item.gsub('\\', '\\\\').gsub(quote, '\\' + quote) + - quote + "\n" -%> -<%- else -%> - <%-# The item is an array -%> - <%- item.each do |n| -%> - <%--%><%= indent * level + '-' -%> - <%- if ! n.kind_of?(String) and - ! n.kind_of?(Numeric) and - ! n.kind_of?(Hash) -%> - <%--%><%= "\n" -%> - <%- end -%> - <%- - _item.push(item) - _skip_indent.push(skip_indent) - - item = n - level = level+1 - skip_indent = true - -%> - <%--%><%= ERB.new(IO.read(macro_path), nil, '-', '_erbout_' + - rand(36**20).to_s(36)).result(OpenStruct.new().send(:binding)) -%> - <%- - skip_indent = _skip_indent.pop() - level = level-1 - item = _item.pop() - -%> - <%- end -%> -<%- end -%> diff --git a/templates/config-encoder-macros/macros/yaml_encode_macro.j2 b/templates/config-encoder-macros/macros/yaml_encode_macro.j2 deleted file mode 100644 index ad1dbc3..0000000 --- a/templates/config-encoder-macros/macros/yaml_encode_macro.j2 +++ /dev/null @@ -1,76 +0,0 @@ -{# - # Jinja2 macro which converts Python data structure to YAML format - #} - -{%- macro yaml_encode( - item, - convert_bools=false, - convert_nums=false, - indent=" ", - level=0, - quote='"', - skip_indent=false) %} - - {%- if item is mapping %} - {#- The item is a dict #} - {%- for key, val in item.iteritems() | sort -%} - {%- if skip_indent and loop.first -%} - {{ " " }} - {%- else -%} - {{ indent * level }} - {%- endif -%} - - {{ key }}: - - {%- if val is not string and val is not number -%} - {{ "\n" }} - {%- endif -%} - - {{ yaml_encode( - val, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - quote=quote) }} - {%- else -%} - {{ indent * level + "{ }\n" }} - {%- endfor %} - - {%- elif item is number or - (convert_nums and ( - item | int | string == item or - item | float | string == item)) or - (convert_bools and item in ["true", "True", "false", "False"]) or - item in [True, False] %} - - {#- The item is a number or boolean -#} - {{ " " ~ item | lower ~ "\n" }} - - {%- elif item is string %} - {#- The item is a string -#} - {{ " " + - quote + - item | replace("\\", "\\\\") | replace(quote, "\\" + quote) + - quote + "\n" }} - - {%- else %} - {#- The item is a list #} - {%- for n in item -%} - {{ indent * level }}- - {%- if n is not string and - n is not number and - n is not mapping -%} - {{ "\n" }} - {%- endif -%} - {{ yaml_encode( - n, - convert_bools=convert_bools, - convert_nums=convert_nums, - indent=indent, - level=level+1, - quote=quote, - skip_indent=true) }} - {%- endfor %} - {%- endif %} -{%- endmacro %} diff --git a/templates/config-encoder-macros/puppet_apply.sh b/templates/config-encoder-macros/puppet_apply.sh deleted file mode 100755 index 619b518..0000000 --- a/templates/config-encoder-macros/puppet_apply.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -# Apply Puppet manifests -puppet apply \ - --test \ - --confdir=. \ - --ssldir=/tmp/puppet_ssl \ - --templatedir=./templates \ - $@ \ - ./site.pp - -# Or you can use ERB directly: -# $ erb -r ostruct -T '-' 'ini_data={"aaa" => "bbb", "ccc" => {"ddd" => "eee"}}' ./templates/test.ini.erb diff --git a/templates/config-encoder-macros/site.pp b/templates/config-encoder-macros/site.pp deleted file mode 100644 index 4a642a3..0000000 --- a/templates/config-encoder-macros/site.pp +++ /dev/null @@ -1,71 +0,0 @@ -node default { - ### Apache - $apache_data = hiera('apache_data') - - file { '/tmp/test.apache' : - ensure => present, - content => template('test.apache.erb'), - } - - ### Erlang - $erlang_data = hiera('erlang_data') - - file { '/tmp/test.erlang' : - ensure => present, - content => template('test.erlang.erb'), - } - - ### INI - $ini_data = hiera('ini_data') - - file { '/tmp/test.ini' : - ensure => present, - content => template('test.ini.erb'), - } - - file { '/tmp/test.simple' : - ensure => present, - content => template('test.ini_simple.erb'), - } - - - ### JSON - $json_data = hiera('json_data') - - file { '/tmp/test.json' : - ensure => present, - content => template('test.json.erb'), - } - - ### Logstash - $json_data = hiera('logstash_data') - - file { '/tmp/test.logstash' : - ensure => present, - content => template('test.logstash.erb'), - } - - ### TOML - $toml_data = hiera('toml_data') - - file { '/tmp/test.toml' : - ensure => present, - content => template('test.toml.erb'), - } - - ### XML - $xml_data = hiera('xml_data') - - file { '/tmp/test.xml' : - ensure => present, - content => template('test.xml.erb'), - } - - ### YAML - $yaml_data = hiera('yaml_data') - - file { '/tmp/test.yaml' : - ensure => present, - content => template('test.yaml.erb'), - } -} diff --git a/templates/config-encoder-macros/site.yaml b/templates/config-encoder-macros/site.yaml deleted file mode 100644 index 8b0c841..0000000 --- a/templates/config-encoder-macros/site.yaml +++ /dev/null @@ -1,109 +0,0 @@ ---- - -- name: Apache encode example - hosts: all - vars_files: - - vars/apache_test.yaml - tasks: - - name: Create test.apache file - template: - src: templates/test.apache.j2 - dest: /tmp/test.apache - tags: - - apache - -- name: Erlang encode example - hosts: all - vars_files: - - vars/erlang_test.yaml - tasks: - - name: Create test.erlang file - template: - src: templates/test.erlang.j2 - dest: /tmp/test.erlang - tags: - - erlang - -- name: INI encode example - hosts: all - vars_files: - - vars/ini_test.yaml - tasks: - - name: Create test.ini file - template: - src: templates/test.ini.j2 - dest: /tmp/test.ini - tags: - - ini - -- name: Simplified INI encode example - hosts: all - vars_files: - - vars/ini_test.yaml - tasks: - - name: Create test.simple file - template: - src: templates/test.ini_simple.j2 - dest: /tmp/test.ini_simple - tags: - - simple - -- name: JSON encode example - hosts: all - vars_files: - - vars/json_test.yaml - tasks: - - name: Create test.json file - template: - src: templates/test.json.j2 - dest: /tmp/test.json - tags: - - json - -- name: Logstash encode example - hosts: all - vars_files: - - vars/logstash_test.yaml - tasks: - - name: Create test.logstash file - template: - src: templates/test.logstash.j2 - dest: /tmp/test.logstash - tags: - - logstash - -- name: TOML encode example - hosts: all - vars_files: - - vars/toml_test.yaml - tasks: - - name: Create test.toml file - template: - src: templates/test.toml.j2 - dest: /tmp/test.toml - tags: - - toml - -- name: XML encode example - hosts: all - vars_files: - - vars/xml_test.yaml - tasks: - - name: Create test.xml file - template: - src: templates/test.xml.j2 - dest: /tmp/test.xml - tags: - - xml - -- name: YAML encode example - hosts: all - vars_files: - - vars/yaml_test.yaml - tasks: - - name: Create test.yaml file - template: - src: templates/test.yaml.j2 - dest: /tmp/test.yaml - tags: - - yaml diff --git a/templates/config-encoder-macros/templates/test.apache.erb b/templates/config-encoder-macros/templates/test.apache.erb deleted file mode 100644 index 6290ec9..0000000 --- a/templates/config-encoder-macros/templates/test.apache.erb +++ /dev/null @@ -1,11 +0,0 @@ -# -# This file is managed by Puppet. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -<%- - item = @apache_data || (apache_data.kind_of?(String) ? eval(apache_data) : apache_data) - macro_path = 'macros/apache_encode_macro.erb' --%> -<%= ERB.new(IO.read(macro_path), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> diff --git a/templates/config-encoder-macros/templates/test.apache.j2 b/templates/config-encoder-macros/templates/test.apache.j2 deleted file mode 100644 index b8db502..0000000 --- a/templates/config-encoder-macros/templates/test.apache.j2 +++ /dev/null @@ -1,9 +0,0 @@ -# -# This file is managed by Ansible. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -{% from "macros/apache_encode_macro.j2" import apache_encode with context -%} - -{{ apache_encode(apache_data) }} diff --git a/templates/config-encoder-macros/templates/test.erlang.erb b/templates/config-encoder-macros/templates/test.erlang.erb deleted file mode 100644 index 575af7f..0000000 --- a/templates/config-encoder-macros/templates/test.erlang.erb +++ /dev/null @@ -1,11 +0,0 @@ -% -% This file is managed by Puppet. -% Do not edit this file manually. -% Any changes will be automatically reverted. -% - -<%- - item = @erlang_data || (erlang_data.kind_of?(String) ? eval(erlang_data) : erlang_data) - macro_path = 'macros/erlang_encode_macro.erb' --%> -<%= ERB.new(IO.read(macro_path), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> diff --git a/templates/config-encoder-macros/templates/test.erlang.j2 b/templates/config-encoder-macros/templates/test.erlang.j2 deleted file mode 100644 index ec06568..0000000 --- a/templates/config-encoder-macros/templates/test.erlang.j2 +++ /dev/null @@ -1,9 +0,0 @@ -% -% This file is managed by Ansible. -% Do not edit this file manually. -% Any changes will be automatically reverted. -% - -{% from "macros/erlang_encode_macro.j2" import erlang_encode with context -%} - -{{ erlang_encode(erlang_data) }} diff --git a/templates/config-encoder-macros/templates/test.ini.erb b/templates/config-encoder-macros/templates/test.ini.erb deleted file mode 100644 index bc60dab..0000000 --- a/templates/config-encoder-macros/templates/test.ini.erb +++ /dev/null @@ -1,10 +0,0 @@ -# -# This file is managed by Puppet. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -<%- - item = @ini_data || (ini_data.kind_of?(String) ? eval(ini_data) : ini_data) --%> -<%= ERB.new(IO.read('macros/ini_encode_macro.erb'), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> diff --git a/templates/config-encoder-macros/templates/test.ini.j2 b/templates/config-encoder-macros/templates/test.ini.j2 deleted file mode 100644 index 840c1c4..0000000 --- a/templates/config-encoder-macros/templates/test.ini.j2 +++ /dev/null @@ -1,9 +0,0 @@ -# -# This file is managed by Ansible. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -{% from "macros/ini_encode_macro.j2" import ini_encode with context -%} - -{{ ini_encode(ini_data) }} diff --git a/templates/config-encoder-macros/templates/test.ini_simple.erb b/templates/config-encoder-macros/templates/test.ini_simple.erb deleted file mode 100644 index ef5e97c..0000000 --- a/templates/config-encoder-macros/templates/test.ini_simple.erb +++ /dev/null @@ -1,12 +0,0 @@ -# -# This file is managed by Puppet. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -<%- - item = @ini_data || (ini_data.kind_of?(String) ? eval(ini_data) : ini_data) - delimiter = ' ' - section_is_comment = true --%> -<%= ERB.new(IO.read('macros/ini_encode_macro.erb'), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> diff --git a/templates/config-encoder-macros/templates/test.ini_simple.j2 b/templates/config-encoder-macros/templates/test.ini_simple.j2 deleted file mode 100644 index 551fd57..0000000 --- a/templates/config-encoder-macros/templates/test.ini_simple.j2 +++ /dev/null @@ -1,9 +0,0 @@ -# -# This file is managed by Ansible. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -{% from "macros/ini_encode_macro.j2" import ini_encode with context -%} - -{{ ini_encode(ini_data, section_is_comment=true, delimiter=" ") }} diff --git a/templates/config-encoder-macros/templates/test.json.erb b/templates/config-encoder-macros/templates/test.json.erb deleted file mode 100644 index 9444eaa..0000000 --- a/templates/config-encoder-macros/templates/test.json.erb +++ /dev/null @@ -1,5 +0,0 @@ -<%- - item = @json_data || (json_data.kind_of?(String) ? eval(json_data) : json_data) - macro_path = 'macros/json_encode_macro.erb' --%> -<%= ERB.new(IO.read(macro_path), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> diff --git a/templates/config-encoder-macros/templates/test.json.j2 b/templates/config-encoder-macros/templates/test.json.j2 deleted file mode 100644 index 1962872..0000000 --- a/templates/config-encoder-macros/templates/test.json.j2 +++ /dev/null @@ -1,3 +0,0 @@ -{% from "macros/json_encode_macro.j2" import json_encode with context -%} - -{{ json_encode(json_data) }} diff --git a/templates/config-encoder-macros/templates/test.logstash.erb b/templates/config-encoder-macros/templates/test.logstash.erb deleted file mode 100644 index 88c13e9..0000000 --- a/templates/config-encoder-macros/templates/test.logstash.erb +++ /dev/null @@ -1,11 +0,0 @@ -# -# This file is managed by Puppet. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -<%- - item = @logstash_data || (logstash_data.kind_of?(String) ? eval(logstash_data) : logstash_data) - macro_path = 'macros/logstash_encode_macro.erb' --%> -<%= ERB.new(IO.read(macro_path), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> diff --git a/templates/config-encoder-macros/templates/test.logstash.j2 b/templates/config-encoder-macros/templates/test.logstash.j2 deleted file mode 100644 index f854c68..0000000 --- a/templates/config-encoder-macros/templates/test.logstash.j2 +++ /dev/null @@ -1,9 +0,0 @@ -# -# This file is managed by Ansible. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -{% from "macros/logstash_encode_macro.j2" import logstash_encode with context -%} - -{{ logstash_encode(logstash_data) }} diff --git a/templates/config-encoder-macros/templates/test.toml.erb b/templates/config-encoder-macros/templates/test.toml.erb deleted file mode 100644 index d02120d..0000000 --- a/templates/config-encoder-macros/templates/test.toml.erb +++ /dev/null @@ -1,11 +0,0 @@ -# -# This file is managed by Puppet. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -<%- - item = @toml_data || (toml_data.kind_of?(String) ? eval(toml_data) : toml_data) - macro_path = 'macros/toml_encode_macro.erb' --%> -<%= ERB.new(IO.read(macro_path), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> diff --git a/templates/config-encoder-macros/templates/test.toml.j2 b/templates/config-encoder-macros/templates/test.toml.j2 deleted file mode 100644 index 909c892..0000000 --- a/templates/config-encoder-macros/templates/test.toml.j2 +++ /dev/null @@ -1,9 +0,0 @@ -# -# This file is managed by Ansible. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -{% from "macros/toml_encode_macro.j2" import toml_encode with context -%} - -{{ toml_encode(toml_data) }} diff --git a/templates/config-encoder-macros/templates/test.xml.erb b/templates/config-encoder-macros/templates/test.xml.erb deleted file mode 100644 index 68e7ab9..0000000 --- a/templates/config-encoder-macros/templates/test.xml.erb +++ /dev/null @@ -1,15 +0,0 @@ - - - - -<%- - item = @xml_data || (xml_data.kind_of?(String) ? eval(xml_data) : xml_data) - macro_path = 'macros/xml_encode_macro.erb' --%> -<%= ERB.new(IO.read(macro_path), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> diff --git a/templates/config-encoder-macros/templates/test.xml.j2 b/templates/config-encoder-macros/templates/test.xml.j2 deleted file mode 100644 index d62ae88..0000000 --- a/templates/config-encoder-macros/templates/test.xml.j2 +++ /dev/null @@ -1,13 +0,0 @@ - - - - -{% from "macros/xml_encode_macro.j2" import xml_encode with context -%} - -{{ xml_encode(xml_data) }} diff --git a/templates/config-encoder-macros/templates/test.yaml.erb b/templates/config-encoder-macros/templates/test.yaml.erb deleted file mode 100644 index 14d4326..0000000 --- a/templates/config-encoder-macros/templates/test.yaml.erb +++ /dev/null @@ -1,11 +0,0 @@ -# -# This file is managed by Puppet. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -<%- - item = @yaml_data || (yaml_data.kind_of?(String) ? eval(yaml_data) : yaml_data) - macro_path = 'macros/yaml_encode_macro.erb' --%> -<%= ERB.new(IO.read(macro_path), nil, '-', '_erbout1').result(OpenStruct.new().send(:binding)) -%> diff --git a/templates/config-encoder-macros/templates/test.yaml.j2 b/templates/config-encoder-macros/templates/test.yaml.j2 deleted file mode 100644 index 78e5efb..0000000 --- a/templates/config-encoder-macros/templates/test.yaml.j2 +++ /dev/null @@ -1,9 +0,0 @@ -# -# This file is managed by Ansible. -# Do not edit this file manually. -# Any changes will be automatically reverted. -# - -{% from "macros/yaml_encode_macro.j2" import yaml_encode with context -%} - -{{ yaml_encode(yaml_data) }} diff --git a/templates/config-encoder-macros/vars/apache_test.yaml b/templates/config-encoder-macros/vars/apache_test.yaml deleted file mode 100644 index 59c3220..0000000 --- a/templates/config-encoder-macros/vars/apache_test.yaml +++ /dev/null @@ -1,64 +0,0 @@ ---- - -apache_data: - content: - - options: - - ServerName: localhost - - Listen: 8080 - - PidFile: tmp/httpd.pid - - LockFile: tmp/accept.lock - - options: - - "# This is an example of a comment": "" - - LoadModule: - - authz_host_module - - /usr/libexec/apache2/mod_authz_host.so - - LoadModule: - - dir_module - - /usr/libexec/apache2/mod_dir.so - - LoadModule: - - env_module - - /usr/libexec/apache2/mod_env.so - - LoadModule: - - mime_module - - /usr/libexec/apache2/mod_mime.so - - LoadModule: - - log_config_module - - /usr/libexec/apache2/mod_log_config.so - - LoadModule: - - rewrite_module - - /usr/libexec/apache2/mod_rewrite.so - - LoadModule: - - php5_module - - /usr/local/opt/php53/libexec/apache2/libphp5.so - - LogLevel: info - - options: - - ErrorLog: "|cat" - - LogFormat: - - "%h %l %u %t \"%r\" %>s %b" - - common - - CustomLog: - - "|cat" - - common - - options: - - DocumentRoot: build - - sections: - - name: Directory - param: build - content: - - options: - - AllowOverride: all - - Order: allow,deny - - Allow: - - from - - all - - options: - - AddType: - - application/x-httpd-php - - .php - - DirectoryIndex: - - index.html - - index.php - - options: - - SetEnv: - - LOCAL_SERVER - - true diff --git a/templates/config-encoder-macros/vars/erlang_test.yaml b/templates/config-encoder-macros/vars/erlang_test.yaml deleted file mode 100644 index cec3ec8..0000000 --- a/templates/config-encoder-macros/vars/erlang_test.yaml +++ /dev/null @@ -1,14 +0,0 @@ ---- - -erlang_data: - - rabbit: - - tcp_listeners: - - '"127.0.0.1"': 5672 - - ssl_listeners: - - 5671 - - ssl_options: - - cacertfile: /path/to/testca/cacert.pem - - certfile: /path/to/server/cert.pem - - keyfile: /path/to/server/key.pem - - verify: verify_peer - - fail_if_no_peer_cert: true diff --git a/templates/config-encoder-macros/vars/ini_test.yaml b/templates/config-encoder-macros/vars/ini_test.yaml deleted file mode 100644 index aa8c59d..0000000 --- a/templates/config-encoder-macros/vars/ini_test.yaml +++ /dev/null @@ -1,15 +0,0 @@ ---- - -ini_data: - var1: val1 - var2: val2 - section1: - aaa: - - asdf - - zxcv - bbb: 123 - ccc: 'true' - section2: - ddd: asdfasd - eee: 1234 - fff: 'false' diff --git a/templates/config-encoder-macros/vars/json_test.yaml b/templates/config-encoder-macros/vars/json_test.yaml deleted file mode 100644 index 72fda5d..0000000 --- a/templates/config-encoder-macros/vars/json_test.yaml +++ /dev/null @@ -1,16 +0,0 @@ ---- - -json_data: - string: This is a string - number: 123 - boolean: 'true' - dict: - aaa: bbb - ccc: true - list: - - eee - - fff - complex: - ggg: - - hhh - - iii: jjj diff --git a/templates/config-encoder-macros/vars/logstash_test.yaml b/templates/config-encoder-macros/vars/logstash_test.yaml deleted file mode 100644 index a7cb5c4..0000000 --- a/templates/config-encoder-macros/vars/logstash_test.yaml +++ /dev/null @@ -1,33 +0,0 @@ ---- - -logstash_data: - - :input: - - :file: - path: /var/log/messages - type: syslog - - :file: - path: /var/log/apache/access.log - type: apache - - :filter: - - ':if [type] == "apache"': - - ':if [status] =~ /^5\d\d/': - :nagios: - codes: plain - workers: 1 - - ':else if [status] =~ /^4\d\d/': - :elasticsearch: - xxx: - aaa: bbb - ccc: ddd - hosts: - - es-server1 - - es-server2 - query: "type:start AND operation:%{[opid]}" - fields: - - "@timestamp" - - started - - :output: - - :file: - path: /var/log/%{type}.%{+yyyy.MM.dd.HH} - - :statsd: - increment: apache.%{[response][status]} diff --git a/templates/config-encoder-macros/vars/toml_test.yaml b/templates/config-encoder-macros/vars/toml_test.yaml deleted file mode 100644 index 041cebc..0000000 --- a/templates/config-encoder-macros/vars/toml_test.yaml +++ /dev/null @@ -1,33 +0,0 @@ ---- - -toml_data: - title: TOML Example - owner: - name: Tom Preston-Werner - organization: GitHub - bio: "GitHub Cofounder & CEO\nLikes tater tots and beer." - dob: "1979-05-27T07:32:00Z" - database: - server: 192.168.1.1 - ports: [ 8001, 8001, 8002 ] - connection_max: 5000 - enabled: true - servers: - alpha: - ip: 10.0.0.1 - dc: eqdc10 - beta: - ip: 10.0.0.2 - dc: eqdc10 - country: "中国" - clients: - data: [ ["gamma", "delta"], [1, 2] ] - hosts: [ "alpha", "omega" ] - products: - - - name: Hammer - sku: 738594937 - - - name: Nail - sku: 284758393 - color: gray diff --git a/templates/config-encoder-macros/vars/xml_test.yaml b/templates/config-encoder-macros/vars/xml_test.yaml deleted file mode 100644 index 1707ee2..0000000 --- a/templates/config-encoder-macros/vars/xml_test.yaml +++ /dev/null @@ -1,15 +0,0 @@ ---- - -xml_data: - root: - elem1: asd - elem2: - - asd - - zxc - with_attrs: - 'elem3 attr1="val1" attr2=val2': 123 - 'elem4 attr3="val3"': - - Value 1 - - elem5: - - Value 2 - - Value 3 diff --git a/templates/config-encoder-macros/vars/yaml_test.yaml b/templates/config-encoder-macros/vars/yaml_test.yaml deleted file mode 100644 index 7d68a00..0000000 --- a/templates/config-encoder-macros/vars/yaml_test.yaml +++ /dev/null @@ -1,16 +0,0 @@ ---- - -yaml_data: - string: This is a string - number: 123 - boolean: 'true' - dict: - aaa: bbb - ccc: true - list: - - eee - - fff - complex: - ggg: - - hhh - - iii: jjj diff --git a/templates/config-encoder-macros/yaml_converter.py b/templates/config-encoder-macros/yaml_converter.py deleted file mode 100755 index fbbb086..0000000 --- a/templates/config-encoder-macros/yaml_converter.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env python2 - -from argparse import RawTextHelpFormatter -from jinja2 import Template -from yaml import load -import argparse -import sys - - -def parse_arguments(): - description = 'YAML converter' - epilog = ( - "Examples:\n" - " $ %(prog)s -f apache -v apache_data -y ./vars/apache_test.yaml\n" - " $ %(prog)s -f json -v json_data -y ./vars/json_test.yaml") - - parser = argparse.ArgumentParser( - description=description, - epilog=epilog, - formatter_class=RawTextHelpFormatter) - parser.add_argument( - '-f', '--format', - metavar='FORMAT', - choices=['apache', 'erlang', 'ini', 'json', 'logstash', 'toml', 'xml', 'yaml'], - required=True, - help='output format') - parser.add_argument( - '-p', '--path', - metavar='PATH', - default='.', - help='path to the macros directory (default: .)') - parser.add_argument( - '-v', '--var', - metavar='NAME', - help='read data from certain YAML variable') - parser.add_argument( - '-y', '--yaml', - metavar='FILE', - dest='yaml_fh', - type=argparse.FileType('r'), - default='-', - help='YAML file to convert (default: -)') - - return (parser.parse_args(), parser) - - -def main(): - # Parse command line arguments - (args, parser) = parse_arguments() - - # Load the YAML data to Python data structure - yaml_data = load(args.yaml_fh) - args.yaml_fh.close() - - if args.var: - yaml_data = yaml_data[args.var] - - # Read Jinja2 template as text - template_fh = open( - '%s/macros/%s_encode_macro.j2' % (args.path, args.format)) - template_text = template_fh.read() - template_fh.close() - - # Create Jinja2 template object - t = Template(template_text) - # Convert the YAML data to the final format - encode_method = getattr(t.module, '%s_encode' % args.format) - output = encode_method(yaml_data) - - # Print the result - sys.stdout.write(output.encode('utf8')) - - -if __name__ == '__main__': - main() diff --git a/templates/config-encoder-macros/yaml_converter.rb b/templates/config-encoder-macros/yaml_converter.rb deleted file mode 100755 index 2de51b0..0000000 --- a/templates/config-encoder-macros/yaml_converter.rb +++ /dev/null @@ -1,121 +0,0 @@ -#!/usr/bin/env ruby - -require 'erb' -require 'optparse' -require 'ostruct' -require 'yaml' - -def parse_arguments - args = OpenStruct.new - - # Default values - args.format = nil - args.help = false - args.path = '.' - args.var = nil - args.yaml_file = nil - - options = OptionParser.new do |opts| - opts.program_name = $PROGRAM_NAME - opts.banner = "usage: #{opts.program_name} " \ - '[-h] -f FORMAT [-p PATH] [-v NAME] [-y FILE]' - - opts.separator('') - opts.separator('YAML converter') - opts.separator('') - opts.separator('optional arguments:') - - opts.on( - '-f', '--format FORMAT', - 'output format') do |f| - args.format = f - end - - opts.on( - '-h', '--help', - 'show this help message and exit') do |h| - args.help = h - end - - opts.on( - '-p', '--path PATH', - 'path to the macros directory (default: .)') do |p| - args.path = p - end - - opts.on( - '-v', '--var NAME', - 'read data from certain YAML variable') do |v| - args.var = v - end - - opts.on( - '-y', '--yaml FILE', - 'YAML file to convert (default: -)') do |y| - args.yaml_file = y - end - - opts.separator('') - opts.separator('Examples:') - opts.separator(" $ #{opts.program_name} -f apache -v apache_data -y " \ - './vars/apache_test.yaml') - opts.separator(" $ #{opts.program_name} -f json -v json_data -y " \ - './vars/json_test.yaml') - end - - options.parse! - - return args, options -end - -def main - # Parse command line arguments - args, options = parse_arguments - - # Check if help should be displayed - if args.help - print options.help - exit - end - - # Check if format was specified - if args.format.nil? - print options.help - abort("\nERROR: Format not specified.") - end - - # Check if specified format is supported - formats = %w(apache erlang ini json toml xml yaml) - unless formats.include?(args.format) - abort('ERROR: Unsuported format. Suported formats are: ' + - formats.join(', ')) - end - - if args.yaml_file.nil? - # Read data from pipeline - yaml_input = ARGF.read - yaml_load_fnc = YAML.method(:load) - else - # Check if the YAML file exists - abort('ERROR: YAML file not found.') unless File.exist?(args.yaml_file) - yaml_input = args.yaml_file - yaml_load_fnc = YAML.method(:load_file) - end - - # Convert the YAML data to the Ruby data structure - if args.var.nil? - item = yaml_load_fnc.call(yaml_input) - else - item = yaml_load_fnc.call(yaml_input)[args.var] - end - - # Check if the macro file exists - macro_path = "#{args.path}/macros/#{args.format}_encode_macro.erb" - abort('ERROR: Macro file not found.') unless File.exist?(macro_path) - - # Convert the YAML data to the final format - binding = OpenStruct.new.send(:binding) - print ERB.new(IO.read(macro_path), nil, '-', '_erbout0').result(binding) -end - -main if __FILE__ == $PROGRAM_NAME diff --git a/templates/rabbitmq.config.j2 b/templates/rabbitmq.config.j2 index 450db4c..817a644 100644 --- a/templates/rabbitmq.config.j2 +++ b/templates/rabbitmq.config.j2 @@ -1,9 +1,3 @@ -% -% This file is managed by Ansible. -% Do not edit this file manually. -% Any changes will be automatically reverted. -% +{{ ansible_managed | comment('erlang') }} -{% from "templates/config-encoder-macros/macros/erlang_encode_macro.j2" import erlang_encode with context -%} - -{{ erlang_encode(rabbitmq_config, convert_bools=true, convert_nums=true) }} +{{ rabbitmq_config | encode_erlang(convert_bools=true, convert_nums=true) }} From 2536701cbed99cc55e6c360d3d4338e8a40cd2c4 Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Tue, 1 Aug 2017 13:46:28 +0100 Subject: [PATCH 6/8] updated README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 4d72b81..c49a52d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ # RabbitMQ Ansible Role +## Dependcies + +It depends from: + +- [`config_encoder_filters v1.3`](https://galaxy.ansible.com/jtyr/config_encoder_filters) + ## Version See: From 247295e3ade83e4bce5a3d8690baa58d4b0d084b Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Tue, 1 Aug 2017 17:06:53 +0100 Subject: [PATCH 7/8] fixing cluster feature --- tasks/cluster.yml | 4 ---- tasks/configure-cluster.yml | 15 +++------------ tasks/configure.yml | 17 ++++++++++++++--- tasks/node_fullrestart.yml | 4 ++++ tasks/node_fullstart.yml | 10 ++++++++++ tasks/node_fullstop.yml | 17 +++++++++++++++++ templates/rabbitmq.config.j2 | 2 +- 7 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 tasks/node_fullrestart.yml create mode 100644 tasks/node_fullstart.yml create mode 100644 tasks/node_fullstop.yml diff --git a/tasks/cluster.yml b/tasks/cluster.yml index d755a0b..8e0dfed 100644 --- a/tasks/cluster.yml +++ b/tasks/cluster.yml @@ -14,7 +14,3 @@ - name: ensure rabbitmq cluster member fail: msg="Unable to join the cluster." when: ("'already_member' not in rabbitmq_output.stderr") and rabbitmq_output.rc != 0 - -#- name: start rabbitmq app -# shell: rabbitmqctl start_app -# when: rabbitmq_cluster and rabbitmq_nodename == rabbitmq_cluster_master diff --git a/tasks/configure-cluster.yml b/tasks/configure-cluster.yml index bfdc0c4..8a2c177 100644 --- a/tasks/configure-cluster.yml +++ b/tasks/configure-cluster.yml @@ -6,17 +6,8 @@ line: "{{ hostvars[item].rabbitmq_cluster_ip_address | default(hostvars[item].ansible_default_ipv4.address) }} {{ hostvars[item].ansible_hostname }}" with_items: "{{ ansible_play_hosts }}" -- name: set erlang cookie - template: - src: erlang.cookie.j2 - dest: "{{ rabbitmq_erlang_cookie_file }}" - owner: rabbitmq - group: rabbitmq - mode: 0400 - notify: - - rabbitmq restart - - - - include: cluster.yml when: rabbitmq_cluster and rabbitmq_nodename != rabbitmq_cluster_master + +- name: start rabbitmq app + shell: rabbitmqctl start_app diff --git a/tasks/configure.yml b/tasks/configure.yml index 48ebb9a..9b19e14 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -5,10 +5,21 @@ src: rabbitmq.config.j2 dest: /etc/rabbitmq/rabbitmq.config when: rabbitmq_config is defined - notify: - - rabbitmq restart -- include: node_start.yml +- name: rabbitmq full stop + include: node_fullstop.yml + when: rabbitmq_erlang_cookie is defined + +- name: set erlang cookie + template: + src: erlang.cookie.j2 + dest: "{{ rabbitmq_erlang_cookie_file }}" + owner: rabbitmq + group: rabbitmq + mode: 0400 + when: rabbitmq_erlang_cookie is defined + +- include: node_fullstart.yml - include: vhosts.yml diff --git a/tasks/node_fullrestart.yml b/tasks/node_fullrestart.yml new file mode 100644 index 0000000..4dc690d --- /dev/null +++ b/tasks/node_fullrestart.yml @@ -0,0 +1,4 @@ +--- + +- include: node_fullstop.yml +- include: node_fullstart.yml diff --git a/tasks/node_fullstart.yml b/tasks/node_fullstart.yml new file mode 100644 index 0000000..81f1742 --- /dev/null +++ b/tasks/node_fullstart.yml @@ -0,0 +1,10 @@ +--- + +- name: start rabbitmq server + service: + name: rabbitmq-server + enabled: true + state: started + +- name: start rabbitmq app + shell: rabbitmqctl start_app diff --git a/tasks/node_fullstop.yml b/tasks/node_fullstop.yml new file mode 100644 index 0000000..d07c6d6 --- /dev/null +++ b/tasks/node_fullstop.yml @@ -0,0 +1,17 @@ +--- + +- name: start rabbitmq server + service: + name: rabbitmq-server + state: started + +- name: stop rabbitmq app + shell: rabbitmqctl stop_app + +- name: rabbitmq app reset + shell: rabbitmqctl reset + +- name: stop rabbitmq server + service: + name: rabbitmq-server + state: stopped diff --git a/templates/rabbitmq.config.j2 b/templates/rabbitmq.config.j2 index 817a644..339f294 100644 --- a/templates/rabbitmq.config.j2 +++ b/templates/rabbitmq.config.j2 @@ -1,3 +1,3 @@ -{{ ansible_managed | comment('erlang') }} +{{ ansible_managed | comment('erlang') }}--- {{ rabbitmq_config | encode_erlang(convert_bools=true, convert_nums=true) }} From f3722cce73d9c9b67b5b9a1d4e3ca2e5b9703eb2 Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Wed, 2 Aug 2017 09:50:09 +0100 Subject: [PATCH 8/8] bumped rabbitmq version. added default cluster name and task to set it up --- defaults/main.yml | 10 ++-------- tasks/configure-cluster.yml | 3 +++ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 37496a3..f9333a3 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,18 +1,14 @@ --- rabbitmq_cluster: False - rabbitmq_cluster_master: "rabbit@{{ hostvars[ansible_play_hosts.0].ansible_hostname }}" - +rabbitmq_cluster_name: "rabbitmq-cluster" rabbitmq_erlang_cookie_file: /var/lib/rabbitmq/.erlang.cookie - rabbitmq_plugin_dir: "/usr/lib/rabbitmq/lib/rabbitmq_server-{{ rabbitmq_version.split('-').0 }}/plugins" - rabbitmq_plugins: - rabbitmq_management rabbitmq_plugins_disabled: [] - rabbitmq_users: - user: admin password: admin @@ -21,8 +17,6 @@ rabbitmq_users: rabbitmq_users_absent: - guest -rabbitmq_version: 3.6.6-1 - +rabbitmq_version: 3.6.10-1 rabbitmq_vhosts: [] - rabbitmq_vhosts_absent: [] diff --git a/tasks/configure-cluster.yml b/tasks/configure-cluster.yml index 8a2c177..cf12e21 100644 --- a/tasks/configure-cluster.yml +++ b/tasks/configure-cluster.yml @@ -6,6 +6,9 @@ line: "{{ hostvars[item].rabbitmq_cluster_ip_address | default(hostvars[item].ansible_default_ipv4.address) }} {{ hostvars[item].ansible_hostname }}" with_items: "{{ ansible_play_hosts }}" +- name: set cluster name + shell: "rabbitmqctl set_cluster_name {{ rabbitmq_cluster_name }}" + - include: cluster.yml when: rabbitmq_cluster and rabbitmq_nodename != rabbitmq_cluster_master