用分子覆盖 defaults/main.yml 变量

override defaults/main.yml variables with molecule

我遇到的问题是 运行ning ansible 脚本的默认值通常存储在 defaults/main.yml 中,但我还没有找到 override 的方法他们。我正在使用分子进行测试,我想要一种将变量传递到我的 molecule/default/molecule.ymlmolecule/default/playbook.yml 的方法,以便我可以动态设置 phpVersion.

# defaults/main.yml
phpVersion: 7.0
#tasks/main.yml
---
- name: Check if OS is Ubuntu LTS
  fail: msg="Server must be Ubuntu LTS"
  when: ansible_distribution != 'Ubuntu'
    or (ansible_distribution_version != '14.04'
      and ansible_distribution_version != '16.04'
      and ansible_distribution_version != '18.04')

- name: Get default PHP version
  shell: >
    set -o pipefail && \
    /usr/bin/php -v | grep 'PHP '
  register: php_installed_default_version
  failed_when: false
  changed_when: false

- name: Add ondrej repo to allow multiple PHP versions
  apt_repository:
    repo: "ppa:ondrej/php"
    update_cache: yes

- name: Include php version specific playbook
  include_tasks: "php{{ item }}.yml"
  with_items: "{{ php_versions }}"
  when: php_versions is defined

- name: Set default PHP version to 7.0 (PHP CLI)
  shell: update-alternatives --set php /usr/bin/php{{ phpVersion }}
  register: command_result
  when: phpVersion == 7.0
    and php_installed_default_version is defined
    and php_installed_default_version.stdout is defined
    and (not php_installed_default_version.stdout | regex_search("PHP 7\.0"))
  changed_when: command_result.stdout == "to provide"

- name: Set default PHP version to 7.1 (PHP CLI)
  shell: update-alternatives --set php /usr/bin/php{{ phpVersion }}
  register: command_result
  when: phpVersion == 7.1
    and php_installed_default_version is defined
    and php_installed_default_version.stdout is defined
    and (not php_installed_default_version.stdout | regex_search("PHP 7\.1"))
  changed_when: command_result.stdout == "to provide"

- name: Set default PHP version to 7.2 (PHP CLI)
  shell: update-alternatives --set php /usr/bin/php{{ phpVersion }}
  register: command_result
  when: phpVersion == 7.2
    and php_installed_default_version is defined
    and php_installed_default_version.stdout is defined
    and (not php_installed_default_version.stdout | regex_search("PHP 7\.2"))
  changed_when: command_result.stdout == "to provide"

# find out what update alternatives spits out and parse some of that through the registered variable: did this task change or not.
 - name: Set default PHP version to 7.3 (PHP CLI)
  shell: update-alternatives --set php /usr/bin/php{{ phpVersion }}
  register: command_result
  when: phpVersion == 7.3
    and php_installed_default_version is defined
    and php_installed_default_version.stdout is defined
    and (not php_installed_default_version.stdout | regex_search("PHP 7\.3"))
  changed_when: command_result.stdout == "to provide"
# molecule/default/molecule.yml
---
dependency:
  name: galaxy
driver:
  name: docker
lint:
  name: yamllint
  options:
    config-data:
      ignore: venv
platforms:
  - name: instance
    image: "superelectron/docker-ubuntu-ansible-php:${MOLECULE_DISTRO:-ubuntu1804-php70}"
    command: ${MOLECULE_DOCKER_COMMAND:-""}
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:ro
    privileged: true
    pre_build_image: true
    instance_raw_config_args:
      - "vm.network 'forwarded_port', guest: 80, host: 8088"
provisioner:
  name: ansible
  log: true
  lint:
    name: ansible-lint
verifier:
  name: testinfra
  lint:
    name: flake8
# molecule/default/playbook.yml
---
- name: Converge
  hosts: all
  roles:
    - role: ansible-role-php
  vars:
    phpVersion: 7.0

molecule.ymlplaybook.yml 你可以看到传递这样的东西来设置 phpVersion 是多么棒:

phpVersion=7.0 MOLECULE_DISTRO=ubuntu1804-php70 molecule test --destroy=never

并让 playbook.yml 设置这样的变量:

vars:
    phpVersion: ${phpVersion}

问题是,如何使用分子动态设置 tasks/main.yml 中使用的变量,以便我的测试动态设置 phpVersion?

编辑:实际上可能比下面做得更好,但我没有时间对其进行全面测试。下面的解决方案仍然有效。选择最适合你的。

您实际上可以将环境变量从 molecule.yml 传递给分子 ansible provisionner(请参阅 doc,它本身接受环境变量以回退到默认值的模板。

这是新想法。下面的解决方案在分子剧本中更清晰,但需要两个不同的环境变量(一个用于命令行,另一个由分子传递给剧本)。

molecule.yml

provisioner:
  name: ansible
  log: true
  lint:
    name: ansible-lint
  env:
    MY_PHP: ${MOLECULE_PHP:-"7.0"}

在你的分子剧本中:

vars:
   phpVersion: "{{ lookup('env', 'MY_PHP') }}"

你的命令:

# Default
molecule converge

# Override
MOLECULE_PHP=7.4 molecule converge

这是一种解决方案。它有两个小缺点:

  • env lookup returns 一个空字符串表示不存在的环境变量。您不能使用 default 过滤器,因此寻找回退值有点冗长
  • 您必须在剧本变量中重复角色的默认值。

在你的分子剧本中:

vars:
  phpOverride: "{{ lookup('env', 'MY_PHP') }}"
  phpVersion: "{{ (phpOverride | length > 0) | ternary(phpOverride, '7.0') }}"

并在您的命令行中:

# For default value
molecule converge

# With override
MY_PHP=7.4 molecule converge