似乎没有在Ansible playbook

时间:2016-08-28 14:34:38

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

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

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

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

1 个答案:

答案 0 :(得分:4)

  

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

是的,你的变量是游戏限制的,所以它们可用于第一次播放(你提示它们)并且不可用于第二次播放。

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

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

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