Tohutohu: me pehea te whakamatautau i nga mahi e tika ana me te mohio mo nga raru i mua i te whakaputa

Всем Привет!

Kei te mahi ahau hei miihini DevOps i roto i te ratonga toha hotera. Ostrovok.ru. I roto i tenei tuhinga e hiahia ana ahau ki te korero mo o maatau wheako ki te whakamatautau i nga mahi Ansible.

I Ostrovok.ru, ka whakamahia e matou te ansible hei kaiwhakahaere whirihoranga. I tata nei ka tae mai matou ki te hiahia ki te whakamatautau i nga mahi, engari, i te mea i puta mai, kaore he maha o nga taputapu mo tenei - ko te mea tino rongonui, pea, ko te anga Molecule, no reira ka whakatau matou ki te whakamahi. Engari i puta mai he wahangu ana tuhinga mo te maha o nga mahanga. Kaore i kitea e matou he aratohu tino taipitopito i roto i te reo Ruhia, no reira ka whakatau matou ki te tuhi i tenei tuhinga.

Tohutohu: me pehea te whakamatautau i nga mahi e tika ana me te mohio mo nga raru i mua i te whakaputa

rāpoi ngota

Ngota ngota — he anga hei awhina i nga mahi Ansible.

Whakaahuatanga ngawari: Ka waihangahia e te Molecule he tauira i runga i te papaaapapa ka tohua e koe (kapua, miihini mariko, ipu; mo etahi atu korero, tirohia te waahanga atekōkiri), ka whakahaere i to mahi ki runga, ka whakahaere i nga whakamatautau me te muku i te tauira. Mena he rahua tetahi o nga kaupae, ma te Molecule e whakamohio atu ki a koe.

Ano atu.

He ahua o te ariā

Whakaarohia nga hinonga matua e rua o te Molecule: Tauari me te Atekōkiri.

Whakaari

Kei roto i te tuhinga he whakaahuatanga mo te aha, kei hea, me pehea, me pehea te raupapa ka mahia. Ka taea e tetahi mahi te maha o nga tuhinga tuhi, a, ko ia waahanga he whaiaronga i te huarahi <role>/molecule/<scenario>, kei roto nga whakaahuatanga o nga mahi e hiahiatia ana mo te whakamatautau. Me whai tuhinga default, ka hangaia aunoa mena ka arawhiti koe i te mahi ma te whakamahi i te Molecule. Ko nga ingoa o nga tuhinga e whai ake nei kei to whakaaro.

Ko te raupapa o nga mahi whakamatautau i roto i te tuhinga ka kiia Tuhinga, a ma te taunoa ko:

(Kua tohuhia nga taahiraa ?, i pekehia ma te taunoa mena kaore i tohua e te kaiwhakamahi)

  • lint - rere linters. Ma te taunoa ka whakamahia yamllint и flake8,
  • destroy - te whakakore i nga tauira mai i te whakarewatanga whakamutunga o te Molecule (mehemea kei kona),
  • dependency? - te whakauru i te ti'aturi o te mahi i whakamatauria,
  • syntax - te taki i te wetereo o te mahi e whakamahi ana ansible-playbook --syntax-check,
  • create - te hanga tauira,
  • prepare? — te whakarite i te tauira; hei tauira tirohia / tāuta python2
  • converge - te whakarewatanga o te pukapuka takaro e whakamatautauria ana,
  • idempotence - ka whakaara ano i te pukapuka takaro mo te whakamatautau ngoikore,
  • side_effect? - nga mahi kaore e pa ana ki te mahi, engari e tika ana mo nga whakamatautau,
  • verify - whakahaere whakamātautau o te whirihoranga hua whakamahi testinfra(taunoa) /goss/inspec,
  • cleanup? - (i roto i nga putanga hou) - te tikanga, "horoia" nga hanganga o waho e pa ana ki te Molecule,
  • destroy - Te whakakore i tetahi tauira.

