SlideShare a Scribd company logo
1 of 40
Download to read offline
●
●
○
○
○
○
●
○
○
- 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
- 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}'
- 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
● .yml .yaml .jon .json
● .yaml meta
●
●
○
●
○
○
# 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
# 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
# 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
●
○
○
- file:
path: /etc/foo.conf
mode: 0644
- name: Set foo.conf mode
file:
path: /etc/foo.conf
mode: 0644
- 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
- 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
- lineinfile:
path: /etc/selinux/config
regexp: ^SELINUX=
line: SELINUX=enforcing
- template:
src: selinux_config.j2
dest: /etc/selinux/config
●
●
# 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
● README.md
●
○
○
○
○
○
■
○
○
●
●
●
●
●
●
●
●
●
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

What's hot

Composer 從入門到實戰
Composer 從入門到實戰Composer 從入門到實戰
Composer 從入門到實戰
Shengyou Fan
 
Nginx internals
Nginx internalsNginx internals
Nginx internals
liqiang xu
 

What's hot (20)

Play with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniquePlay with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit Technique
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90mins
 
Page cache in Linux kernel
Page cache in Linux kernelPage cache in Linux kernel
Page cache in Linux kernel
 
Solaris Linux Performance, Tools and Tuning
Solaris Linux Performance, Tools and TuningSolaris Linux Performance, Tools and Tuning
Solaris Linux Performance, Tools and Tuning
 
CRX2Oak - all the secrets of repository migration
CRX2Oak - all the secrets of repository migrationCRX2Oak - all the secrets of repository migration
CRX2Oak - all the secrets of repository migration
 
Composer 從入門到實戰
Composer 從入門到實戰Composer 從入門到實戰
Composer 從入門到實戰
 
Materialized Views and Secondary Indexes in Scylla: They Are finally here!
Materialized Views and Secondary Indexes in Scylla: They Are finally here!Materialized Views and Secondary Indexes in Scylla: They Are finally here!
Materialized Views and Secondary Indexes in Scylla: They Are finally here!
 
Nginx internals
Nginx internalsNginx internals
Nginx internals
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
 
Developing MIPS Exploits to Hack Routers
Developing MIPS Exploits to Hack RoutersDeveloping MIPS Exploits to Hack Routers
Developing MIPS Exploits to Hack Routers
 
Memory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux KernelMemory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux Kernel
 
Apache Server Tutorial
Apache Server TutorialApache Server Tutorial
Apache Server Tutorial
 
Scylla Summit 2022: IO Scheduling & NVMe Disk Modelling
 Scylla Summit 2022: IO Scheduling & NVMe Disk Modelling Scylla Summit 2022: IO Scheduling & NVMe Disk Modelling
Scylla Summit 2022: IO Scheduling & NVMe Disk Modelling
 
Introduction to SLURM
Introduction to SLURMIntroduction to SLURM
Introduction to SLURM
 
Spark 2.x Troubleshooting Guide
Spark 2.x Troubleshooting GuideSpark 2.x Troubleshooting Guide
Spark 2.x Troubleshooting Guide
 
Why is My Stream Processing Job Slow? with Xavier Leaute
Why is My Stream Processing Job Slow? with Xavier LeauteWhy is My Stream Processing Job Slow? with Xavier Leaute
Why is My Stream Processing Job Slow? with Xavier Leaute
 
Developing Microservices with Apache Camel
Developing Microservices with Apache CamelDeveloping Microservices with Apache Camel
Developing Microservices with Apache Camel
 
Cosco: An Efficient Facebook-Scale Shuffle Service
Cosco: An Efficient Facebook-Scale Shuffle ServiceCosco: An Efficient Facebook-Scale Shuffle Service
Cosco: An Efficient Facebook-Scale Shuffle Service
 
Backup and-recovery2
Backup and-recovery2Backup and-recovery2
Backup and-recovery2
 
MariaDB MaxScale
MariaDB MaxScaleMariaDB MaxScale
MariaDB MaxScale
 

Similar to Best practices for ansible roles development

Virtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + PuppetVirtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + Puppet
Omar Reygaert
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
Positive Hack Days
 
Webinar - Managing Files with Puppet
Webinar - Managing Files with PuppetWebinar - Managing Files with Puppet
Webinar - Managing Files with Puppet
OlinData
 

Similar to Best practices for ansible roles development (20)

Virtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + PuppetVirtualization and automation of library software/machines + Puppet
Virtualization and automation of library software/machines + Puppet
 
Composer
ComposerComposer
Composer
 
Puppet: Eclipsecon ALM 2013
Puppet: Eclipsecon ALM 2013Puppet: Eclipsecon ALM 2013
Puppet: Eclipsecon ALM 2013
 
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
2017-03-11 02 Денис Нелюбин. Docker & Ansible - лучшие друзья DevOps
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
Drupal 8 configuration management
Drupal 8 configuration managementDrupal 8 configuration management
Drupal 8 configuration management
 
