变量似乎没有在 Ansible 剧本中定义
Variable does not seem to be defined in Ansible playbook
以下用于为 Laravel 应用程序设置服务器的 Ansible 剧本工作正常:
---
- name: Set up a standard Laravel install
hosts: localhost
vars_prompt:
- name: "domain"
prompt: "Domain name"
private: no
- name: "dbname"
prompt: "Database name"
private: no
- name: "dbuser"
prompt: "Database username"
private: no
- name: "dbpassword"
prompt: "Database password"
private: yes
roles:
- create_droplet
- create_domain
- name: Install dependencies
hosts: launched
roles:
- upgrade
- utilities
- users
- nginx-php
- composer
- nginx_firewall
- redis
- postgres
- git
以下用于设置 Wordpress 安装的类似方法没有:
---
- name: Set up Wordpress with Apache, Memcached and Varnish
hosts: localhost
vars_prompt:
- name: "domain"
prompt: "Domain name"
private: no
- name: "title"
prompt: "Wordpress title"
private: no
- name: "email"
prompt: "Wordpress email"
private: no
- name: "user"
prompt: "Admin username"
private: no
- name: "pass"
prompt: "Admin password"
private: yes
roles:
- create_droplet
- create_domain
- name: Install dependencies
hosts: launched
roles:
- upgrade
- utilities
- users
- apache
- varnish
- memcached
- mysql
- wordpress
两个剧本都使用 create_droplet
和 create_domain
角色在 Digital Ocean 上设置了一个新的 droplet,并将其添加到 launched
组。但是,第二个剧本中提示的变量似乎没有定义,如以下错误消息所示:
TASK [wordpress : Add user "wordpress", belonging to group "wordpress" and having a home dir of /var/www] ***
fatal: [<IP_ADDRESS_REDACTED>]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'pass' is undefined\n\nThe error appears to have been in '/home/matthew/Projects/ansible-setup/playbooks/roles/wordpress/tasks/main.yml': line 28, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Add user \"wordpress\", belonging to group \"wordpress\" and having a home dir of /var/www\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nunbalanced quotes. If starting a value with a quote, make sure the\nline ends with the same set of quotes. For instance this arbitrary\nexample:\n\n foo: \"bad\" \"wolf\"\n\nCould be written as:\n\n foo: '\"bad\" \"wolf\"'\n"}
调试语句的使用已确认在第二个剧本中调用的 none 个角色中 domain
变量似乎已定义。我不确定为什么会这样。但是,如果我删除创建 droplet 的部分并 运行 它针对现有的 droplet,它似乎工作正常。
谁能看出为什么它显示为未定义?是不是和这些变量的作用域有关?
Is it something to do with the scope of these variables?
是的,您的变量是播放绑定的,因此它们可用于第一次播放(在您提示它们的地方),而对于第二次播放不可用。
如果您需要变量在播放之间生存,则需要将其转换为主机事实。
例如,将 post_tasks
添加到您的第一个游戏中:
post_tasks:
- set_fact:
domain: '{{ domain }}'
delegate_to: '{{ item }}'
delegate_facts: true
with_inventory_hostnames: launched
这会将 domain
事实添加到 launched
组中的每个主机。
以下用于为 Laravel 应用程序设置服务器的 Ansible 剧本工作正常:
---
- name: Set up a standard Laravel install
hosts: localhost
vars_prompt:
- name: "domain"
prompt: "Domain name"
private: no
- name: "dbname"
prompt: "Database name"
private: no
- name: "dbuser"
prompt: "Database username"
private: no
- name: "dbpassword"
prompt: "Database password"
private: yes
roles:
- create_droplet
- create_domain
- name: Install dependencies
hosts: launched
roles:
- upgrade
- utilities
- users
- nginx-php
- composer
- nginx_firewall
- redis
- postgres
- git
以下用于设置 Wordpress 安装的类似方法没有:
---
- name: Set up Wordpress with Apache, Memcached and Varnish
hosts: localhost
vars_prompt:
- name: "domain"
prompt: "Domain name"
private: no
- name: "title"
prompt: "Wordpress title"
private: no
- name: "email"
prompt: "Wordpress email"
private: no
- name: "user"
prompt: "Admin username"
private: no
- name: "pass"
prompt: "Admin password"
private: yes
roles:
- create_droplet
- create_domain
- name: Install dependencies
hosts: launched
roles:
- upgrade
- utilities
- users
- apache
- varnish
- memcached
- mysql
- wordpress
两个剧本都使用 create_droplet
和 create_domain
角色在 Digital Ocean 上设置了一个新的 droplet,并将其添加到 launched
组。但是,第二个剧本中提示的变量似乎没有定义,如以下错误消息所示:
TASK [wordpress : Add user "wordpress", belonging to group "wordpress" and having a home dir of /var/www] ***
fatal: [<IP_ADDRESS_REDACTED>]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'pass' is undefined\n\nThe error appears to have been in '/home/matthew/Projects/ansible-setup/playbooks/roles/wordpress/tasks/main.yml': line 28, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Add user \"wordpress\", belonging to group \"wordpress\" and having a home dir of /var/www\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nunbalanced quotes. If starting a value with a quote, make sure the\nline ends with the same set of quotes. For instance this arbitrary\nexample:\n\n foo: \"bad\" \"wolf\"\n\nCould be written as:\n\n foo: '\"bad\" \"wolf\"'\n"}
调试语句的使用已确认在第二个剧本中调用的 none 个角色中 domain
变量似乎已定义。我不确定为什么会这样。但是,如果我删除创建 droplet 的部分并 运行 它针对现有的 droplet,它似乎工作正常。
谁能看出为什么它显示为未定义?是不是和这些变量的作用域有关?
Is it something to do with the scope of these variables?
是的,您的变量是播放绑定的,因此它们可用于第一次播放(在您提示它们的地方),而对于第二次播放不可用。
如果您需要变量在播放之间生存,则需要将其转换为主机事实。
例如,将 post_tasks
添加到您的第一个游戏中:
post_tasks:
- set_fact:
domain: '{{ domain }}'
delegate_to: '{{ item }}'
delegate_facts: true
with_inventory_hostnames: launched
这会将 domain
事实添加到 launched
组中的每个主机。