Ansible Developer Filament

From tannerjc wiki
Jump to: navigation, search

Purpose

Ansible has a lot of documentation and is increasingly building out development docs, but not everyone learns the same way. This page provides self-guided labs which are intended to teach new Ansible developers the ins and outs of writing code and working with the github PR process and shippable CI system. It is expected that the developer does a commit+push and waits for tests to run after each step in the process to learn the behavior of shippable tests and ansibot. You are expected to see a lot of errors and failures through this process. Please use the docs, the sourcecode, the community and the Ansible core team (in that order) if you find yourself stuck at any point.

NOTE: Between each step of the pullrequest process, you may need to wait 10-15 minutes for the bot to notice new changes and apply new labels. Shippable is often overloaded as well, so tests may take 30-40 minutes to run.

NOTE: This is not in the official docs because it was rejected. https://github.com/ansible/ansible/pull/26921

Level 1: module development

hello world

  1. fork ansible
  2. checkout the fork
  3. create a feature branch named FILAMENT_MODULE
  4. create a new module file lib/ansible/modules/system/filament.py
  5. put "hello world" in filament.py
  6. git add and commit the new file
  7. push the commit to the feature branch
  8. open the branch on github.com and send it as a pullrequest to ansible/ansible
  9. wait for ansibot to triage the issue and for shippable to run the tests
  10. read the shippable test logs to learn how to run the tests locally
  11. fix all test failures locally
  12. commit and push your changes

debugging modules

  1. set a variable named "foo" in the module
  2. use the python-q library to have the module log the value of "foo"
  3. set a breakpoint in the module with pdb/epdb
  4. run the module with test-module
    • get to the breakpoint
    • print "foo"
  5. run the module with ansible-playbook
    • get to the breakpoint
    • print "foo"
  6. commit and push the module with the breakpoint added to see CI test failures.
  7. remove the breakpoint and then commit and push again.

integration testing

  1. Create, commit and push an empty file named test/integration/targets/filament/tasks/main.yml
  2. Make sure the test is being run in shippable.
  3. Observe the test failures and re-run the test locally with ansible-test.
  4. Fill in main.yml with 2 tasks
    • call the filament module and register the result
    • assert that the result contains a key "foo" with a value of "bar"
  5. Fix the module so that the test passes by running the test locally with ansible-test.
  6. Commit and push your changes.

unit testing

  1. add a new function to the module that adds two integeters
  2. write a unit test to assert that the function returns the correct value for ints/strings/bools

module_util

  1. echo "hello world" > lib/ansible/module_utils/filament_utils.py
  2. make the filament module import the function "foobar" from filament_utils
  3. fix any shippable errors

action plugins

  1. make an action plugin that changes "bar" to "!bar"

Level 2: lookups

  1. create a new feature branch on your fork
  2. make a lookup that queries the process table and gets the pid for a process based on an input string
  3. make a pullrequest from the branch
  4. write a unit test for that lookup
  5. write an integration test for the lookup

Level 3: callbacks

  1. create a new feature branch
  2. create a new empty file lib/ansible/plugins/callbacks/filament.py
  3. create an ansible.cfg that enables and switches the default callback to "filament"
  4. run a simple playbook
  5. fix any errors until the ansible-playbook output is readable by referencing the other callbacks

Level 4: connection plugins

  1. create a new feature branch
  2. create an empty file lib/ansible/plugins/connections/filament.py
  3. create a playbook that uses the "lightbulb" connection plugin to run the shell module with "whoami"
  4. run the playbook
  5. fix whatever errors you see by referencing the other connection plugins

Level 5: strategy plugin

  1. subclass the linear strategy in a new strategy called "filament"
  2. put a 1 second sleep between each task

Appendix

Ansible Tools

setup

  1. vagrant init fedora/26-cloud-base
  2. vagrant up
  3. vagrant ssh
  4. sudo dnf -y install git ansible vim-enhanced
  5. sudo rpm -e --nodeps ansible
  6. sudo chmod 777 /opt
  7. git clone https://github.com/jctanner/ansible-tools /opt/ansible-tools
  8. export PATH=$PATH:/opt/ansible-tools
  9. sudo mkdir -p /var/cache/ansible
  10. sudo chown -R 777 /var/cache/ansible

first steps

  • ansible-list-versions
  • ansible-checkout-versions -v
  • ansible-workon AP 0000
  • cd ~/workspace/issues/AP-0000
  1. ansible-test-version devel test.sh
  2. ansible-test-versions --start=1.9 test.sh

resources