【问题标题】:Ansible The task includes an option with an undefined variableAnsible 该任务包含一个带有未定义变量的选项
【发布时间】:2020-08-12 06:46:39
【问题描述】:

我正在使用 ansible 来部署网站。但我得到这个错误:

fatal: [lxc-server]: FAILED! => {"failed": true, "msg": "The task includes an option with an undefined variable. The error was: [{u'authorized_keys': u'{{ delivery_authorized_keys }}', u'group': u'{{ magento_webserver_group }}', u'name': u'{{ magento_project_user }}'}]: {{ http_group_name }}: 'http_group_name' is undefined\n\nThe error appears to have been in '/home/jredor/projets/webstore/architecture/provisioning/provision.yml': line 32, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: \"Prepare the delivery_users object\"\n      ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: [{u'authorized_keys': u'{{ delivery_authorized_keys }}', u'group': u'{{ magento_webserver_group }}', u'name': u'{{ magento_project_user }}'}]: {{ http_group_name }}: 'http_group_name' is undefined"}

我知道有些东西未定义,但问题是,它是由其他人开发的,它应该可以完美运行,而这个其他人已经不存在了。这是provision.yml:

---
# load variables for each servers
- hosts:
    - dbservers
    - cacheservers
    - searchservers
    - webservers
  vars:
    ansible_user: "root"
  tasks:
    - include: includes/include-vars.yml

# Prepare the delivery authorized keys
- hosts:
    - dbservers
    - cacheservers
    - searchservers
    - webservers
  connection: local

  vars:
    ansible_user: "root"
    tmp_delivery_users:
      - name:   "{{ magento_project_user }}"
        group:  "{{ magento_webserver_group }}"
        authorized_keys: "{{ delivery_authorized_keys }}"

  tasks:
    - name: "Prepare the list of the authorized keys for delivery - Extra Keys"
      set_fact: delivery_authorized_keys="{{ delivery_authorized_extra_keys }}"

    - name: "Prepare the delivery_users object"
      set_fact: delivery_users="{{ tmp_delivery_users }}"

# add hosts alias on Servers
- hosts:
    - dbservers
    - cacheservers
    - searchservers
    - webservers
  vars:
    ansible_user: "root"
  tasks:
    - include: includes/init-hosts.yml
      with_items: "{{ specific_hosts|default([]) }}"

# add magento hosts alias on WebServers
- hosts:
    - webservers
  vars:
    ansible_user: "root"
  tasks:
    - include: includes/init-hosts.yml
      with_items: "{{ magento_server_alias|default([]) }}"

# Generic behaviors on all servers
- hosts:
    - dbservers
    - cacheservers
    - searchservers
    - webservers
  vars:
    ansible_user: "root"
  roles:
    - role: ansible-basicserver

# Generic usage of the ansible roles - DB Server
- hosts: dbservers
  vars:
    ansible_user: "root"
  roles:
    - role: ansible-mysql-server

# Generic usage of the ansible roles - Cache Server
- hosts: cacheservers
  vars:
    ansible_user: "root"
  roles:
    - {
        role: ansible-redis,
        redis_instance_name: "magento_cache",
        redis_setting_port:  "{{ magento_cache_port }}",
        redis_setting_save:  "{{ redis_setting_save_cache }}"
      }
    - {
        role: ansible-redis,
        redis_instance_name: "magento_session",
        redis_setting_port:  "{{ magento_cache_session_port }}",
        redis_setting_save:  "{{ redis_setting_save_session }}"
      }

# Generic usage of the ansible roles - Search Server
- hosts: searchservers
  vars:
    ansible_user: "root"
  roles:
    - role: ansible-elasticsearch

# Prepare php parameters
- hosts: webservers
  vars:
    ansible_user: "root"
  tasks:
    - include: includes/prepare-php-parameters.yml

# Generic usage of the ansible roles - Webserver Server
- hosts: webservers
  vars:
    ansible_user: "root"
  roles:
    - role: ansible-php
    - role: ansible-apache
    - role: ansible-varnish
    - role: ansible-nginx

# Specific usage of the ansible roles - Webserver Server - Dev Tools
- hosts: webservers
  vars:
    ansible_user: "root"
  roles:
    - { role: ansible-npm, when: magento_install_maildev or magento_install_grunt }
    - { role: ansible-maildev, when: magento_install_maildev }

  tasks:
  - name: "Install NPM package: grunt-cli"
    npm: name="grunt-cli" global=yes
    when: magento_install_grunt

  - name: "Add delivery user in groups"
    user:
      name: "{{ magento_project_user }}"
      groups: "{{ magento_delivery_groups }}"

  - name: "Create {{ magento_source_path }} folder"
    file:
      path:  "{{ magento_source_path }}"
      state: directory
      owner: "{{ magento_project_user }}"
      group: "{{ magento_project_group }}"
      mode:  "u=rwX,g=rX,o=rX"

# Specific task for Magento 2
  - name: "Check if Magento app/etc/env.php exists"
    stat:
      path: "{{ magento_source_path }}/app/etc/env.php"
    register: magento_app_etc_env

  - name: "Update app/etc/env.php configuration file"
    template:
      src: "templates/magento/env.php.j2"
      dest: "{{ magento_source_path }}/app/etc/env.php"
      owner: "{{ magento_project_user }}"
      group: "{{ magento_webserver_group }}"
      mode: "u=rw,g=rw,o=r"
    vars:
        magento_cache_database: "{{ magento_cache_database_for_run }}"
    when: magento_app_etc_env.stat.exists

