Ansible 2.0
Dennis Rowe
E-Mail: dennis.rowe@apcera.com
Twitter: @shr3kst3r
What is it?
๏ Another configuration management tool
๏ No client to install (SSH)
๏ YAML as the configuration language
๏ Implemented in python and plugins written in Python
๏ Easy to get started
๏ Ansible 2.0 released January 12, 2016
Task Blocks
๏ Based off of the try/except/finally
๏ Example from Ansible
tasks:
- block:
- debug: msg='i execute normally'
- command: /bin/false
- debug: msg='i never execute, cause ERROR!'
rescue:
- debug: msg='I caught an error'
- command: /bin/false
- debug: msg='I also never execute :-('
always:
- debug: msg="this always executes"
Output
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "i execute normally"
}
TASK [command] *****************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": …
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "I caught an error"
}
TASK [command] *****************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": …
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "this always executes"
}
Playbook Parsing / Error Reporting
๏ Basically rewritten
๏ Clearer identification of errors
๏ Suggested fixes for simple YAML syntax errors
Example Playbook 1
- hosts: all
connection: local
tasks:
- names: install apache2
action: apt pkg=apache2 state=latest
Example Error 1 - Ansible 1.x (“names” should be “name”)
ERROR: names is not a legal parameter in an Ansible task or handler
Example Error 1 - Ansible 2.x (“names” should be “name”)
ERROR! 'names' is not a valid attribute for a Task
The error appears to have been in '/home/vagrant/test.yml': line 4, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- debug: msg="System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}"
- names: install apache2
^ here
Example Playbook 2
- hosts: all
connection: local
tasks:
- debug msg="System {{ inventory_hostname }} …”
Example Error 2 - Ansible 1.x (“debug” should be “debug:”)
ERROR: expecting dict; got: debug msg="System
{{inventory_hostname}} has uuid {{ansible_product_uuid}}"
Example Error 2 - Ansible 2.x (“debug” should be “debug:”)
ERROR! A malformed block was encountered.
The error appears to have been in '/home/vagrant/test.yml': line 3, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- debug msg="System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
Dynamic Includes
๏ In Ansible 1.x includes were inlined during pre-processing
๏ Dynamic includes allow for the re-use of code via loops
loop.yml
- hosts: all
connection: local
tasks:
- include: debugz.yml
with_items:
- 1
- 2
- 3
debugz.yml
---
- debug: msg="Item is {{item}}"
Example Output
TASK [include] *****************************************************************
included: /home/vagrant/debugz.yml for localhost
included: /home/vagrant/debugz.yml for localhost
included: /home/vagrant/debugz.yml for localhost
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "Item is 1"
}
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "Item is 2"
}
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "Item is 3"
}
Execution Strategy
๏ Strategies
‣ linear - A single task is run on all hosts at once
‣ serial - A single task is run on a batch of hosts until completion, then the next batch
‣ Basically batching of linear
‣ free - Just get it done (go as fast as you can)
‣ Not limited to the long poll before moving on
๏ Strategies are now a type of plugin (“free” is a function that is 130 lines long…)
Breaking Changes
๏ Dynamic Includes
‣ Tags - Should be specified on the include task
‣ Handlers - Won’t be seen when in includes
๏ Missing Tags
‣ Does not raise an error if the specified tag is missing
๏ Backslash Escaping
‣  to 
๏ Porting Guide
‣ https://github.com/ansible/ansible/blob/devel/docsite/rst/porting_guide_2.0.rst
The End
Dennis Rowe
E-Mail: dennis.rowe@apcera.com
Twitter: @shr3kst3r

Ansible 2.0

  • 1.
  • 2.
    What is it? ๏Another configuration management tool ๏ No client to install (SSH) ๏ YAML as the configuration language ๏ Implemented in python and plugins written in Python ๏ Easy to get started ๏ Ansible 2.0 released January 12, 2016
  • 3.
    Task Blocks ๏ Basedoff of the try/except/finally ๏ Example from Ansible tasks: - block: - debug: msg='i execute normally' - command: /bin/false - debug: msg='i never execute, cause ERROR!' rescue: - debug: msg='I caught an error' - command: /bin/false - debug: msg='I also never execute :-(' always: - debug: msg="this always executes"
  • 4.
    Output TASK [debug] ******************************************************************* ok:[localhost] => { "msg": "i execute normally" } TASK [command] ***************************************************************** fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": … TASK [debug] ******************************************************************* ok: [localhost] => { "msg": "I caught an error" } TASK [command] ***************************************************************** fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": … TASK [debug] ******************************************************************* ok: [localhost] => { "msg": "this always executes" }
  • 5.
    Playbook Parsing /Error Reporting ๏ Basically rewritten ๏ Clearer identification of errors ๏ Suggested fixes for simple YAML syntax errors
  • 6.
    Example Playbook 1 -hosts: all connection: local tasks: - names: install apache2 action: apt pkg=apache2 state=latest
  • 7.
    Example Error 1- Ansible 1.x (“names” should be “name”) ERROR: names is not a legal parameter in an Ansible task or handler
  • 8.
    Example Error 1- Ansible 2.x (“names” should be “name”) ERROR! 'names' is not a valid attribute for a Task The error appears to have been in '/home/vagrant/test.yml': line 4, column 5, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - debug: msg="System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}" - names: install apache2 ^ here
  • 9.
    Example Playbook 2 -hosts: all connection: local tasks: - debug msg="System {{ inventory_hostname }} …”
  • 10.
    Example Error 2- Ansible 1.x (“debug” should be “debug:”) ERROR: expecting dict; got: debug msg="System {{inventory_hostname}} has uuid {{ansible_product_uuid}}"
  • 11.
    Example Error 2- Ansible 2.x (“debug” should be “debug:”) ERROR! A malformed block was encountered. The error appears to have been in '/home/vagrant/test.yml': line 3, column 5, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: tasks: - debug msg="System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}" ^ here We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance: with_items: - {{ foo }} Should be written as: with_items: - "{{ foo }}"
  • 12.
    Dynamic Includes ๏ InAnsible 1.x includes were inlined during pre-processing ๏ Dynamic includes allow for the re-use of code via loops loop.yml - hosts: all connection: local tasks: - include: debugz.yml with_items: - 1 - 2 - 3 debugz.yml --- - debug: msg="Item is {{item}}"
  • 13.
    Example Output TASK [include]***************************************************************** included: /home/vagrant/debugz.yml for localhost included: /home/vagrant/debugz.yml for localhost included: /home/vagrant/debugz.yml for localhost TASK [debug] ******************************************************************* ok: [localhost] => { "msg": "Item is 1" } TASK [debug] ******************************************************************* ok: [localhost] => { "msg": "Item is 2" } TASK [debug] ******************************************************************* ok: [localhost] => { "msg": "Item is 3" }
  • 14.
    Execution Strategy ๏ Strategies ‣linear - A single task is run on all hosts at once ‣ serial - A single task is run on a batch of hosts until completion, then the next batch ‣ Basically batching of linear ‣ free - Just get it done (go as fast as you can) ‣ Not limited to the long poll before moving on ๏ Strategies are now a type of plugin (“free” is a function that is 130 lines long…)
  • 15.
    Breaking Changes ๏ DynamicIncludes ‣ Tags - Should be specified on the include task ‣ Handlers - Won’t be seen when in includes ๏ Missing Tags ‣ Does not raise an error if the specified tag is missing ๏ Backslash Escaping ‣ to ๏ Porting Guide ‣ https://github.com/ansible/ansible/blob/devel/docsite/rst/porting_guide_2.0.rst
  • 16.