Kei roto i tenei raupapa te nuinga o nga keehi, engari ka taea te whakarereke mehemea e tika ana.

Ka taea e ia o nga kaupae o runga ake te whakahaere motuhake molecule <command>. Engari kia mohio koe mo ia cli-whakahau tera pea he raupapa o nga mahi, ka kitea e koe ma te mahi. molecule matrix <command>. Hei tauira, i te wa e whakahaere ana i te whakahau converge (te whakahaere i te pukapuka takaro kua whakamatauria) ka mahia nga mahi e whai ake nei:

$ molecule matrix converge
...
└── default         # название сценария
    ├── dependency  # установка зависимостей
    ├── create      # создание инстанса
    ├── prepare     # преднастройка инстанса
    └── converge    # прогон плейбука

Ka taea te whakatika te raupapa o enei mahi. Mena kua oti kee tetahi mea mai i te rarangi, ka pekehia. Ko te ahua o naianei, me te whirihora tauira, ka rongoa ki te raarangi Molecule $TMPDIR/molecule/<role>/<scenario>.

Tāpirihia nga kaupae me ? ka taea e koe, ma te whakaahua i nga mahi e hiahiatia ana i roto i te whakatakotoranga pukapuka-ansible-playbook, me te hanga i te ingoa konae kia rite ki te taahiraa: prepare.yml/side_effect.yml. Me tumanako kei roto enei kōnae Molecule ki te kōpaki hōtuhi.

atekōkiri

Ko te taraiwa he hinonga ka hangaia nga tauira mo nga whakamatautau.
Ko te rarangi o nga taraiwa paerewa kei a Molecule nga tauira kua rite: Azure, Docker, EC2, GCE, LXC, LXD, OpenStack, Vagrant, Delegated.

I te nuinga o nga wa, ko nga tauira he konae create.yml и destroy.yml i roto i te kōpaki hōtuhi, e whakaahua ana i te waihanga me te mukunga o te tauira.
Ko nga tuunga ko Docker me Vagrant, na te mea ka puta nga taunekeneke me o raatau waahanga me te kore o nga konae kei runga ake nei.

He pai ki te whakanui i te taraiwa kua tukuna, na te mea ka whakamahia, ko te mahi anake me te whirihoranga tauira ka whakaahuahia i roto i te hanga tauira me te whakakore i nga konae; ko te toenga ma te miihini.

Ko te taraiwa taunoa ko Docker.

Inaianei ka anga taatau ki te whakaharatau me te whakaaro ki etahi atu waahanga kei reira.

Getting Started

Hei "kia ora te ao" ka whakamatauhia e matou he mahi whakaurunga nginx ngawari. Me kowhiria te kaitautoko hei taraiwa - Ki taku whakaaro kua whakauruhia e te nuinga o koutou (me te mahara ko te kaihopu te taraiwa taunoa).

Kia takatu tatou virtualenv ka whakauru ki roto molecule:

> pip install virtualenv
> virtualenv -p `which python2` venv
> source venv/bin/activate
> pip install molecule docker  # molecule установит ansible как зависимость; docker для драйвера

Ko te mahi e whai ake nei ko te arawhiti i tetahi mahi hou.
Ko te whakaurunga o tetahi mahi hou, me te tuhinga hou, ka mahia ma te whakamahi i te whakahau molecule init <params>:

> molecule init role -r nginx
--> Initializing new role nginx...
Initialized role in <path>/nginx successfully.
> cd nginx
> tree -L 1
.
├── README.md
├── defaults
├── handlers
├── meta
├── molecule
├── tasks
└── vars

6 directories, 1 file

I puta he mahi ansible. I tua atu, ko nga taunekeneke katoa me CLI Molecules he mea hanga mai i te putake o te mahi.

Kia kite tatou he aha kei roto i te raarangi mahi:

