首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复模板字符串中的模板错误:没有带有分子molecule_from_yaml驱动程序的“ec2”过滤器

如何修复模板字符串中的模板错误:没有带有分子molecule_from_yaml驱动程序的“ec2”过滤器
EN

Stack Overflow用户
提问于 2019-11-12 10:14:29
回答 1查看 7.7K关注 0票数 1

我运行的是一个分子v2.22,我想使用分子旋转一个ec2实例,来测试我的游戏手册。

但是当分子到达Detroy并创建实例的阶段时,我碰到了一个错误。

我也不明白为什么分子要跳过packages/molecule/provisioner/ansible/plugins/filters/molecule_core.py /usr/lib/python2.7/site- molecule_from_yaml插件,我认为这可能是molecule_from_yaml过滤器的责任。

见下面的错误:

代码语言:javascript
复制
 [WARNING]: Skipping plugin (/usr/lib/python2.7/site-
packages/molecule/provisioner/ansible/plugins/filters/molecule_core.py) as it
seems to be invalid: cannot import name py31compat


    PLAY [Destroy] *****************************************************************

    TASK [Populate instance config] ************************************************
    fatal: [localhost]: FAILED! => {"msg": "template error while templating string: no filter named 'molecule_from_yaml'. String: {{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"}


--> Action: 'create'
 [WARNING]: Skipping plugin (/usr/lib/python2.7/site-
packages/molecule/provisioner/ansible/plugins/filters/molecule_core.py) as it
seems to be invalid: cannot import name py31compat


    PLAY [Create] ******************************************************************

    TASK [Get the ec2 ami(s) by owner and name, if image not set] ******************
    fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ lookup('file', molecule_file) | molecule_from_yaml }}'. Error was a <class 'ansible.errors.AnsibleError'>, original   
    message: template error while templating string: no filter named 'molecule_from_yaml'. String: {{ lookup('file', molecule_file) | molecule_from_yaml }}"}

下面是一个destroy.yml文件,如果有一个实例,它会销毁先前创建的实例。

代码语言:javascript
复制
- name: Destroy
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - block:
        - name: Populate instance config
          set_fact:
            instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
            skip_instances: false
      rescue:
        - name: Populate instance config when file missing
          set_fact:
            instance_conf: {}
            skip_instances: true

    - name: Destroy molecule instance(s)
      ec2:
        state: absent
        instance_ids: "{{ item.instance_ids }}"
      register: server
      with_items: "{{ instance_conf }}"
      when: not skip_instances
      async: 7200
      poll: 0

    - name: Wait for instance(s) deletion to complete
      async_status:
        jid: "{{ item.ansible_job_id }}"
      register: ec2_jobs
      until: ec2_jobs.finished
      retries: 300
      with_items: "{{ server.results }}"

    # Mandatory configuration for Molecule to function.

    - name: Populate instance config
      set_fact:
        instance_conf: {}

    - name: Dump instance config
      copy:
        content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
        dest: "{{ molecule_instance_config }}"
      when: server.changed | bool

下面是create.yml

代码语言:javascript
复制
---
- name: Create
  hosts: localhost
  connection: local
  gather_facts: false
  no_log: "{{ molecule_no_log }}"
  vars:
    ssh_user: ubuntu
    ssh_port: 22
    keypair_name: mpho_csosecuritydev.pem
    keypair_path: /home/ec2-user/mpho_csosecuritydev.pem
    security_group_name: euw1-cso_securitydev_run_ansible-sg
  tasks:
    - name: Get the ec2 ami(s) by owner and name, if image not set
      ec2_ami_facts:
        owners:
        filters:
          name: "{{ item.image_name }}"
      loop: "{{ molecule_yml.platforms }}"
      when: item.image is not defined
      register: ami_facts

    - name: Create molecule instance(s)
      ec2:
        key_name: "{{ keypair_name }}"
        image: ami-08cb423ed619f
        instance_type: t2.micro
        vpc_subnet_id: subnet-07b35
        group: "{{ security_group_name }}"
        instance_tags:
          Name: seceng-molecule-test-amz2
        wait: true
        assign_public_ip: false
        instance_profile_name: euw1-ansible_run-instance_profile
        exact_count: 1
        count_tag:
          instance: seceng-molecule-test-amz2
      register: server
      #      loop: '{{ lookup("file", molecule.yml) | molecule_from_yaml }}'
      loop_control:
        index_var: index
      async: 7200
      poll: 0

    - name: Wait for instance(s) creation to complete
      async_status:
        jid: "{{ item.ansible_job_id }}"
      register: ec2_jobs
      until: ec2_jobs.finished
      retries: 300
      with_items: "{{ server.results }}"

    # Mandatory configuration for Molecule to function.

    - name: Populate instance config dict
      set_fact:
        instance_conf_dict: {
          'instance': "{{ item.instances[0].tags.instance }}",
          'address': "{{ item.instances[0].public_ip }}",
          'user': "{{ ssh_user }}",
          'port': "{{ ssh_port }}",
          'identity_file': "{{ keypair_path }}",
          'instance_ids': "{{ item.instance_ids }}", }
      with_items: "{{ ec2_jobs.results }}"
      register: instance_config_dict
      when: server.changed | bool

    - name: Convert instance config dict to a list
      set_fact:
        instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
      when: server.changed | bool

    - name: Dump instance config
      copy:
        content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
        dest: "{{ molecule_instance_config }}"
      when: server.changed | bool

    - name: Wait for SSH
      wait_for:
        port: "{{ ssh_port }}"
        host: "{{ item.address }}"
        search_regex: SSH
        delay: 10
        timeout: 320
      with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"

    - name: Wait for boot process to finish
      pause:
        minutes: 2

我希望我已就这个问题提供了足够的资料。

EN

回答 1

Stack Overflow用户

发布于 2020-05-15 16:35:39

我也有同样的问题,但是在macOS 10.15.4上本地运行,使用brew安装了ansible和分子。

卸载这两种设备,然后用pip3重新安装有助于:

代码语言:javascript
复制
brew uninstall molecule
brew uninstall ansible
pip3 install ansible --user
pip3 install "molecule[lint]" --user
pip3 install "molecule[docker]" --user   

然后,如果希望识别命令,则需要将python包添加到PATH中。

在我的例子中,由于我使用python 3.7,我将其添加到~/..bashrc中:

代码语言:javascript
复制
export PATH="/Users/<user>/Library/Python/3.7/bin:$PATH"

确保你把你使用的版本。如果您想在mac上使用python 3而不是默认的2,那么在安装了brew之后,将它添加到您的.bashrc (或.zshrc或您使用的任何东西)中:

代码语言:javascript
复制
alias python=/usr/local/bin/python3

来源:https://github.com/ansible-community/molecule/issues/2173

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58816457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档