标签: ansible-template

使用ansible打印在YAML文件中缩进的变量值

我正在使用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)

yaml ansible ansible-template

6
推荐指数
1
解决办法
7855
查看次数

Ansible Jinja2字符串比较

我使用group_vars中定义的变量在Jinja2模板文件中获得变量"env"的值,如:

env: "{{ defined_variable.split('-')[0] }}"
Run Code Online (Sandbox Code Playgroud)

env可能的三个值可能是abc,def,xyz.

在这个值的基础上,我想使用服务器URL,我在其中定义的可能值为defaults/main.yml:

server_abc: https://xxxx.xxx.com
server_def: https://xxxxx.xxx.com
server_xyz: https://xxxx.xxx.com
Run Code Online (Sandbox Code Playgroud)

在Jinja2模板中,我正在尝试:

{% if 'abc'  == "{{env}}" %}
serverURL: '{{ server_abc }}'
{% elif 'def'  == "{{env}}" %}
serverURL: '{{ server_def}}'
{% elif 'xyz' == "{{env}}" %}
 serverURL: '{{ server_xyz }}'
{% else %}
ServerURL: 'server Url not found'
{% endif %}
Run Code Online (Sandbox Code Playgroud)

然而,它总是最终定义,ServerURL = "server URL not found"即使env有价值的abc,defxyz.

如果我尝试在Jinja2模板(硬编码)中替换env,如下所示,条件确实满足为真: …

jinja2 ansible ansible-template

5
推荐指数
1
解决办法
3万
查看次数

模板模块可以处理多个模板/目录吗?

我相信Ansible 复制模块可以采用一大堆"文件"并在一次点击中复制它们.我相信这可以通过递归复制目录来实现.

Ansible 模板模块可以采用一大堆"模板"并在一次点击中部署它们吗?是否存在部署模板文件夹并递归应用它们的事情?

ansible ansible-template ansible-2.x

5
推荐指数
3
解决办法
1万
查看次数

Ansible,set_fact使用if then else语句

我试图在运行时使用set_fact在Ansible中设置一个基于另一个变量的变量.如果使用第一个值,无论实际值是多少.这是我的代码示例:

- name: Global_vars - get date info
    set_fact:  
      jm_env: "{{lookup('env', 'Environment')}}"
      l_env: "{% if '{{jm_env}}==Develop' %}d{% elif '{{jm_env}}==Staging'%}s{% else %}p{% endif %}"
Run Code Online (Sandbox Code Playgroud)

l_envd无论什么jm_env设置.

jinja2 ansible ansible-template

5
推荐指数
2
解决办法
2万
查看次数

Ansible w / Docker-显示当前容器状态

我正在做一个Ansible小项目,我在其中使用Docker容器。

我的问题简短:

我想获取正在运行的Dockercontainer的状态!

我的意思是,我想获取容器的当前状态,Docker通过使用“ docker ps”命令向您显示。

例如:

  1. 已退出
  2. 重新开始

我想从特定的容器中获得这些结果之一。但是无需使用CommandShell 模块

韩国

ansible docker docker-compose ansible-template

5
推荐指数
2
解决办法
7344
查看次数

如何从 Ansible with_subelements 列表中对项目进行排序

我正在使用with_subelements循环遍历一些嵌套数据。我想遍历嵌套元素,但在迭代时对第二级数据进行排序。

    - name: Can I haz sorted nested elements?
      debug: msg="device={{item.0.key}}, mounted at {{item.1.mount_point}}"
      when: profile_data.enabled
      with_subelements:
        - profile_data.layouts
        - partitions
Run Code Online (Sandbox Code Playgroud)

我尝试了一些方法来对列表进行排序,但我怀疑我with_subelements是否以应该使用的方式使用它。

我试过这个没有成功:

      with_subelements:
        - profile_data.layouts
        - "{{ partitions|sort(attribute='number') }}"
Run Code Online (Sandbox Code Playgroud)

这可能不编写我自己的with_sorted_subelements插件吗?

ansible ansible-template

5
推荐指数
1
解决办法
2454
查看次数

Ansible cloudformation 更新堆栈

我正在尝试使用我需要通过 Ansible 更改的参数值来更新 cloudformation 堆栈。堆栈是之前创建的并且有大约 20 个输入参数,但我只需要更新其中一个的值。我尝试了以下方法:

