首页 文章

变量似乎没有在Ansible playbook中定义

提问于
浏览
1

以下用于为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"}

使用调试语句已经确认,在第二个playbook中调用的任何角色中都没有定义 domain 变量 . 我不知道为什么会这样 . 但是,如果我删除创建Droplet的部分并对现有的Droplet运行它,它似乎工作正常 .

任何人都可以看到为什么这显示为未定义?这与这些变量的范围有关吗?

1 回答

  • 4

    这与这些变量的范围有关吗?

    是的,您的变量是播放限制的,因此它们可用于第一次播放(您提示它们)并且不可用于第二次播放 .

    如果你需要变量来在游戏之间存活,你需要将它转换为主机事实 .
    例如,在第一次播放时添加 post_tasks

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

    这将为 launched 组中的每个主机添加 domain 事实 .

相关问题