pytest-testinfra icon indicating copy to clipboard operation
pytest-testinfra copied to clipboard

Added the ability to get ansible settings from environment variables

Open degibenz opened this issue 5 years ago • 1 comments

Tools

  • terraform
  • gitlab-ci
  • terraform-inventory

Motivation So, as I use terraforms to manage the server life cycle, my project does not have a “static” inventory file. Inventory file I get using terraform-inventory. For example like this

- ansible-playbook --inventory=/usr/local/bin/terraform-inventory ${ansible_service_path}

At this moment, by using this library, there is no functionality to instantly redefine any variable or its value, there is only ansible-cli functionality, but it's not enough. In my pull-request there is a functional which may not be suitable for everyone, but it helps to prepare and launch tests more flexibly. For example, tests in my project look like this:

ansible/roles/kibana/tasks/mail.yml

- name: put elasticsearch.repo
  copy:
    src: elasticsearch.repo
    dest: /etc/yum.repos.d/elasticsearch.repo
    owner: root
    group: root
    mode: '644'
  when: action == "install"

- yum:
    name: "{{ packages }}"
    update_cache: yes
    state: present
    enablerepo: "elasticsearch"
  register: elasticinstalled
  name: installs kibana
  when: action == "install"

main.tf

resource "openstack_compute_instance_v2" "kibana_server" {
  name        = "kibana_server"
  flavor_name = var.flavor_name

  key_pair = var.key_pair_name

  security_groups = [
    data.consul_keys.consul_sg.var.id,
    data.consul_keys.ssh_sg.var.id,
    openstack_compute_secgroup_v2.kibana_sg.id
  ]

  availability_zone = var.region

  network {
    uuid = var.network_id
  }

  block_device {
    uuid                  = openstack_blockstorage_volume_v1.kibana_server_volume.id
    source_type           = "volume"
    boot_index            = 0
    destination_type      = "volume"
    delete_on_termination = true
  }

}

variables.tf

variable "image_name" {
  default = "CentOS-7.4-Standard"
}

variable "flavor_name" {
  default = "Standard-2-4-50"
}

variable "ssh_user_name" {
  default = "centos"
}

.gitlab-ci.yml

test-infra:
  stage: test
  variables:
    ANSBILE_FROM_ENV: 'true'
    ansible_user: centos
    ansible_become_user: centos
    ansible_become: 'true'
    ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
    ansible_ssh_extra_args: '-o StrictHostKeyChecking=no'
  script:
   - terraform init -input=false
   - terraform apply -auto-approve -input=false
   - terraform-inventory -inventory ./ >> inventory/test-${CI_PIPELINE_ID}
   - ansible-playbook --inventory=inventory/test-${CI_PIPELINE_ID} -e "action=install"
   - make pytest ROLE_NAME=kibana INVENT_FILE=inventory/test-${CI_PIPELINE_ID}
  after_script:
    - terraform destroy -auto-approve
  tags:
    - bastion-mcs

degibenz avatar Feb 11 '20 18:02 degibenz

Build succeeded (third-party-check pipeline).

opendev-zuul[bot] avatar Feb 11 '20 19:02 opendev-zuul[bot]