What's new in Ansible 2.0
Allan Denot
∙ 2 year experience with Ansible
∙ 3 years experience with AWS
∙ Senior DevOps Engineer at Odecee
∙ Co-founder of spikenode.com
@denot allandenot.com
Allan Denot @denot allandenot.com
∙ Brazilian
∙ 5 years in Australia
New in Ansible 2.0
What’s New
Refactoring
● Improved variable management
● Better use of OOP
● Internal APIs
Many new modules
Language features
Release Date
September 8th:
Testing v2
git clone https://github.com/ansible/ansible.git
cd ansible
. v2/hacking/env-setup
Strategies
- hosts: all
gather_facts: no
strategy: free
tasks:
- pause: seconds={{ 10 |random}}
- debug: msg="msg_1"
- pause: seconds={{ 10 |random}}
- debug: msg="msg_2"
- pause: seconds={{ 10 |random}}
- debug: msg="msg_3"
Execution Strategies
Example playbook:
Execution Strategies
strategy: linear (default)
TASK [debug msg=msg_1]
**********************************************************
ok: [host3] => { "msg": "msg_1", "changed": false}
ok: [host4] => { "msg": "msg_1", "changed": false}
ok: [host2] => { "msg": "msg_1", "changed": false}
ok: [host1] => { "msg": "msg_1", "changed": false}
TASK [debug msg=msg_2]
**********************************************************
ok: [host4] => {"msg": "msg_2", "changed": false}
ok: [host1] => {"msg": "msg_2", "changed": false}
ok: [host2] => {"msg": "msg_2", "changed": false}
ok: [host3] => {"msg": "msg_2", "changed": false}
TASK [debug msg=msg_3]
**********************************************************
ok: [host1] => {"msg": "msg_3", "changed": false}
ok: [host2] => {"msg": "msg_3", "changed": false}
ok: [host3] => {"msg": "msg_3", "changed": false}
ok: [host4] => {"msg": "msg_3", "changed": false}
PLAY [<no name specified>]
******************************************************
ok: [host3] => {"msg": "msg_1", "changed": false}
ok: [host4] => {"msg": "msg_1", "changed": false}
ok: [host2] => {"msg": "msg_1", "changed": false}
ok: [host4] => {"msg": "msg_2", "changed": false}
ok: [host2] => {"msg": "msg_2", "changed": false}
ok: [host4] => {"msg": "msg_3", "changed": false}
ok: [host1] => {"msg": "msg_1", "changed": false}
ok: [host2] => {"msg": "msg_3", "changed": false}
ok: [host3] => {"msg": "msg_2", "changed": false}
ok: [host3] => {"msg": "msg_3", "changed": false}
ok: [host1] => {"msg": "msg_2", "changed": false}
ok: [host1] => {"msg": "msg_3", "changed": false}
strategy: free
Blocks
tasks:
- yum: name={{ item }} state=installed
with_items:
- httpd
- memcached
when: ansible_distribution == 'CentOS'
become: true
become_user: root
- template: src=templates/src.j2 dest=/etc/foo.conf
when: ansible_distribution == 'CentOS'
become: true
become_user: root
- service: name=bar state=started enabled=True
when: ansible_distribution == 'CentOS'
become: true
become_user: root
Blocks
Until 1.9:
REPETITIONREPETITIONREPETITIONREPETITIONREPETITION
tasks:
- block:
- yum: name={{ item }} state=installed
with_items:
- httpd
- memcached
- template: src=templates/src.j2 dest=/etc/foo.conf
- service: name=bar state=started enabled=True
when: ansible_distribution == 'CentOS'
become: true
become_user: root
Blocks
2.0:
tasks:
- block:
- name: Shell script to connect the app to a monitoring service.
script: monitoring-connect.sh
rescue:
- name: This will only run in case of an error in the block.
debug: msg="There was an error in the block."
always:
- name: This will always run, no matter what.
debug: msg="This always executes."
Blocks for Error Handling
2.0:
Modules
Modules
Notorious additions
package - generic OS package manager
- name: install the latest version of ntpdate
package: name=ntpdate state=latest
# This uses a variable as this changes per distro.
- name: remove the apache package
package : name={{apache}} state=absent
expect - executes a command and responds to prompt
- expect:
command: passwd username
responses:
(?i)password: "MySekretPa$$word
find - return a list of files based on criteria
# Recursively find /tmp files older than 4 weeks and equal or greater than 1 megabyte
- find: paths="/tmp" age="4w" size="1m" recurse=yes
# Recursively find /var/tmp files with last access time greater than 3600 seconds
- find: paths="/var/tmp" age="3600" age_stamp=atime recurse=yes
# find /var/log files equal or greater than 10 megabytes ending with .log or .log.gz
- find: paths="/var/tmp" patterns="*.log","*.log.gz" size="10m"
Modules
EC2
iam
ec2_win_password
ec2_ami_find
ec2_ami_copy
ec2_vpc_subnet
ec2_vpc_igw
ecs_task
ecs_cluster
ecs_taskdefinition
ec2_elb_facts
s3_logging
s3_bucket
s3_lifecycle
dynamodb_table
route53_zone
route53_health_check
IAM
EC2 windows
EC2 AMI
VPC
ECS
ELB
S3
DYNAMODB
ROUTE53
Compatibility
Playbooks
Playbooks should be 100% compatible,
no changes required.
Using 2.0 modules TODAY
1 Go to https://github.com/ansible/ansible-modules-core/
Using 2.0 modules TODAY
2 Browse to the module
3
Using 2.0 modules TODAY
4 Save the module file (or copy/paste)
Using 2.0 modules TODAY
5 Save under: library/cloud/amazon/iam2.py
6 Use it normally: tasks:
- name: Create two new IAM users with API keys
iam2:
iam_type: user
name: "{{ item }}"
state: present
password: "{{ temp_pass }}"
access_key_state: create
with_items:
- jcleese
- mpython
And finally...
There’s a good chance that Tower will be open sourced…
Questions?
Links
github.com/adenot
allandenot.com
spikenode.com

