ansible是基于python开发的自动化运维工具,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
Inventory
Inventory文件管理的是Managed node ,Ansible 可同时操作属于一个组的多台主机,组和主机之间的关系通过 inventory 文件配置. 默认的文件路径为 /etc/ansible/hosts,但是也可以使用-i file_name 来指定inventory文件。
[agnet]
192.168.42.225
192.168.34.56
Dynamic Inventory
还可以使用-i参数来指定返回json格式的脚本,来确定inventory,详见:
https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#developing-inventory
#!/usr/bin/python
import json
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--list', dest='list',
help='out to stdout a json-encoded dictionary contains hosts', action="store_true")
parser.add_argument('--host', dest='host', help='out to stdout a json-encoded dictionary contains this hosts')
def get_hostinfo():
pass
def main():
args = parser.parse_args()
result = get_hostinfo()
out = {}
if (args.list):
out = result
print(json.dumps(out))
if __name__ == '__main__':
main()
Modules
ansible使用“模块”来完成大部分的任务,也被称为”Ad-Hoc Commands”( 这个单字是来自拉丁文常用短语中的一个短语,通常用来形容为一个特定的问题或任务而专门设定的解决方案),模块可以做安装软件,复制文件,使用模板等等。在命令行种以-m指定模块, -a用于将任何参数传递给定义的模块 -m,常用的模块,尽管shell可以执行大部分命令,但是可靠的模块可以保证执行命令是幂等的
$ ansible all -m command -a "echo Hello World"
server1 | SUCCESS | rc=0 >>
Hello World
常用模块:
- -m ping
- -m shell
- -m command
Playbooks
我们可以通过事先写好的剧本 (Playbooks) 来让各个 Managed Node 进行指定的动作 (Plays) 和任务 (Tasks)。
- Play:通常为某个特定的目的
- Task:是要实行 Play 这个目地所需做的每个步骤
- Module:Ansible 所提供的各种操作方法 ```yaml
name: “Hello World” # The play name hosts: agent tasks: # The task name
name: “Hello World” hosts: agent tasks:
- name: test
command: echo “Hello World”
notify:
- debug
handlers:
- name: test
command: echo “Hello World”
notify:
Roles
roles是对PlayBook中task的封装,以更加清晰明了的结构表示一个task,来一般roels的目录结构如下,,Ansible将自动搜索并读取叫做main.yml的yaml文件:
创建角色
# Create a roles directory
mkdir roles
cd roles
# Bootstrap a new role named "hello_world"
ansible-galaxy init hello_world
files
handlers
meta
templates
vars
tasks
运行角色
Facts
请注意,运行剧本时的第一行总是“收集事实”。
在运行任何任务之前,Ansible将收集有关其配置的系统的信息。这些被称为事实,并且包括广泛的系统信息,如CPU核心数量,可用的ipv4和ipv6网络,挂载的磁盘,Linux发行版等等。根据事实可以实现幂等
Vault
加密
YAML
yaml是一种易于读写的文件格式,一般以.yml结尾,
https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
开始与结尾
yaml可以 ---
开头,以 ...
结尾,但是 这并不是强制要求的
列表类型
All members of a list are lines beginning at the same indentation level starting with a "- "
(a dash and a space):
---
# A list of tasty fruits
fruits:
- Apple
- Orange
- Strawberry
- Mango
...
字典类型
A dictionary is represented in a simple key: value
form (the colon must be followed by a space):
# An employee record
martin:
name: Martin D'vloper
job: Developer
skill: Elite
各种形式的布尔值
create_key: yes
needs_agent: no
knows_oop: True
likes_emacs: TRUE
uses_cvs: false
多行
Values can span multiple lines using |
or >
. Spanning multiple lines using a “Literal Block Scalar” |
will include the newlines and any trailing spaces. Using a “Folded Block Scalar” >
will fold newlines to spaces; it’s used to make what would otherwise be a very long line easier to read and edit. In either case the indentation will be ignored. Examples are:
include_newlines: |
exactly as you see
will appear these three
lines of poetry
fold_newlines: >
this is really a
single line of text
despite appearances
ansible:
http://www.ansible.com.cn/docs/guides.html
https://serversforhackers.com/c/an-ansible2-tutorial
https://www.w3cschool.cn/automate_with_ansible/automate_with_ansible-qzva27p4.html
入门:https://blog.csdn.net/pushiqiang/article/details/78126063
http://www.361way.com/ansible-install/4371.html
lvs:https://blog.csdn.net/chao199512/article/category/7564403