# Update permissions
  - include: includes/permissions-tasks-full.yml

错误在那里触发:

  - name: "Prepare the delivery_users object"
  set_fact: delivery_users="{{ tmp_delivery_users }}"

所以,就像我说的,我知道 http_group_name 是未定义的,但我该如何定义呢?我该如何调试?

我注意到 http_group_name 是在 distro-vars 中定义的,其中每个 distri 都有一个 yml 文件。有 CentOS-7、Debian-8、Debian-9、RadHat-7 和 Ubuntu-16.04。但我的发行版是 Ubuntu 20.04。会不会是问题?我应该创建一个 Ubuntu-20.04.yml 吗? 谢谢

编辑: 我的 env.php.j2

    {% set document_root_is_pub = 'false' %}
{% if magento_mode == 'production' %}{% set document_root_is_pub = 'true' %}{% endif %}
<?php
return array(
    'db' => array(
        'connection' => array(
            'default' => array(
                'host' => '{{ magento_db_host }}',
                'dbname' => '{{ magento_db_name }}',
                'username' => '{{ magento_db_user }}',
                'password' => '{{ magento_db_password }}',
                'model' => 'mysql4',
                'engine' => 'innodb',
                'initStatements' => 'SET NAMES utf8;',
                'active' => '1',
                'driver_options' => array(PDO::MYSQL_ATTR_LOCAL_INFILE => true),
            ),
        ),
        'table_prefix' => '{{ magento_db_table_prefix }}',
    ),
    'backend' => array(
        'frontName' => '{{ magento_backend_frontname }}',
    ),
    'install' => array(
        'date' => '{{ magento_install_date }}',
    ),
    'crypt' => array(
        'key' => '{{ magento_crypt_key }}',
    ),
    'session' => array(
        'save' => 'redis',
        'redis' => array(
            'host' => '{{ magento_cache_session_host }}',
            'port' => '{{ magento_cache_session_port }}',
            'database' => '{{ magento_cache_session_database }}',
            'disable_locking' => '1',
        ),
    ),
    'cache' => array(
        'frontend' => array(
            'default' => array(
                'backend' => 'Cm_Cache_Backend_Redis',
                'id_prefix' => '{{ magento_cache_id_prefix }}',
                'backend_options' => array(
                    'server' => '{{ magento_cache_host }}',
                    'port' => '{{ magento_cache_port }}',
                    'persistent' => '',
                    'database' => '{{ magento_cache_database }}',
                    'force_standalone' => '0',
                    'connect_retries' => '1',
                    'read_timeout' => '10',
                    'automatic_cleaning_factor' => '0',
                    'compress_data' => '1',
                    'compress_tags' => '1',
                    'compress_threshold' => '20480',
                    'compression_lib' => 'gzip',
                ),
            ),
        ),
    ),
    'http_cache_hosts' => array(
{% set loop_index = 0 %}
{% for host in magento_http_cache_hosts %}
        {{ loop_index }} => array(
            'host' => '{{ host.host }}',
            'port' => '{{ host.port }}',
        ),
{% set loop_index = loop_index + 1 %}
{% endfor %}
    ),
    'MAGE_MODE' => '{{ magento_mode }}',
    'directories' => array(
        'document_root_is_pub' => {{ document_root_is_pub }},
    ),
    'queue' => array(
        'amqp' => array(
            'host' => '',
            'port' => '',
            'user' => '',
            'password' => '',
            'virtualhost' => '/',
            'ssl' => '',
        ),
    ),
    'resource' => array(
        'default_setup' => array(
            'connection' => 'default',
        ),
    ),
    'x-frame-options' => 'SAMEORIGIN',
    'cache_types' => array(
        'config' => 1,
        'layout' => 1,
        'block_html' => 1,
        'collections' => 1,
        'reflection' => 1,
        'db_ddl' => 1,
        'eav' => 1,
        'customer_notification' => 1,
        'target_rule' => 1,
        'full_page' => 1,
        'config_integration' => 1,
        'config_integration_api' => 1,
        'full_page' => 1,
        'translate' => 1,
        'config_webservice' => 1,
    ),
);

我这里没有任何“http_group_name”。这是错误吗?我应该在那里定义它吗?如果是这样,我该怎么做?

【问题讨论】:

  • 如果错误确实是由您显示的步骤触发的,那么您肯定无法在此处向我们展示某些内容。不过,我怀疑你在 templates/magento/env.php.j2 中使用了一个变量 http_group_name,而你确实忘记在你的剧本中设置了。
  • 错误指向:/home/jredor/projets/webstore/architecture/provisioning/provision.ymlline 32, column 7。如果您对此进行调查或更新问题,您可能会得到答案。
  • @β.εηοιτ.βε 我编辑了关于 http_group_name 的问题
  • @SeshadriC 就像我说的那样,错误第 32 行是以下行: - name: "Prepare the delivery_users object" 这就是我不明白的原因。但错误也谈到了 http_group_name,所以我查看了这个并编辑了问题
  • 看起来正在加载操作系统/发行版依赖变量。我认为您的发行版应该有一个 vars 文件。它记录在tips and tricks

标签: ansible


【解决方案1】:

问题出在发行版上。由于我在容器上使用了 debian 10,因此我在 debian 9 上创建了一个新容器,并且它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2021-07-31
    相关资源
    最近更新 更多