ansible-playbook了解(一)

发布时间 2023-09-13 15:27:48作者: 家乐福的搬砖日常

1.ansible playbook 了解

playbook是ansible用于配置,部署,和管理被节点的剧本
通过playbook的详细描述,执行其中的一些列tasks,可以让远端的主机达到预期的状态。playbook就像ansible控制器给被控节点列出的一系列to-do-list,而且被控节点必须要完成
playbook顾名思义,即剧本,现实生活中演员按照剧本表演,在ansible中,这次由被控计算机表演,进行安装,部署应用,提供对外的服务等,以及组织计算机处理各种各样的事情。

2.ansible playbook使用场景

执行一些简单的任务,使用ad-hoc命令可以方便的解决问题,但是有时一个设施过于复杂,需要大量的操作的时候,执行的ad-hoc命令是不合适的,这时候最好使用playbook。
就像执行shell命令与写shell脚本一样,也可以理解为批处理任务,不过playbook有自己的语法格式
使用playbook可以方便的重复使用这些代码,可以移植到不同的机器上面,像函数一样,最大化的利用代码。在你使用Ansible的过程中,你也会发现,你所处理的大部分操作都是编写playbook。可以把常见的应用都编写playbook,之后管理服务器会变得很简单。

3.Ansible playbook格式

playbook由YAML语言编写,后缀名是 .yml 或者 .yaml。

  ---
  - hosts:webservers
    remote_user: root  #指定远程登录用户

    tasks:
      - name: hello
        command: hostname
 ...

开头我们一般会用连续三个连字号(-)区分不同内容,而 hosts 是执行的远程主机列表,tasks 就是我们的任务列表,在tasks里面一个name对应一个task,command也就是 ansible 单个模块.

4.ansible playbook 简单例子,远程创建一个文件

4.1  查看ansible hosts文件

cat /etc/ansible/hosts 

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
192.168.235.147


# Ex 2: A collection of hosts belonging to the 'webservers' group

[webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
43.143.98.52

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com

4.2  yaml编写的ansible playbook

[root@localhost home]# cat test.yml 
---
- hosts: webservers
  remote_user: root
  # 定义任务列表
  tasks:
    - name: create a newfile
      file: name=/home/new-test-file state=touch
...

注释:“---”表示文档的开始;“...”表示文档的结束;"-" 后通常跟随空格,用来清晰地表示出层级结构。hosts:主机列表;tasks:任务;在tasks里面一个name对应一个task;file是ansible模块

4.3 测试和执行过程

[root@localhost home]# ansible-playbook -C test.yml 

PLAY [webservers] ***********************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************************************************
ok: [43.143.98.52]

TASK [create a newfile] *****************************************************************************************************************************************************************************************************************************************************************
ok: [43.143.98.52]

PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************
43.143.98.52               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost home]# ansible-playbook  test.yml 

PLAY [webservers] ***********************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************************************************
ok: [43.143.98.52]

TASK [create a newfile] *****************************************************************************************************************************************************************************************************************************************************************
changed: [43.143.98.52]

PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************
43.143.98.52               : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0