我正在编写一个Ansible模板,需要在主机组中生成一个ip列表,不包括当前主机IP.我在网上和文档中搜索过但我找不到任何允许您删除列表中项目的过滤器.我已经在下面创建了(hacky)for循环来做这个但是想知道是否有人知道像这样过滤的"最佳实践"方式.
{% set filtered_list = [] %}
{% for host in groups['my_group'] if host != ansible_host %}
{{ filtered_list.append(host)}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
让我们说组['my_group']有3个ip(192.168.1.1,192.168.1.2和192.168.1.3).为192.168.1.1生成模板时,它应该只打印ip的192.168.1.2和192.168.1.3.
我有一个Ansible手册,它设置了很多变量.一个剧本有这个任务:
- name: create config file
template:
src: 'templates/main_config.j2'
dest: "{{ tmp_dir }}/main_config.json"
Run Code Online (Sandbox Code Playgroud)
该模板main_config.j2在父Ansible playbooks和tasks中写入被定义为变量的字符串.
我想基于Ansible变量的值包含另一个Jinja2模板.
{% include "./templates/configurations.j2" %},
{% include "./templates/security.j2" %},
{% include './templates/' + {{ job }} + '_steps.j2' %}
Run Code Online (Sandbox Code Playgroud)
job 是在父级剧本中设置的Ansible变量.
这不起作用.可能是什么问题呢?
我合并了Ansible库存中的两个列表:
set_fact:
fact1: "{{ groups['group1'] + groups[group2']|list }}
Run Code Online (Sandbox Code Playgroud)
输出为:
fact1:
- server01
- server02
- server03
Run Code Online (Sandbox Code Playgroud)
有了以上结果,我需要在https://每个元素的前面加上一个端口号。然后,我需要将其转换为服务器配置的逗号分隔列表。
在此示例中,我想要:https://server01:8000,https://server02:8000,https://server03:8000。
我尝试使用联接:
set_fact:
fact2: "{{ fact1|join(':8000,') }}"
Run Code Online (Sandbox Code Playgroud)
可以部分工作,但最后一台服务器没有端口。
我如何实现我的目标?
如何在变量出现在regex_search参数中的 ansible playbook 中使用正则表达式找到匹配项?
以下剧本找不到匹配项...运行时使用: ansible-playbook playbook.yml
- hosts: localhost
gather_facts: no
tasks:
- set_fact:
pattern: "{{ 'foobar' | regex_search('foo') }}"
- set_fact:
m: "{{ 'beefoo' | regex_search('bee{{ pattern }}') }}"
- debug:
msg: "hi {{ m }}"
Run Code Online (Sandbox Code Playgroud) 我正在使用template模块创建一个 systemd 服务
---
- name: Systemd service
template:
src: sonar.unit.j2
dest: /etc/systemd/system/sonarqube.service
when: "ansible_service_mgr == 'systemd'"
Run Code Online (Sandbox Code Playgroud)
sonarqube.service 的内容当然可以改变。更改时我想重新启动服务。我怎样才能做到这一点?
准备模板时出现错误。谁能告诉你如何解决?
\n如有必要,还可以编辑变量。
\n vars:\n All\xd0\xa1ountry:\n - "name1"\n - "name2"\n name1:\n - "region1a" \n - "region1b" \n name2:\n - "region2a"\n - "region2b"\nRun Code Online (Sandbox Code Playgroud)\n代码
\n{% for country in All\xd0\xa1ountry %} \n{name: "{{ country }}",{% for count in {{ country }} %}My country = {{ count }}\n{% endfor %}{% endfor %}\nRun Code Online (Sandbox Code Playgroud)\n结果是一个错误\nAnsibleError:模板化字符串时出现模板错误:预期标记“:”,得到“}”
\n是的,最后我希望得到整个列表的输出
\nname: "name1 My country = "region1a" My country = "region1b" \nname: "name2: My country = "region2a" My country = "region2b"\nRun Code Online (Sandbox Code Playgroud)\n 我尝试使用 traefik 标签启动 docker 容器。要为容器创建 traefik 路由器,您必须放置一些像这样的标签。
app_names:
- tower01
- tower02
docker_labels:
awx_web:
traefik.enable: "true"
traefik.http.routers.{{ app_name }}.entrypoints: "http"
traefik.http.routers.{{ app_name }}.rule: "Host(`{{ app_server_fqdn }}`)"
traefik.http.routers.{{ app_name }}.middlewares: "https-redirect@file"
traefik.http.routers.{{ app_name }}-sec.entrypoints: "https"
traefik.http.routers.{{ app_name }}-sec.rule: "Host(`{{ app_server_fqdn }}`)"
traefik.http.routers.{{ app_name }}-sec.tls: "true"
traefik.http.routers.{{ app_name }}-sec.tls.options: "myTLSOptions@file"
traefik.http.routers.{{ app_name }}-sec.tls.certresolver: "le"
traefik.http.routers.{{ app_name }}-sec.middlewares: "default-headers@file"
traefik.http.services.{{ app_name }}.loadbalancer.server.port: "8052"
com.centurylinklabs.watchtower.enable: "{{ autoupdate_container[loop_item] }}"
Run Code Online (Sandbox Code Playgroud)
并使用与此类似的任务:
app_names:
- tower01
- tower02
docker_labels:
awx_web:
traefik.enable: "true"
traefik.http.routers.{{ app_name }}.entrypoints: "http"
traefik.http.routers.{{ app_name …Run Code Online (Sandbox Code Playgroud) 基于以下ansible playbook值..
target: "actual.domain.com"
aliases:
- "alias1.domain.com"
- "alias2.domain.com"
Run Code Online (Sandbox Code Playgroud)
我正在尝试设置一个ansible模板来生成nginx server_name,在这种情况下应该是:
server_name: "actual.domain.com alias1.domain.com alias2.domain.com"
Run Code Online (Sandbox Code Playgroud)
所以,我尝试了以下jinja2脚本......
{% if item.aliases is defined %}
{% set servername = [ item.target ] %}
{% for alias in item.aliases.iteritems() %}
{% if alias|length > 0 %}
{% servername|join(' '), alias %} # <= line 30
{% endif %}
{% endfor %}
server_name {{ servername }};
{% else %}
server_name {{ item.target }};
{% endif %}
....
Run Code Online (Sandbox Code Playgroud)
但它失败了,行号:30,错误:遇到未知标签'servername'
哪里可能是错的?
谢谢你的帮助和HNY!
我正在使用Ansible生成Behat配置文件.此配置文件是YAML文件.我正在使用这样的Jinja2模板:
default:
paths:
features: '../all/tests/features'
filters:
tags: "~@api&&~@drush"
extensions:
Behat\MinkExtension\Extension:
files_path: '{{ project_docroot }}/sites/all/tests/files'
files_path: '{{ project_docroot }}'
goutte: ~
selenium2: ~
base_url: '{{ base_url }}'
Drupal\DrupalExtension\Extension:
blackbox: ~
drush_driver: "drush"
drush:
root: "{{ project_docroot }}"
api_driver: "drupal"
drupal:
drupal_root: "{{ project_docroot }}"
region_map:
{{ project_behat_region_map }}
selectors:
{{ project_behat_selectors }}
Run Code Online (Sandbox Code Playgroud)
以下定义的变量:
project_behat_region_map: |
content: "#content"
footer: "#footer"
header: "#header"
header bottom: "#header-bottom"
navigation: "#navigation"
highlighted: "#highlighted"
help: "#help"
bottom: "#bottom"
project_behat_selectors: |
message_selector: '.messages'
error_message_selector: '.messages.error'
success_message_selector: '.messages.status'
warning_message_selector: …Run Code Online (Sandbox Code Playgroud) 我已经在不同的角色中定义了 nginx_upstreams 变量,该角色又使用 geerlingguy.nginx 角色,并且我还指定了“名称”、“策略”和“服务器”,但是当我运行此角色时,ansible 会抛出给定的错误下面就好像它无法访问为 nginx_upstream 定义的“name”变量一样。
这是抛出错误的任务
- name: Create upstream files
file:
path: "{{ nginx_vhost_path }}/{{ item.name + '.conf' }}"
state: touch
with_items: "{{ nginx_upstreams }}"
Run Code Online (Sandbox Code Playgroud)
这是定义“nginx_upstreams”时使用的角色。
- name: "Configure specific nginx service for concert to connect on remote host"
include_role:
name: geerlingguy.nginx
vars:
#for configuration specific to each server
nginx_upstreams:
- name: SOME_UPSTREAM_NAME
strategy: SOME_STRATEGY
servers: "{{ SOME_SERVER }}"
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误-
fatal: [IP]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error …Run Code Online (Sandbox Code Playgroud) ansible ×10
ansible-template ×10
jinja2 ×5
ansible-2.x ×1
docker ×1
regex ×1
templating ×1
traefik ×1
yaml ×1