SlideShare a Scribd company logo
Best practices for ansible roles development
●
●
○
○
○
○
●
○
○
Best practices for ansible roles development
- file: path=/etc/foo.conf mode=0644
- file:
path=/etc/foo.conf
mode=0644
- file: "path=/etc/foo.conf mode=0644"
- file:
path: /etc/foo.conf
mode: 0644
Best practices for ansible roles development
- file:
path: "{{ my_path }}"
owner: "foo"
group: "bar"
mode: "0644"
- copy:
dest: "{{ my_path }}"
content: " Some very long line
which needs to be wrapped"
- copy:
dest: "{{ my_path }}"
content: "FirstnSecondn"
- file:
path: "{{ my_path }}"
owner: foo
group: bar
mode: 0644
- copy:
dest: "{{ my_path }}"
content: >2-
Some very long line
which needs to be wrapped
- copy:
dest: "{{ my_path }}"
content: |
First
Second
● - { } [ ] * & ? | > ! % ` # @ :
- file:
path: "{{ my_path }}"
mode: 0644
●
- debug:
msg: "Path: {{ my_path }}"
● yes false
- copy:
dest: "{{ my_path }}"
content: "yes"
● yamllint
ansible all -i localhost, --connection local -m debug -a 'msg={{xxx}}' -e '{xxx: @asd}'
Best practices for ansible roles development
- file:
path: "{{ my_path }}"
owner: foo
group: bar
mode: 0644
- hosts: all
vars:
data:
aaa: bbb
ccc:
- ddd:
- eee
# Half tabs (4 spaces)
- file:
path: "{{ my_path }}"
owner: foo
group: bar
mode: 0644
# Inconsistent indentation
- hosts: all
vars:
data:
aaa: bbb
ccc:
- ddd:
- eee
Best practices for ansible roles development
● .yml .yaml .jon .json
● .yaml meta
Best practices for ansible roles development
●
●
○
●
○
○
Best practices for ansible roles development
# roles/role1/defaults/main.yaml
var1: aaa
# roles/role2/defaults/main.yaml
var1: bbb
# group_vars/all
var1: ccc
# role1/defaults/main.yaml
role1_var1: aaa
# role2/defaults/main.yaml
role2_var1: bbb
# group_vars/all
role2_var1: ccc
Best practices for ansible roles development
# roles/role1/defaults/main.yaml
role1_var1: aaa
# roles/role1/tasks/main.yaml
- debug:
msg: >
var1={{ role1_var1 }},
var2={{ role1_var2 }}
# group_vars/all
role1_var2: bbb
# roles/role1/defaults/main.yaml
role1_var1: aaa
# Must be defined by the user
role1_var2: null
# roles/role1/tasks/main.yaml
- debug:
msg: >
var1={{ role1_var1 }},
var2={{ role1_var2 }}
# group_vars/all
role1_var2: bbb
Best practices for ansible roles development
# roles/role1/defaults/main.yaml
role1_var1: aaa
# roles/role1/vars/main.yaml
role1_var2: bbb
# roles/role1/tasks/main.yaml
- debug:
msg: >
var1={{ role1_var1 }},
var2={{ role1_var2 }}
# roles/role1/defaults/main.yaml
role1_var1: aaa
role1_var2: bbb
# roles/role1/tasks/main.yaml
- debug:
msg: >
var1={{ role1_var1 }},
var2={{ role1_var2 }}
● vars defaults
# roles/role1/meta/main.yaml
dependencies:
- role2
# roles/role1/vars/main.yaml
role1_var1: bbb
# roles/role2/defaults/main.yaml
role1_var1: aaa
●
○
○
Best practices for ansible roles development
- file:
path: /etc/foo.conf
mode: 0644
- name: Set foo.conf mode
file:
path: /etc/foo.conf
mode: 0644
Best practices for ansible roles development
- cron:
name: Run my command
job: /usr/bin/my_prog
minute: "*"
hour: "*"
state: present
- cron:
name: Run my command
job: /usr/bin/my_prog
- cron:
name: Run my command
minute: "{{ minute }}"
hour: "{{ hour }}"
job: /usr/bin/my_prog
Best practices for ansible roles development
- package:
name: mysql-server
- template:
src: my.cnf.j2
dest: /etc/my.cnf
- service:
name: mysql
enabled: yes
state: started
- package:
name: mysql-server
tags:
- mysql_pkg
- template:
src: my.cnf.j2
dest: /etc/my.cnf
tags:
- mysql_config
- service:
name: mysql
enabled: yes
state: started
tags:
- mysql_service
# roles/mysql/tasks/main.yaml
- package:
name: "{{ mysql_pkg }}"
notify: Restart MySQL service
tags: mysql_pkg
- template:
src: my.cnf.j2
dest: "{{ mysql_config_path }}"
notify: Restart MySQL service
tags: mysql_config
- service:
name: "{{ mysql_service }}"
enabled: yes
tags: mysql_service
- service:
name: "{{ mysql_service }}"
state: started
register: mysql_service_started
tags: mysql_service
# roles/mysql/handlers/main.yaml
- name: Restart MySQL service
service:
name: "{{ mysql_service }}"
state: restarted
when: >
mysql_service_started is not defined or
not mysql_service_started.changed
# roles/mysql/defaults/main.yaml
mysql_pkg: mysql-server
mysql_config_path: /etc/my.cnf
mysql_service: mysql
Best practices for ansible roles development
- lineinfile:
path: /etc/selinux/config
regexp: ^SELINUX=
line: SELINUX=enforcing
- template:
src: selinux_config.j2
dest: /etc/selinux/config
Best practices for ansible roles development
●
●
# Desired config file (myapp.cfg):
[section1]
option11=value11
option12=value12
# myapp_role/templates/myapp.cfg.j2:
{{ myapp_config | encode_ini }}
# myapp_role/defaults/main.yaml:
myapp_config:
section1:
option11: value11
option12: value12
# myapp_role/tasks/main.yaml:
- name: Create config file
template:
dest: /etc/myapp/ myapp.cfg
src: myapp.cfg.j2
# myapp_role/defaults/main.yaml:
myapp_section1_option11: value1
myapp_section1_option12: value2
myapp_section1__default:
option11: "{{ myapp_section1_option11 }}"
option12: "{{ myapp_section1_option12 }}"
myapp_section1__custom: []
myapp_section1: "{{
myapp_section1__default.update(myapp_section1__custom)}}{{
myapp_section1__default}}"
myapp_config__default:
section1: "{{ myapp_section1 }}"
myapp_config__custom: {}
myapp_config: "{{
myapp_config__default.update(myapp_config__custom) }}{{
myapp_config__default }}"
# Desired config file (/etc/selinux/config):
SELINUX=enforcing
SELINUXTYPE=targeted
# roles/sudo/templates/selinux_config.j2:
{{ ansible_managed | comment }}
{{ selinux_config | encode_ini(ucase_prop=true) }}
# roles/selinux/defaults/main.yaml:
selinux_config:
selinux: enforcing
selinuxtype: targeted
# roles/selinux/tasks/main.yaml:
- name: Create config file
template:
dest: /etc/selinux/config
src: selinux_config.j2
Best practices for ansible roles development
● README.md
●
○
○
○
○
○
■
○
○
Best practices for ansible roles development
●
●
●
●
●
●
●
●
●
git clone https://github.com/jtyr/vagrantfile_config.git /tmp/vagrantfile_config
mkdir -p /tmp/test/roles && cd /tmp/test
git clone https://github.com/jtyr/ansible-nginx.git roles/nginx
git clone https://github.com/jtyr/ansible-config_encoder_filters.git roles/config_encoder_filters
ln -s /tmp/vagrantfile_config/Vagrantfile ./
cat > vagrant.yaml <<END
---
defaults:
provision_individual: yes
vms:
testvm1:
ports:
HTTP:
host: 8080
guest: 80
END
cat > site.yaml <<END
---
- hosts: all
become: yes
roles:
- nginx
END
vagrant up
vagrant provision
ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory site.yaml
vagrant ssh
ssh -p 10000 -i .vagrant/machines/test/virtualbox/private_key -l vagrant localhost
vagrant destroy -f
Best practices for ansible roles development

More Related Content

PDF
PPTX
OVN DBs HA with scale test
PDF
Cycloudのストレージ紹介と歴史
PDF
【EX/QFX】JUNOS ハンズオントレーニング資料 EX/QFX シリーズ サービス ゲートウェイ コース
PDF
Kdump and the kernel crash dump analysis
PDF
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
PDF
containerdの概要と最近の機能
PPT
Linux Crash Dump Capture and Analysis
OVN DBs HA with scale test
Cycloudのストレージ紹介と歴史
【EX/QFX】JUNOS ハンズオントレーニング資料 EX/QFX シリーズ サービス ゲートウェイ コース
Kdump and the kernel crash dump analysis
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
containerdの概要と最近の機能
Linux Crash Dump Capture and Analysis

What's hot (20)

PDF
Kernel Recipes 2014 - The Linux graphics stack and Nouveau driver
PDF
SDCCオープンネットワークのご紹介【2021/01版】
PDF
Introduction to Vacuum Freezing and XID
PDF
Linux tuning to improve PostgreSQL performance
PDF
OpenStack超入門シリーズ いまさら聞けないNeutronの使い方
PDF
QEMU in Cross building
PDF
無料で仮想Junos環境を手元に作ろう
PPTX
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
ODP
4. linux file systems
PDF
Building Network Functions with eBPF & BCC
PPTX
Linux 802.11 subsystem and brcmsmac WLAN driver
PPT
U boot porting guide for SoC
PPTX
Windows Internals for Linux Kernel Developers
PDF
大規模DCのネットワークデザイン
PDF
Creating a DMZ - pfSense Hangout January 2016
PDF
EBPF and Linux Networking
PDF
How VXLAN works on Linux
PDF
LinuxCon 2015 Linux Kernel Networking Walkthrough
PDF
不揮発メモリ(NVDIMM)とLinuxの対応動向について
Kernel Recipes 2014 - The Linux graphics stack and Nouveau driver
SDCCオープンネットワークのご紹介【2021/01版】
Introduction to Vacuum Freezing and XID
Linux tuning to improve PostgreSQL performance
OpenStack超入門シリーズ いまさら聞けないNeutronの使い方
QEMU in Cross building
無料で仮想Junos環境を手元に作ろう
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
4. linux file systems
Building Network Functions with eBPF & BCC
Linux 802.11 subsystem and brcmsmac WLAN driver
U boot porting guide for SoC
Windows Internals for Linux Kernel Developers
大規模DCのネットワークデザイン
Creating a DMZ - pfSense Hangout January 2016
EBPF and Linux Networking
How VXLAN works on Linux
LinuxCon 2015 Linux Kernel Networking Walkthrough
不揮発メモリ(NVDIMM)とLinuxの対応動向について
Ad

Similar to Best practices for ansible roles development (20)

PDF
Templating in ansible
PDF
Linux 系統管理與安全:基本 Linux 系統知識
PDF
MariaDB, MySQL and Ansible: automating database infrastructures
PDF
AnsibleFest 2014 - Role Tips and Tricks
ODP
An introduction to Rex - FLOSS UK DevOps York 2015
PDF
Fun with containers: Use Ansible to build Docker images
PDF
More tips n tricks
PDF
A tour of Ansible
PPTX
Learning Puppet basic thing
PDF
Ansible is Our Wishbone
PDF
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
PDF
Ansible : what's ansible & use case by REX
PPTX
Configuration management
ODP
Configuration Management and Salt
PDF
Getting Started with Ansible - Jake.pdf
PDF
Ansible 101 - Presentation at Ansible STL Meetup
PDF
Augeas
PPT
Ansible presentation
PDF
Configuration Management - Finding the tool to fit your needs
PDF
Tomáš Čorej: Configuration management & CFEngine3
Templating in ansible
Linux 系統管理與安全:基本 Linux 系統知識
MariaDB, MySQL and Ansible: automating database infrastructures
AnsibleFest 2014 - Role Tips and Tricks
An introduction to Rex - FLOSS UK DevOps York 2015
Fun with containers: Use Ansible to build Docker images
More tips n tricks
A tour of Ansible
Learning Puppet basic thing
Ansible is Our Wishbone
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
Ansible : what's ansible & use case by REX
Configuration management
Configuration Management and Salt
Getting Started with Ansible - Jake.pdf
Ansible 101 - Presentation at Ansible STL Meetup
Augeas
Ansible presentation
Configuration Management - Finding the tool to fit your needs
Tomáš Čorej: Configuration management & CFEngine3
Ad

More from jtyr (12)

PDF
Ansible Inventory Plugins
PDF
Ansible Callback Plugins
PDF
Managing VMware VMs with Ansible
PDF
How does Ansible's agentless architecture work?
PDF
Variable precedence: Where should I put a variable?
PDF
Managing multiple environments with Ansible
PDF
Jinja2 filters
PDF
Make the prompt great again
PDF
Development of Ansible modules
PDF
Overcoming problems of the standard Ansible inventory file
PDF
Automation and Ansible
PDF
LEGO IR Controller
Ansible Inventory Plugins
Ansible Callback Plugins
Managing VMware VMs with Ansible
How does Ansible's agentless architecture work?
Variable precedence: Where should I put a variable?
Managing multiple environments with Ansible
Jinja2 filters
Make the prompt great again
Development of Ansible modules
Overcoming problems of the standard Ansible inventory file
Automation and Ansible
LEGO IR Controller

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Electronic commerce courselecture one. Pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
PPTX
Programs and apps: productivity, graphics, security and other tools
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Electronic commerce courselecture one. Pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MIND Revenue Release Quarter 2 2025 Press Release
“AI and Expert System Decision Support & Business Intelligence Systems”
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
20250228 LYD VKU AI Blended-Learning.pptx
The AUB Centre for AI in Media Proposal.docx
Big Data Technologies - Introduction.pptx
Network Security Unit 5.pdf for BCA BBA.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
Programs and apps: productivity, graphics, security and other tools

Best practices for ansible roles development

  • 4. - file: path=/etc/foo.conf mode=0644 - file: path=/etc/foo.conf mode=0644 - file: "path=/etc/foo.conf mode=0644" - file: path: /etc/foo.conf mode: 0644
  • 6. - file: path: "{{ my_path }}" owner: "foo" group: "bar" mode: "0644" - copy: dest: "{{ my_path }}" content: " Some very long line which needs to be wrapped" - copy: dest: "{{ my_path }}" content: "FirstnSecondn" - file: path: "{{ my_path }}" owner: foo group: bar mode: 0644 - copy: dest: "{{ my_path }}" content: >2- Some very long line which needs to be wrapped - copy: dest: "{{ my_path }}" content: | First Second
  • 7. ● - { } [ ] * & ? | > ! % ` # @ : - file: path: "{{ my_path }}" mode: 0644 ● - debug: msg: "Path: {{ my_path }}" ● yes false - copy: dest: "{{ my_path }}" content: "yes" ● yamllint ansible all -i localhost, --connection local -m debug -a 'msg={{xxx}}' -e '{xxx: @asd}'
  • 9. - file: path: "{{ my_path }}" owner: foo group: bar mode: 0644 - hosts: all vars: data: aaa: bbb ccc: - ddd: - eee # Half tabs (4 spaces) - file: path: "{{ my_path }}" owner: foo group: bar mode: 0644 # Inconsistent indentation - hosts: all vars: data: aaa: bbb ccc: - ddd: - eee
  • 11. ● .yml .yaml .jon .json ● .yaml meta
  • 15. # roles/role1/defaults/main.yaml var1: aaa # roles/role2/defaults/main.yaml var1: bbb # group_vars/all var1: ccc # role1/defaults/main.yaml role1_var1: aaa # role2/defaults/main.yaml role2_var1: bbb # group_vars/all role2_var1: ccc
  • 17. # roles/role1/defaults/main.yaml role1_var1: aaa # roles/role1/tasks/main.yaml - debug: msg: > var1={{ role1_var1 }}, var2={{ role1_var2 }} # group_vars/all role1_var2: bbb # roles/role1/defaults/main.yaml role1_var1: aaa # Must be defined by the user role1_var2: null # roles/role1/tasks/main.yaml - debug: msg: > var1={{ role1_var1 }}, var2={{ role1_var2 }} # group_vars/all role1_var2: bbb
  • 19. # roles/role1/defaults/main.yaml role1_var1: aaa # roles/role1/vars/main.yaml role1_var2: bbb # roles/role1/tasks/main.yaml - debug: msg: > var1={{ role1_var1 }}, var2={{ role1_var2 }} # roles/role1/defaults/main.yaml role1_var1: aaa role1_var2: bbb # roles/role1/tasks/main.yaml - debug: msg: > var1={{ role1_var1 }}, var2={{ role1_var2 }}
  • 20. ● vars defaults # roles/role1/meta/main.yaml dependencies: - role2 # roles/role1/vars/main.yaml role1_var1: bbb # roles/role2/defaults/main.yaml role1_var1: aaa ● ○ ○
  • 22. - file: path: /etc/foo.conf mode: 0644 - name: Set foo.conf mode file: path: /etc/foo.conf mode: 0644
  • 24. - cron: name: Run my command job: /usr/bin/my_prog minute: "*" hour: "*" state: present - cron: name: Run my command job: /usr/bin/my_prog - cron: name: Run my command minute: "{{ minute }}" hour: "{{ hour }}" job: /usr/bin/my_prog
  • 26. - package: name: mysql-server - template: src: my.cnf.j2 dest: /etc/my.cnf - service: name: mysql enabled: yes state: started - package: name: mysql-server tags: - mysql_pkg - template: src: my.cnf.j2 dest: /etc/my.cnf tags: - mysql_config - service: name: mysql enabled: yes state: started tags: - mysql_service
  • 27. # roles/mysql/tasks/main.yaml - package: name: "{{ mysql_pkg }}" notify: Restart MySQL service tags: mysql_pkg - template: src: my.cnf.j2 dest: "{{ mysql_config_path }}" notify: Restart MySQL service tags: mysql_config - service: name: "{{ mysql_service }}" enabled: yes tags: mysql_service - service: name: "{{ mysql_service }}" state: started register: mysql_service_started tags: mysql_service # roles/mysql/handlers/main.yaml - name: Restart MySQL service service: name: "{{ mysql_service }}" state: restarted when: > mysql_service_started is not defined or not mysql_service_started.changed # roles/mysql/defaults/main.yaml mysql_pkg: mysql-server mysql_config_path: /etc/my.cnf mysql_service: mysql
  • 29. - lineinfile: path: /etc/selinux/config regexp: ^SELINUX= line: SELINUX=enforcing - template: src: selinux_config.j2 dest: /etc/selinux/config
  • 32. # Desired config file (myapp.cfg): [section1] option11=value11 option12=value12 # myapp_role/templates/myapp.cfg.j2: {{ myapp_config | encode_ini }} # myapp_role/defaults/main.yaml: myapp_config: section1: option11: value11 option12: value12 # myapp_role/tasks/main.yaml: - name: Create config file template: dest: /etc/myapp/ myapp.cfg src: myapp.cfg.j2
  • 33. # myapp_role/defaults/main.yaml: myapp_section1_option11: value1 myapp_section1_option12: value2 myapp_section1__default: option11: "{{ myapp_section1_option11 }}" option12: "{{ myapp_section1_option12 }}" myapp_section1__custom: [] myapp_section1: "{{ myapp_section1__default.update(myapp_section1__custom)}}{{ myapp_section1__default}}" myapp_config__default: section1: "{{ myapp_section1 }}" myapp_config__custom: {} myapp_config: "{{ myapp_config__default.update(myapp_config__custom) }}{{ myapp_config__default }}"
  • 34. # Desired config file (/etc/selinux/config): SELINUX=enforcing SELINUXTYPE=targeted # roles/sudo/templates/selinux_config.j2: {{ ansible_managed | comment }} {{ selinux_config | encode_ini(ucase_prop=true) }} # roles/selinux/defaults/main.yaml: selinux_config: selinux: enforcing selinuxtype: targeted # roles/selinux/tasks/main.yaml: - name: Create config file template: dest: /etc/selinux/config src: selinux_config.j2
  • 39. git clone https://github.com/jtyr/vagrantfile_config.git /tmp/vagrantfile_config mkdir -p /tmp/test/roles && cd /tmp/test git clone https://github.com/jtyr/ansible-nginx.git roles/nginx git clone https://github.com/jtyr/ansible-config_encoder_filters.git roles/config_encoder_filters ln -s /tmp/vagrantfile_config/Vagrantfile ./ cat > vagrant.yaml <<END --- defaults: provision_individual: yes vms: testvm1: ports: HTTP: host: 8080 guest: 80 END cat > site.yaml <<END --- - hosts: all become: yes roles: - nginx END vagrant up vagrant provision ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory site.yaml vagrant ssh ssh -p 10000 -i .vagrant/machines/test/virtualbox/private_key -l vagrant localhost vagrant destroy -f