What's new in Ansible 2.0

  • 1.
    What's new inAnsible 2.0
  • 2.
    Allan Denot ∙ 2year experience with Ansible ∙ 3 years experience with AWS ∙ Senior DevOps Engineer at Odecee ∙ Co-founder of spikenode.com @denot allandenot.com
  • 3.
    Allan Denot @denotallandenot.com ∙ Brazilian ∙ 5 years in Australia
  • 4.
  • 5.
    What’s New Refactoring ● Improvedvariable management ● Better use of OOP ● Internal APIs Many new modules Language features
  • 6.
  • 7.
    Testing v2 git clonehttps://github.com/ansible/ansible.git cd ansible . v2/hacking/env-setup
  • 8.
  • 9.
    - hosts: all gather_facts:no strategy: free tasks: - pause: seconds={{ 10 |random}} - debug: msg="msg_1" - pause: seconds={{ 10 |random}} - debug: msg="msg_2" - pause: seconds={{ 10 |random}} - debug: msg="msg_3" Execution Strategies Example playbook:
  • 10.
    Execution Strategies strategy: linear(default) TASK [debug msg=msg_1] ********************************************************** ok: [host3] => { "msg": "msg_1", "changed": false} ok: [host4] => { "msg": "msg_1", "changed": false} ok: [host2] => { "msg": "msg_1", "changed": false} ok: [host1] => { "msg": "msg_1", "changed": false} TASK [debug msg=msg_2] ********************************************************** ok: [host4] => {"msg": "msg_2", "changed": false} ok: [host1] => {"msg": "msg_2", "changed": false} ok: [host2] => {"msg": "msg_2", "changed": false} ok: [host3] => {"msg": "msg_2", "changed": false} TASK [debug msg=msg_3] ********************************************************** ok: [host1] => {"msg": "msg_3", "changed": false} ok: [host2] => {"msg": "msg_3", "changed": false} ok: [host3] => {"msg": "msg_3", "changed": false} ok: [host4] => {"msg": "msg_3", "changed": false} PLAY [<no name specified>] ****************************************************** ok: [host3] => {"msg": "msg_1", "changed": false} ok: [host4] => {"msg": "msg_1", "changed": false} ok: [host2] => {"msg": "msg_1", "changed": false} ok: [host4] => {"msg": "msg_2", "changed": false} ok: [host2] => {"msg": "msg_2", "changed": false} ok: [host4] => {"msg": "msg_3", "changed": false} ok: [host1] => {"msg": "msg_1", "changed": false} ok: [host2] => {"msg": "msg_3", "changed": false} ok: [host3] => {"msg": "msg_2", "changed": false} ok: [host3] => {"msg": "msg_3", "changed": false} ok: [host1] => {"msg": "msg_2", "changed": false} ok: [host1] => {"msg": "msg_3", "changed": false} strategy: free
  • 11.
  • 12.
    tasks: - yum: name={{item }} state=installed with_items: - httpd - memcached when: ansible_distribution == 'CentOS' become: true become_user: root - template: src=templates/src.j2 dest=/etc/foo.conf when: ansible_distribution == 'CentOS' become: true become_user: root - service: name=bar state=started enabled=True when: ansible_distribution == 'CentOS' become: true become_user: root Blocks Until 1.9: REPETITIONREPETITIONREPETITIONREPETITIONREPETITION
  • 13.
    tasks: - block: - yum:name={{ item }} state=installed with_items: - httpd - memcached - template: src=templates/src.j2 dest=/etc/foo.conf - service: name=bar state=started enabled=True when: ansible_distribution == 'CentOS' become: true become_user: root Blocks 2.0:
  • 14.
    tasks: - block: - name:Shell script to connect the app to a monitoring service. script: monitoring-connect.sh rescue: - name: This will only run in case of an error in the block. debug: msg="There was an error in the block." always: - name: This will always run, no matter what. debug: msg="This always executes." Blocks for Error Handling 2.0:
  • 15.
  • 16.
    Modules Notorious additions package -generic OS package manager - name: install the latest version of ntpdate package: name=ntpdate state=latest # This uses a variable as this changes per distro. - name: remove the apache package package : name={{apache}} state=absent expect - executes a command and responds to prompt - expect: command: passwd username responses: (?i)password: "MySekretPa$$word find - return a list of files based on criteria # Recursively find /tmp files older than 4 weeks and equal or greater than 1 megabyte - find: paths="/tmp" age="4w" size="1m" recurse=yes # Recursively find /var/tmp files with last access time greater than 3600 seconds - find: paths="/var/tmp" age="3600" age_stamp=atime recurse=yes # find /var/log files equal or greater than 10 megabytes ending with .log or .log.gz - find: paths="/var/tmp" patterns="*.log","*.log.gz" size="10m"
  • 17.
  • 18.
  • 19.
    Playbooks Playbooks should be100% compatible, no changes required.
  • 20.
    Using 2.0 modulesTODAY 1 Go to https://github.com/ansible/ansible-modules-core/
  • 21.
    Using 2.0 modulesTODAY 2 Browse to the module 3
  • 22.
    Using 2.0 modulesTODAY 4 Save the module file (or copy/paste)
  • 23.
    Using 2.0 modulesTODAY 5 Save under: library/cloud/amazon/iam2.py 6 Use it normally: tasks: - name: Create two new IAM users with API keys iam2: iam_type: user name: "{{ item }}" state: present password: "{{ temp_pass }}" access_key_state: create with_items: - jcleese - mpython
  • 24.
  • 26.
    There’s a goodchance that Tower will be open sourced…
  • 27.