> tree molecule/default/
molecule/default/
├── Dockerfile.j2  # Jinja-шаблон для Dockerfile
├── INSTALL.rst.   # Немного информации об установке зависимостей сценария
├── molecule.yml   # Файл конфигурации
├── playbook.yml   # Плейбук запуска роли
└── tests          # Директория с тестами стадии verify
    └── test_default.py

1 directory, 6 files

Kia tātarihia te whirihora molecule/default/molecule.yml (whakakapi anake te ahua docker):

---
dependency:
  name: galaxy
driver:
  name: docker
lint:
  name: yamllint
platforms:
  - name: instance
    image: centos:7
provisioner:
  name: ansible
  lint:
    name: ansible-lint
scenario:
  name: default
verifier:
  name: testinfra
  lint:
    name: flake8

te whakawhirinaki

Ko tenei waahanga e whakaatu ana i te puna o nga whakawhirinakitanga.

Kōwhiringa pea: tupuni, , anga.

He anga whakahau noa te Anga ki te kore te tupuni me te kirikiri e kapi i o hiahia.

E kore ahau e noho roa ki konei, kua nui te korero i roto tuhinga.

atekōkiri

Ingoa taraiwa. Ki a matou he docker tenei.

kiko

He yamllint te papa.

Ko nga whiringa whaihua i roto i tenei waahanga o te whirihora ko te kaha ki te tautuhi i tetahi konae whirihoranga mo te yamllint, nga taurangi taiao whakamua, te whakakore ranei i te kopae:

lint:
  name: yamllint
  options:
    config-file: foo/bar
  env:
    FOO: bar
  enabled: False

mahere

He whakaahua i te whirihoranga o nga tauira.
I roto i te take mo te docker hei taraiwa, ka huri te Molecule ki runga i tenei waahanga, a kei te waatea ia huānga o te rarangi Dockerfile.j2 hei taurangi item.

I roto i te take o te taraiwa i roto i nei create.yml и destroy.yml, kei te waatea te waahanga i roto i a raatau hei molecule_yml.platforms, me nga whitiwhitinga kei runga kua whakamaramatia i roto i enei konae.

I te mea ka tukuna e Molecule te whakahaere tauira ki nga waahanga Ansible, me rapu koe mo te rarangi o nga tautuhinga ka taea ki reira. Mo Docker, hei tauira, ka whakamahia te kōwae docker_container_module. Ko ēhea kōwae e whakamahia ana i ētahi atu taraiwa ka kitea i roto tuhinga.

Ka kitea ano nga tauira mo te whakamahi i nga momo taraiwa i roto i nga whakamatautau o te Molecule ake.

Me whakakapi i konei henehe:7 i runga i Ubuntu.

kaiwhakawhiwhi

Ko "Kaiwhakarato" te hinonga e whakahaere ana i nga tauira. Mo te Molecule, ka taea tenei; kaore i te whakamaherehia te tautoko mo etahi atu, no reira ka taea tenei waahanga, me te rahui, ka kiia he whirihoranga ansible roa.
He maha nga mea ka taea e koe te tohu i konei, engari ka whakanui ahau i nga kaupapa matua, ki taku whakaaro:

  • pukamahi: ka taea e koe te tautuhi ko wai nga pukapuka takaro hei whakamahi i etahi waahanga.

provisioner:
  name: ansible
  playbooks:
    create: create.yml
    destroy: ../default/destroy.yml
    converge: playbook.yml
    side_effect: side_effect.yml
    cleanup: cleanup.yml

provisioner:
  name: ansible
  config_options:
    defaults:
      fact_caching: jsonfile
    ssh_connection:
      scp_if_ssh: True

  • hononga_whiringa: kōwhiringa hono

provisioner:
  name: ansible  
  connection_options:
    ansible_ssh_common_args: "-o 'UserKnownHostsFile=/dev/null' -o 'ForwardAgent=yes'"

  • kōwhiringa: Ko nga whiringa ka taea me nga taurangi taiao

