【问题标题】:Variable does not seem to be defined in Ansible playbookAnsible playbook 中似乎没有定义变量
【发布时间】:2017-01-04 15:57:34
【问题描述】:

以下用于为 Laravel 应用程序设置服务器的 Ansible playbook 可以正常工作:

---
- 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_dropletcreate_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"}

调试语句的使用已经证实,在第二个剧本中调用的角色中,domain 变量似乎没有被定义。我不确定为什么会这样。但是,如果我删除创建液滴的部分并针对现有液滴运行它,它似乎可以正常工作。

谁能明白为什么这显示为未定义?是不是和这些变量的作用域有关?

【问题讨论】:

    标签: yaml ansible ansible-playbook ansible-2.x


    【解决方案1】:

    和这些变量的作用域有关吗?

    是的,您的变量是播放绑定的,因此它们可用于第一次播放(在您提示它们的位置),而不能用于第二次播放。

    如果您需要变量在播放之间生存,则需要将其转换为宿主事实。
    例如,将post_tasks 添加到您的第一个游戏中:

    post_tasks:
      - set_fact:
          domain: '{{ domain }}'
        delegate_to: '{{ item }}'
        delegate_facts: true
        with_inventory_hostnames: launched
    

    这会将domain 事实添加到launched 组中的每个主机。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多