Tuesday, September 10, 2019

Ansible and YAML Guide

Ansible
-------
https://docs.ansible.com/ansible/latest/user_guide/index.html

 Adhoc commands
--------------
#ansible all -i hosts -m ping

 Ansible Inventory
 ----------------
/etc/ansible/hosts - Default host inventory
 #ansible -i --- to invoke inventory Doc url for inventory, https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html  

Yaml_hosts - yaml format hosts inventory
       
---
all: 
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        alpha.example.org:
  dbservers: 
    hosts:
      192.168.1.110:


Ansible Playbook
----------------
#ansible-playbook -k -K -i  <inventory>
-k for ssh password
-K for sudo password

YAML
--------

'yaml_format.yml'
----------------------
       
# Comments denoted with '#' character
#here is a list programming languages
lanuages:
  - c++
  - java
  - python
# vendor record
vendor1:
  name: for boo
  product: widget
  color: black
# vendor records # Dictionary
- vendor1:
    name: foo boo
    product: widget
 color:
   - black
   - green
   - red
- vendor2:
    name: dalton vendor
 product: widget
 color:
   - blue
   - orange
   - red
  
# dictonary in abbreviated format
vendor1: {name: joe vendor, product: widget, color:black}
#list in abbreviated format
languages: ['c++', 'java', 'python']
#boolean values
use_agent: yes
use_agent: no
use_agent: True
use_agent: TRUE
use_agent: false
# spanning multiple lines
scalar: |
    these lines will
 appear exactly as
 they are written here
folded: >
    really just a single
 line of text to make
 easier to read and exit
...


'yaml_notes.yml'
---------------------
       
---
# unquoted scalars - a color followed by a space or new line is not allowed
drive: c:
# no space or new line here so this is allowed
path: c:\temp
# quote (or double quote) hash values to preserve the colon
drive: 'c:'
# with dobule quotes you can escape characters
value: "a \n new line and \t tab for example"
# Ansible uses "{{ var }}" to denote variables so if you have a value after a
# color that starts with a "{", YAML will interpret it as a dictonary, thus
# you must quote it properly
value: "{{ var }}"
# if a value begins with a quote, then the entire value must be quoted
value: "{{ variable }}/additional/string/literal"
value: "{{ variable }}\\backslashes\\are\\special\\chars"
# there are some reserved characters that can't be used as the first character
# in an unquoted scalar
# [] {} > | * & ! % # ` @ ,
# for a literal boolean value (like yes or true) as a string use quotes
not_boolean: "yes"
string: "True"
# YAML converts some strings into floating-point numbers so if you need to
# specify something like a version number for example, use quotes if it apperars
# to be like a floating-point value
ver: "2.0"
...


Ansible Variables
---------------------
https://docs.ansible.com/ansible/latest/user_guide/index.html

'Variables.txt'
----------------
       
# Variable naming converntion says that the names should consists of letters,
# numbers, and underscores, and they should always begin with a letter.
listen_port
listen_port
# Not valid
listen-port
listen port
listen.port
123
# YAML supports dictonaries which map key/value pairs.
record:
  field1: one
  field2: two
# A specific field can then be referenced from the dictonary using bracket or
# dot notation
record['field1']
record.field1
# Variables may be defined directly in playbooks inline.
- hosts: webservers
  var:
   http_port: 80
# Variables can be defined via include files and roles. Using roles in preferred
# since it provides a convenient organizational system.



'Host_group_variables.txt'
-------------------------------
       
# You can assign variables to hosts in inventory
[west]
host1 http_port=8080
host2 http_port=3000
# You can assign variables to a group of hosts.
[west]
host1
host2
[west:vars]
proxy=proxy.west.local
# ...and the same in yaml syntax
west:
  hosts:
    host1:
 host2:
  vars:
    proxy.west.local
# Variables are merged or flattened by default to the specific host before abbreviated
# play in run a ansible overwrites variables by default, including those that are
# defined for groups/hosts. Order of procedence from lowest to hightest is:
all group (since it is parent to all other groups)
parent group
child group
host
# Groups at that same level are merged alphabetically, with the last group merged
# overwriting previous groups. Thus, an x_group will be merged with a y_group and y_group
# variables that match will overwrite the once in the x_group. Here, the result of the merge is that myvar == y.
x_group:
    myvar: x
y_group:
    myvar: y
# Since Ansible version 2.4, you can use the ansible_group_priority groups
# variable to change the merge order for groups of the same level. The larger
# the prority number, the later it is merged, thereby providing it with a higher
# prority. The default for the variable is 1 if not explicitly set. Here, the
# result of the merge is that myvar ==x.
x_group:
    myvar: x
 ansible_group_priority: 5
y_group:
    myvar: y
 
'using_variables.txt'
------------------------
       
# For example, this provides a basic from of variable substitution and is also
# Variable can be referenced in playbooks using the Jinja2 templating system.
# valid as is, directly within playbooks:
{{ my_var }}

Create rpm and deb using fpm

Create rpm and deb using fpm  fpm -s dir -t rpm -n unbound-exporter -v 1.0 --prefix /usr/bin unbound_exporter   fpm -s dir -t rpm -n unbound...