[LINUX] Introduction to Ansible Part 2'Basic Grammar'

Ansible basic grammar summary

Really basic grammar

Comment out

You can comment out just by adding #.

comment(YAML)


#If you add, it will be a comment.

list

The YAML list is the same as the JSON, Ruby array, and Python list. (Strictly speaking, it's called a sequence in YAML)

list(YAML)


-Taro
-17 years old
-male

This is shown in JSON as follows.

list(JSON)


[
  "Taro"
  "17 years old"
  "male"
]

dictionary

A YAML dictionary is equal to a JSON object, a Python dictionary. (Strictly called mapping)

dictionary(YAML)


name:Pablo Diego Jose Francisco de Paula Juan Nepomuseno Maria de los Remedios Crispin Crispiano de la Sandissima Trinidad Luis y Picasso
age:17 years old
gender:male

In JSON, it is written as follows.

list(JSON)


{
 "name": "Pablo Diego Jose Francisco de Paula Juan Nepomuseno Maria de los Remedios Crispin Crispiano de la Sandisima Trinidad Luis y Picasso",
 "age" : "17 years old",
 "gender": "male"
}

Line wrap

Line wrapping is possible with >. The YAML parser replaces line breaks with whitespace.

Wrap back(YAML)


name: >
Pablo Diego Jose Francisco de Paula Juan Nepomuseno>
Maria de los Remedios Crispin Crispiano de la>
Sandisima Trinidad Luis y Picasso

Variable assignment

Just enclose the variable name in {{}}.

Variable assignment(YAML)


name: Picasso

{{name}}

Playbook syntax

Basic idea of playbook

Below is a sample playbook.

Playbook.yml


---
- hosts: all
  tasks:
    - name: Hello World!
      debug:
        msg: "Hello World!"

If you write the above code in JSON, it will be as follows.

Playbook.json


[
  {
    "hosts": "all",
    "tasks": {
      "name": "Hello World!",
      "debug": {
        "msg": "Hello World!"
      }
    }
  }
]

In other words, you can see that the playbook is written in the form of a dictionary list.

In the playbook

  1. Target host
  2. You need a list of instructions (tasks) to execute on the host.

Below are some of the supported options in the playbook list.

name A comment indicating the contents of the list. Ansible outputs this at the start of execution.

hosts Specify the set of hosts to set.

become If this option is set to true, Ansible will sudo as the root user and execute all instructions (tasks). (Useful for managing Ubuntu servers.)

vars A list of variables and values.

tasks A list of instructions (tasks) to be executed on the host.

handler If Ansible detects a change in state, you can take further action by using a handler.

tasks The modules that can be specified under tasks are listed below. apt Install and remove packages using the package manager apt. copy Copy the file from the local machine to the host. file Set file, symbolic link, and directory attributes. service Start, stop, and restart services. template Generate a file from the template and copy the file to the host. (By the way, the difference between copy and template is whether variables can be used)
** Related materials ** Introduction to Ansible (1)'Hello World' Introduction to Ansible Part ③'Inventory'
** References ** First Ansible (written by Lorin Hochstein, translated by Ryuji Tamagawa, O'Reilly Japan Co., Ltd.)

Recommended Posts

Introduction to Ansible Part 2'Basic Grammar'
Introduction to Ansible Part ③'Inventory'
Introduction to Ansible Part ④'Variable'
Introduction to Ansible Part 1'Hello World !!'
Introduction to PyQt4 Part 1
Python Basic Grammar Memo (Part 1)
Introduction to Python Hands On Part 1
Web-WF Python Tornado Part 3 (Introduction to Openpyexcel)
Kaggle: Introduction to Manual Feature Engineering Part 1
Introduction to MQTT (Introduction)
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Supervisor
Introduction to Tkinter 1: Introduction
Introduction to Apache Beam with Cloud Dataflow (over 2.0.0 series) ~ Basic part ~ ParDo ~
Introduction to PyQt
Introduction to Scrapy (2)
[Linux] Introduction to Linux
Introduction to Scrapy (4)
Python3 basic grammar
Introduction to discord.py (2)
Introduction to discord.py
[Introduction to cx_Oracle] (Part 3) Basics of Table Reference
Apply Pandas Extraction Basic Grammar / Response to matplotlib
[Introduction to cx_Oracle] (Part 7) Handling of bind variables
[Introduction to Python] Basic usage of lambda expressions
[Go] Basic grammar ① Definition
Python basic grammar / algorithm
[Go] Basic grammar ② Statement
Introduction to Lightning pytorch
Introduction to Web Scraping
Introduction to Nonparametric Bayes
Introduction to EV3 / MicroPython
Introduction to Python language
Introduction to TensorFlow-Image Recognition
Introduction to OpenCV (python)-(2)
Python basic grammar (miscellaneous)
Python Basic Course (Introduction)
Python basic memorandum part 2
[Go] Basic grammar ③ Pointer
Python basic memo --Part 2
Python basic grammar note (4)
Python basic grammar note (3)
[Introduction to cx_Oracle] (Part 11) Basics of PL / SQL Execution
Introduction to Dependency Injection
Python basic grammar memo
Introduction to Private Chainer
[Introduction to Python] Basic usage of the library matplotlib
Python basic memo --Part 1
[Introduction to cx_Oracle] (Part 13) Connection using connection pool (client side)
Introduction to Python numpy pandas matplotlib (~ towards B3 ~ part2)
Introduction to machine learning
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
[Introduction to cx_Oracle] (Part 4) Fetch and scroll of result set
An introduction to Python that even monkeys can understand (Part 3)
Machine learning to learn with Nogizaka46 and Keyakizaka46 Part 1 Introduction
Introduction to Python scikit-learn, matplotlib, single-layer algorithm (~ towards B3 ~ part3)
An introduction to Python that even monkeys can understand (Part 1)
An introduction to Python that even monkeys can understand (Part 2)
Introduction to Socket API Learned in C Part 2 Client Edition
AOJ Introduction to Programming Topic # 1, Topic # 2, Topic # 3, Topic # 4