- name: update
  cloudformation:
    stack_name: "{{ stack_name }}"
    state: present
    region: "{{ region }}"
    disable_rollback: false
  args:
    template_parameters:
      CreateAlarms: "{{ create_alarms }}"
Run Code Online (Sandbox Code Playgroud)

当我运行它时,播放会抛出一个错误,指出它需要其他模板参数的值。从这里的 ansible 文档http://docs.ansible.com/ansible/latest/cloudformation_module.html,它说“如果state存在,则堆栈确实存在,并且既未指定也templatetemplate_url指定,将重用先前的模板。” 我如何告诉 cloudformation 模块也使用以前的值?我知道 aws cli 通过 usePreviousValue 标志支持它,但是我如何使用 Ansible cloudformation 做到这一点?

提前致谢。

ansible ansible-template

5
推荐指数
1
解决办法
1239
查看次数

如何在jinja2中附加到ansible的列表中

下面是我写的在ansible中使用的jinja2模板.

{% set port = 1234 %}
{% set server_ip = [] %}
{% for ip in host_ip  %}
{% do server_ip.append({{ ip }}:{{ port }}) %}
{% endfor %}
{% server_ip|join(', ') %}
Run Code Online (Sandbox Code Playgroud)

以下是我想要的输出:

devices = 192.168.56.14:1234,192.168.56.13:1234,192.168.56.10:1234
Run Code Online (Sandbox Code Playgroud)

但是当我运行ansible playbook时,它会抛出如下错误:

"AnsibleError: teme templating string: Encountered unknown tag 'do'. Jinja was looking for th: 'endfor' or 'else'
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激..

templates jinja2 ansible ansible-template

5
推荐指数
3
解决办法
2万
查看次数

无法找到或访问本地目录中的文件

当我尝试从本地目录复制到远程目录时,我的项目出现错误

错误信息:

fatal: [xxx]: FAILED! => {
"changed": false, "msg": "could not find 
src=/Users/xxx/Desktop/Docker/The_Task/./roles/docker_registry/templates,
Could not find or access 
'/Users/xxx/Desktop/Docker/The_Task/./roles/docker_registry/templates' 
on the Ansible Controller.\n
If you are using a module and expect the file to exist on the remote, see the remote_src option"
}
Run Code Online (Sandbox Code Playgroud)

剧本.yml

- name: Copying required files
  template:
    src: ./roles/docker_registry/templates
    dest: /tmp/docker_registry
Run Code Online (Sandbox Code Playgroud)

如果我做:

cd /Users/xxx/Desktop/Docker/The_Task/./roles/docker_registry/templates
Run Code Online (Sandbox Code Playgroud)

它将目录更改为我想要的目录...错误似乎来自ansible端。(它不适用于绝对路径八)

如果我使用默认的复制模块,那么它工作得很好

- name: copy files [local -> remote]
  copy:
    src: ./roles/docker_registry/templates
    dest: /tmp/docker_registry
Run Code Online (Sandbox Code Playgroud)

知道我应该做什么才能让它发挥作用吗?[其从本地复制 -> 远程 ]


但我仍然无法将整个目录复制到远程计算机中

ansible ansible-template

5
推荐指数
1
解决办法
5万
查看次数

如何在 Ansible/Jinja2 中获取变量的插值

我正在尝试以这种方式定义 Ansible 变量:

user:
  name: First Last
  nick: '{{ vars["user"]["name"] | regex_replace("\W", "_") }}'
  email: '{{ vars["user"]["nick"] }}@example.com'
Run Code Online (Sandbox Code Playgroud)

结果email是:"{{ vars[\"user\"][\"name\"] | regex_replace(\"\\W\", \"_\") }}@example.com

我也试过这样设置email{{ lookup("vars", "user.nick") }}@example.com
or {{ lookup("vars", "user")["nick"] }}@example.com
它说An unhandled exception occurred while running the lookup plugin 'vars'.

有没有办法将结果变量值作为:

user:
  name: First Last
  nick: First_Last
  email: First_Last@example.com
Run Code Online (Sandbox Code Playgroud)

?

ansible 2.9.10,python 版本 = 3.8.5

jinja2 ansible ansible-template ansible-filter

5
推荐指数
1
解决办法
246
查看次数