ansible-template

How can I test jinja2 templates in ansible?

一世执手 提交于 2019-11-28 15:37:13
问题 Sometimes I need to test some jinja2 templates that I use in my ansible roles. What is the simplest way for doing this? For example, I have a template (test.j2): {% if users is defined and users %} {% for user in users %}{{ user }} {% endfor %} {% endif %} and vars (in group_vars/all): --- users: - Mike - Smith - Klara - Alex 回答1: At this time exists 4 different variants: 1_Online (using https://cryptic-cliffs-32040.herokuapp.com/) Based on jinja2-live-parser code. 2_Interactive (using python

How can I use default with variable in Ansible?

空扰寡人 提交于 2019-11-28 14:32:32
I know that I can use a simple hardcoded string in default but I am trying to do this: myvar: "{{ lookup('env','var1') | default("{{var2}}",true) }}" But it adds that as a string instead of evaluating it. Once you opened a Jinja2 expression with {{ you don't need to open it again (especially quoted) and you can refer to the variables by their names: myvar: "{{ lookup('env','var1') | default(var2, true) }}" 来源: https://stackoverflow.com/questions/42776108/how-can-i-use-default-with-variable-in-ansible

How can I take a list of server names and append a resource URI and port to each?

大憨熊 提交于 2019-11-27 22:31:30
I merged two lists from an Ansible inventory: set_fact: fact1: "{{ groups['group1'] + groups[group2']|list }} The output is: fact1: - server01 - server02 - server03 With the above results, I need to append https:// to the front, and a port number to the back of each element. Then I need to convert it to a comma delimited list for a server config. In this example I want: https://server01:8000,https://server02:8000,https://server03:8000 . I tried using a join: set_fact: fact2: "{{ fact1|join(':8000,') }}" which partly worked but it left the last server without a port. How can I achieve my goal?

How can I take a list of server names and append a resource URI and port to each?

江枫思渺然 提交于 2019-11-27 16:31:04
问题 I merged two lists from an Ansible inventory: set_fact: fact1: "{{ groups['group1'] + groups[group2']|list }} The output is: fact1: - server01 - server02 - server03 With the above results, I need to append https:// to the front, and a port number to the back of each element. Then I need to convert it to a comma delimited list for a server config. In this example I want: https://server01:8000,https://server02:8000,https://server03:8000 . I tried using a join: set_fact: fact2: "{{ fact1|join('

How to use template module with different set of variables?

怎甘沉沦 提交于 2019-11-27 10:04:24
问题 My use case is the following : I have a template file, and I would like to create 2 different files from that template, with the variables being filled by a different set of variables for each file. For example, lets say I want to template the file containing the line: mkdir -p {{myTemplateVariable}} I would like to find a proper way to get this variable filled by "File1" and "File2". Something like : - name: template test 1 template: src=myTemplateFile dest=result1 - name: template test 2

How can I use default with variable in Ansible?

你。 提交于 2019-11-27 08:33:10
问题 I know that I can use a simple hardcoded string in default but I am trying to do this: myvar: "{{ lookup('env','var1') | default("{{var2}}",true) }}" But it adds that as a string instead of evaluating it. 回答1: Once you opened a Jinja2 expression with {{ you don't need to open it again (especially quoted) and you can refer to the variables by their names: myvar: "{{ lookup('env','var1') | default(var2, true) }}" 来源: https://stackoverflow.com/questions/42776108/how-can-i-use-default-with

How to get the first element of a list from the output of setup module in Ansible?

天大地大妈咪最大 提交于 2019-11-27 05:23:50
I received the following data from the setup module: "ansible_nodename": "3d734bc2a391", "ansible_os_family": "RedHat", "ansible_pkg_mgr": "yum", "ansible_processor": [ "AuthenticAMD", "AMD PRO A10-8700B R6, 10 Compute Cores 4C+6G" ], "ansible_processor_cores": 1, "ansible_processor_count": 1, "ansible_processor_threads_per_core": 1, I want to retrieve the 1st value of ansible_processor and use it in a Jinja2 template. If I use {{ ansible_processor }} , it's giving me both values: "AuthenticAMD", "AMD PRO A10-8700B R6, 10 Compute Cores 4C+6G" But I want only the first one. To get first item of