provisioner:
  name: ansible  
  options:
    vvv: true
    diff: true
  env:
    FOO: BAR

tauira

Taitara me te whakaahuatanga o nga raupapa tuhinga.
Ka taea e koe te huri i te matrix mahi taunoa o tetahi tono ma te taapiri i te ki <command>_sequence a hei uara mo taua mea, te tautuhi i te rarangi o nga hikoinga e hiahiatia ana.
Me kii e hiahia ana tatou ki te huri i te raupapa o nga mahi i te wa e whakahaere ana i te whakahau whakahaere pukapuka takaro: molecule converge

# изначально:
# - dependency
# - create
# - prepare
# - converge
scenario:
  name: default
  converge_sequence:
    - create
    - converge

tirohia

Te whakarite anga mo nga whakamatautau me te papa mo taua mea. Ma te taunoa, ka whakamahia te kopae testinfra и flake8. Ko nga whiringa ka taea te rite ki runga ake:

verifier:
  name: testinfra
  additional_files_or_dirs:
    - ../path/to/test_1.py
    - ../path/to/test_2.py
    - ../path/to/directory/*
  options:
    n: 1
  enabled: False
  env:
    FOO: bar
  lint:
    name: flake8
    options:
      benchmark: True
    enabled: False
    env:
      FOO: bar

Ka hoki ano ki to tatou mahi. Me whakatika te konae tasks/main.yml ki tenei ahua:

---
- name: Install nginx
  apt:
    name: nginx
    state: present

- name: Start nginx
  service:
    name: nginx
    state: started

A taapirihia nga whakamatautau ki molecule/default/tests/test_default.py

def test_nginx_is_installed(host):
    nginx = host.package("nginx")
    assert nginx.is_installed

def test_nginx_running_and_enabled(host):
    nginx = host.service("nginx")
    assert nginx.is_running
    assert nginx.is_enabled

def test_nginx_config(host):
    host.run("nginx -t")

Ka mutu, ka noho noa ki te oma (mai i te putake o te mahi, me whakamahara ahau ki a koe):

> molecule test

Te whakapau roa i raro i te kaipahua:

--> Validating schema <path>/nginx/molecule/default/molecule.yml.
Validation completed successfully.
--> Test matrix

└── default
    ├── lint
    ├── destroy
    ├── dependency
    ├── syntax
    ├── create
    ├── prepare
    ├── converge
    ├── idempotence
    ├── side_effect
    ├── verify
    └── destroy

--> Scenario: 'default'
--> Action: 'lint'
--> Executing Yamllint on files found in <path>/nginx/...
Lint completed successfully.
--> Executing Flake8 on files found in <path>/nginx/molecule/default/tests/...
Lint completed successfully.
--> Executing Ansible Lint on <path>/nginx/molecule/default/playbook.yml...
Lint completed successfully.
--> Scenario: 'default'
--> Action: 'destroy'

    PLAY [Destroy] *****************************************************************

    TASK [Destroy molecule instance(s)] ********************************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    TASK [Wait for instance(s) deletion to complete] *******************************
    ok: [localhost] => (item=None)
    ok: [localhost]

    TASK [Delete docker network(s)] ************************************************

    PLAY RECAP *********************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0

--> Scenario: 'default'
--> Action: 'dependency'
Skipping, missing the requirements file.
--> Scenario: 'default'
--> Action: 'syntax'

    playbook: <path>/nginx/molecule/default/playbook.yml

--> Scenario: 'default'
--> Action: 'create'

    PLAY [Create] ******************************************************************

    TASK [Log into a Docker registry] **********************************************
    skipping: [localhost] => (item=None)

    TASK [Create Dockerfiles from image names] *************************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    TASK [Discover local Docker images] ********************************************
    ok: [localhost] => (item=None)
    ok: [localhost]

    TASK [Build an Ansible compatible image] ***************************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    TASK [Create docker network(s)] ************************************************

    TASK [Create molecule instance(s)] *********************************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    TASK [Wait for instance(s) creation to complete] *******************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    PLAY RECAP *********************************************************************
    localhost                  : ok=5    changed=4    unreachable=0    failed=0

--> Scenario: 'default'
--> Action: 'prepare'
Skipping, prepare playbook not configured.
--> Scenario: 'default'
--> Action: 'converge'

    PLAY [Converge] ****************************************************************

    TASK [Gathering Facts] *********************************************************
    ok: [instance]

    TASK [nginx : Install nginx] ***************************************************
    changed: [instance]

    TASK [nginx : Start nginx] *****************************************************
    changed: [instance]

    PLAY RECAP *********************************************************************
    instance                   : ok=3    changed=2    unreachable=0    failed=0

--> Scenario: 'default'
--> Action: 'idempotence'
Idempotence completed successfully.
--> Scenario: 'default'
--> Action: 'side_effect'
Skipping, side effect playbook not configured.
--> Scenario: 'default'
--> Action: 'verify'
--> Executing Testinfra tests found in <path>/nginx/molecule/default/tests/...
    ============================= test session starts ==============================
    platform darwin -- Python 2.7.15, pytest-4.3.0, py-1.8.0, pluggy-0.9.0
    rootdir: <path>/nginx/molecule/default, inifile:
    plugins: testinfra-1.16.0
collected 4 items

    tests/test_default.py ....                                               [100%]

    ========================== 4 passed in 27.23 seconds ===========================
Verifier completed successfully.
--> Scenario: 'default'
--> Action: 'destroy'

    PLAY [Destroy] *****************************************************************

    TASK [Destroy molecule instance(s)] ********************************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    TASK [Wait for instance(s) deletion to complete] *******************************
    changed: [localhost] => (item=None)
    changed: [localhost]

    TASK [Delete docker network(s)] ************************************************

    PLAY RECAP *********************************************************************
    localhost                  : ok=2    changed=2    unreachable=0    failed=0

I whakamatauria to maatau mahi ngawari kaore he raru.
He mea tika kia mahara mena ka puta nga raru i te wa e mahi ana molecule test, na, ki te kore koe i whakarereke i te raupapa taunoa, ka mukua e te Molecule te tauira.

He pai nga whakahau e whai ake nei mo te patuiro:

> molecule --debug <command> # debug info. При обычном запуске Молекула скрывает логи.
> molecule converge          # Оставляет инстанс после прогона тестируемой роли.
> molecule login             # Зайти в созданный инстанс.
> molecule --help            # Полный список команд.

Tūnga Tāianei

Ko te taapiri i tetahi tuhinga hou ki tetahi mahi o naianei ko mai i te raarangi mahi me nga whakahau e whai ake nei:

# полный список доступных параметров
> molecule init scenarion --help
# создание нового сценария
> molecule init scenario -r <role_name> -s <scenario_name>

Mena koinei te tuhinga tuatahi i roto i te mahi, katahi ko te tawhā -s ka taea te waiho na te mea ka hanga he tuhinga default.

mutunga

Kei te kite koe, ehara i te mea tino uaua te Molecule, a, ka whakamahi koe i o ake tauira, ka taea e koe te whakaiti i te horahanga o te tuhinga hou hei whakatika i nga taurangi i roto i nga pukapuka takaro mo te hanga me te whakakore i nga tauira. He pai te whakauru o te ngota ngota ki nga punaha CI, ka taea e koe te whakanui ake i te tere o te whanaketanga ma te whakaiti i te waa mo te whakamatautau a-ringa o nga pukapuka takaro.

Mauruuru koe mo to aro. Mena he wheako koe ki te whakamatautau i nga mahi e tika ana, a kaore e pa ana ki te Molecule, korero mai ki a maatau i roto i nga korero!

Source: will.com

Tāpiri i te kōrero