Django로 만든 웹 애플리케이션 도커라이징하기 + 도커 컴포즈로 개발 환경 구축하기
Django로 만든 웹 애플리케이션 도커라이징하기 + 도커 컴포즈로 개발 환경 구축하기Django로 만든 웹 애플리케이션 도커라이징하기 + 도커 컴포즈로 개발 환경 구축하기
Django로 만든 웹 애플리케이션 도커라이징하기 + 도커 컴포즈로 개발 환경 구축하기
 
WorkFlow: An Inquiry Into Productivity by Timothy Bolton
WorkFlow:  An Inquiry Into Productivity by Timothy BoltonWorkFlow:  An Inquiry Into Productivity by Timothy Bolton
WorkFlow: An Inquiry Into Productivity by Timothy Bolton
 
PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...
PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...
PuppetConf 2016: The Challenges with Container Configuration – David Lutterko...
 
Challenges of container configuration
Challenges of container configurationChallenges of container configuration
Challenges of container configuration
 
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
 
Fast and cost effective geospatial analysis pipeline with AWS lambda
Fast and cost effective geospatial analysis pipeline with AWS lambdaFast and cost effective geospatial analysis pipeline with AWS lambda
Fast and cost effective geospatial analysis pipeline with AWS lambda
 
Puppet Camp LA 2015: Basic Puppet Module Design (Beginner)
Puppet Camp LA  2015: Basic Puppet Module Design (Beginner)Puppet Camp LA  2015: Basic Puppet Module Design (Beginner)
Puppet Camp LA 2015: Basic Puppet Module Design (Beginner)
 
Puppetcamp module design talk
Puppetcamp module design talkPuppetcamp module design talk
Puppetcamp module design talk
 
PHP selber bauen
PHP selber bauenPHP selber bauen
PHP selber bauen
 
Centos config
Centos configCentos config
Centos config
 
Webinar - Managing Files with Puppet
Webinar - Managing Files with PuppetWebinar - Managing Files with Puppet
Webinar - Managing Files with Puppet
 
Hadoop installation on windows
Hadoop installation on windows Hadoop installation on windows
Hadoop installation on windows
 
Linux command line cheatsheet
Linux command line cheatsheetLinux command line cheatsheet
Linux command line cheatsheet
 
Big data using Hadoop, Hive, Sqoop with Installation
Big data using Hadoop, Hive, Sqoop with InstallationBig data using Hadoop, Hive, Sqoop with Installation
Big data using Hadoop, Hive, Sqoop with Installation
 

More from jtyr

More from jtyr (12)

Ansible Inventory Plugins
Ansible Inventory PluginsAnsible Inventory Plugins
Ansible Inventory Plugins
 
Ansible Callback Plugins
Ansible Callback PluginsAnsible Callback Plugins
Ansible Callback Plugins
 
Managing VMware VMs with Ansible
Managing VMware VMs with AnsibleManaging VMware VMs with Ansible
Managing VMware VMs with Ansible
 
How does Ansible's agentless architecture work?
How does Ansible's agentless architecture work?How does Ansible's agentless architecture work?
How does Ansible's agentless architecture work?
 
Managing multiple environments with Ansible
Managing multiple environments with AnsibleManaging multiple environments with Ansible
Managing multiple environments with Ansible
 
Jinja2 filters
Jinja2 filtersJinja2 filters
Jinja2 filters
 
Templating in ansible
Templating in ansibleTemplating in ansible
Templating in ansible
 
Make the prompt great again
Make the prompt great againMake the prompt great again
Make the prompt great again
 
Development of Ansible modules
Development of Ansible modulesDevelopment of Ansible modules
Development of Ansible modules
 
Overcoming problems of the standard Ansible inventory file
Overcoming problems of the standard Ansible inventory fileOvercoming problems of the standard Ansible inventory file
Overcoming problems of the standard Ansible inventory file
 
Automation and Ansible
Automation and AnsibleAutomation and Ansible
Automation and Ansible
 
LEGO IR Controller
LEGO IR ControllerLEGO IR Controller
LEGO IR Controller
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

Best practices for ansible roles development

  • 1.
  • 3.
  • 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
  • 5.
  • 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}'
  • 8.
  • 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
  • 10.
  • 11. ● .yml .yaml .jon .json ● .yaml meta
  • 12.
  • 14.
  • 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
  • 16.
  • 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
  • 18.
  • 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 ● ○ ○
  • 21.
  • 22. - file: path: /etc/foo.conf mode: 0644 - name: Set foo.conf mode file: path: /etc/foo.conf mode: 0644
  • 23.
  • 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
  • 25.
  • 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
  • 28.
  • 29. - lineinfile: path: /etc/selinux/config regexp: ^SELINUX= line: SELINUX=enforcing - template: src: selinux_config.j2 dest: /etc/selinux/config
  • 30.
  • 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
  • 35.
  • 37.
  • 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