python中Ansible模块的Playbook的具体使⽤
Playbook
在上⼀节中,我们详细介绍了Ansible提供的⼀些常⽤模块。可以看到,Ansible中的每个模块专注于某⼀⽅⾯的功能。虽然每个模块实现的功能都⽐较简单,但是,将各个模块组合起来就可以实现⽐较复杂的功能。在Ansible中,将各个模块组合起来的⽂件是⼀个YAML格式的配置⽂件。这个配置⽂件,在Ansible中称为Playbook。
在这⼀节中,我们将循序渐进地介绍Ansible中的Playbook,我们将⾸先介绍Playbook的定义,然后介绍如何使⽤Playbook完成远程服务器部署,之后详细介绍Playbook的基本语法,使⽤Playbook的基本讲法就能够完成⼤部分的部署任务。
在这⼀节中,们将介绍如何使⽤Playbook的基本语法完成nginx与MongoDB的部署,最后,我们介绍了部分Playbook的⾼级语法。
1、Playbook的定义
Playbook不同于其他使⽤单个模块操作远程服务器,Playbook的功能更加强⼤。如果只使⽤Playbook的基本功能,那么,Playbook是⼀个⾮常简单的配、管理和部署系统。此外,Playbook也可以实现各种⾼级功
能,如指定任务的执⾏顺序,委派其他主机来执⾏某⼀个任务,与监控服务器和负载均衡组件进⾏交互等。
有⼀个⾮常恰当的⽐喻,,Ansible中的模块类似于Linux下的命令,Ansible中的Playbook类似于Linux下的Shell脚本⽂件。Shell脚本⽂件将各个Linux命令组合起来,以此实现复杂的功能,Playbook将各个模块组合起来也可以实现复杂的部署功能。在shell脚本中,除了调⽤Linux命令以外,还有⼀些基本的语法,如变量定义、if语句、for循环等。在Playbook中,⼀⽅⾯通过YAML格式进⾏定义提⾼Playbook的可读性、可维护性,降低⼯程师的学习负担;另⼀⽅⾯,Ansible提供了若⼲可以应⽤在Playbook中的选项,以便⼯程师实现更加⾼级的功能。
⼀个Playbook可以包含⼀到多个Play,每⼀个Play是⼀个完整的部署任务。在Play中,我们需要指定对哪些远程服务器执⾏操作,以及对这些远程服务器执⾏哪些操作。
下⾯是⼀个名为l的Playbook。在这个Playbook中,我们定义了两个Play,前者⽤来在数据库服务器上部署MongoDB,后者⽤来在web服务器上部署“应⽤”。这⾥只是为了对Playbook进⾏演⽰,并没有真的部署应⽤。
[root@python ~]# vim l
---
- hosts: dbservers
become: yes
become_method: sudo
tasks:
- name: install mongodb
yum: name=mongodb-server state=present
- hosts: webservers
tasks:
- name: copy file
copy: src=/ dest=/
- name: change mode
file: dest=/ mode=655 owner=root group=root
这个Playbook中包含了两个Play。⼀个Playbook可以包含⼀到多个Play,所以即使Playbook中值包含⼀个Play,也需要使⽤列表的形式进⾏定义。在YAML语法中,“- hosts”前⾯的“-”表⽰定义列表。
在Ansible中,⼀个Play必须包含以下两项:
1. hosts:需要对哪些远程服务器执⾏操作
2. tasks:需要在这些服务器上执⾏的任务列表
例如,对web服务器进⾏部署时,我们仅仅使⽤了hosts和tasks两个选项。前者表⽰对哪些服务器执⾏操作,后者表⽰对服务器执⾏哪些操作。在部署数据库服务器时需要安装软件,因此使⽤了become与become_method两个选项,⽤来表⽰使⽤管理员的⾝份去安装MongoDB数据库。
⼀个Play可以包含⼀到多个task,因此task也必须以YAML的列表形式进⾏定义。可以看到,在这个例⼦中,对数据库服务器进⾏操作时仅包含了⼀个task,对web服务器进⾏部署时包含了两个task。
在Ansible中,task有两种定义形式:
1. action:module options
2. module:options
前⼀种形式是Ansible的旧版本语法,第2种形式是新版本的语法,直接使⽤模块的名称作为键,使⽤模块的参数作为值。如下所⽰:
- name: install httpd
yum: name=httpd update_cache=yes state=present
在安装Apache的例⼦中,“name=httpd update_cache=yes state=present”是⼀个完整的字符串,⽽不是⼀个字典。只是字符串的值是⼀
个“key=value”形式的参数。
在参数较多时,为了增加Playbook的可读性,我们也可以像下⾯这样定义⼀个task:
- name: install httpd
yum: >
name=httpd
update_cache=yes
state=present
在Ansible中,当参数较长时,除了使⽤“>”进⾏折叠换⾏以外,也可以使⽤缩进字块的形式:
- name: install httpd
yum:
name: httpd
update_cache: yes
state: present
虽然从字⾯来看,这两种指定参数的⽅式相差不⼤。但是,从YAML的语法来说,这是完全不同的两个⽅法。前者是⼀个⽐较长的字符串,后者是⼀个字典。
task的定义中,name是可选的。所以,像下⾯这样定义task也是完全合法的:
- yum: name=httpd update_cache=yes state=present
name的作⽤在于,执⾏Playbook时作为注释进⾏显⽰,以便使⽤者知道当前执⾏到哪⼀步。因此,在定义task时,⼀般都会定义name字段。
在实际⼯作中,虽然⼀个Playbook可以包含多个Play,但是为了Playbook的可读性和可维护性,我们⼀般只会在Playbook中编写⼀个Play。例如,对于这⾥的例⼦,我们可以将l这个Playbook拆分成两个Playbook,分别名为db.yml与l。其中,db.yml⽂件包含了与数据库服务器相关的部署任务,l⽂件包含了与web服务器相关的部署任务。
当我们需要部署数据库服务器和web服务器时,可以先执⾏db.yml⽂件,再执⾏l⽂件。除此之外,Ansible还提供了⼀种便捷⽅式来处理这种情况。例如,我们可以编写⼀个名为l的Playbook,它的内容如下:
---
- include: db.yml
- include: l
include选项是Ansible提供的,⽤于在⼀个Playbook中导⼊其他Playbook。在Ansible中,只需要使⽤include选项导⼊其他Playbook⽂件,执⾏这个Playbook时,被导⼊的Playbook便会依次执⾏。
上⾯详细介绍了Ansible的Playbook定义,这个Playbook定义虽然⽐较简单,但是,是⼀个⽐较完整的Playbook例⼦。在实际⼯作中使⽤的Playbook也不会⽐这个Playbook复杂很多。
我们接下来将介绍如何使⽤ansible-playbook命令执⾏Playbook,然后再介绍Playbook的其他语法。
2、ansible拆分l
查看⼀下所需⽂件是否正确
[root@python ~]# cat hosts
127.0.0.1
[webservers]
192.168.1.60
[dbservers]
192.168.1.80
[common:children]
dbservers
webservers
[root@python ~]# cat /etc/ansible/ansible.cfg
[defaults]
remote_user = root
remote_port = 22
inventory = /root/hosts
拆分l
[root@python ~]# l
---
- hosts: dbservers
become: yes
become_method: sudo
tasks:
- name: install mongodb
yum: name=mongodb-server state=present #mongodb-server 可欢成其他服务如(git)
[root@python ~]# l
---
- hosts: webservers
tasks:
- name: copy file
copy: src=/ dest=/
-
name: change mode
file: dest=/ mode=655 owner=root group=root
[root@python ~]# l
---
- include: db.yml
- include: l
[root@python ~]# touch /
[root@python ~]# touch /
3、使⽤Ansible-playbook执⾏Playbook
上⼀⼩节中,我们简单地介绍了Playbook的定义。那么,当我们有了⼀个Playbook⽂件以后,如何执⾏这个⽂件完成应⽤部署呢?我们知道,Ansible安装完成以后存在多个可执⾏的命令⾏⼯具,其中,ansible-playbook便是⽤于执⾏Playbook的命令⾏⼯具。
ansible-playbook的执⾏⽅式如下:
ansible-playbook l
ansible-playbook命令也有若⼲命令⾏选项,其中,有部分选项与ansible命令相同。Ansible中也存在⼀些ansible-playbook特有的命令⾏选项。
ansible-playbook命令与ansible命令相同的命令⾏选项:
-T --timeout:建⽴SSH连接的超时时间
--key-file --private-key:建⽴SSH连接的私钥⽂件
-i --inventory-file:指定Inventory⽂件,默认是/etc/ansible/hosts
-f --forks:并发执⾏的进程数,默认为5
--list-hosts:playbooks匹配的服务器列表。
ansible-playbook也有⼀个名为--list-hosts的选项,该选项的作⽤是列出匹配的服务器列表。例如,在我们这个 Playbook的例⼦中,hosts⽂件的内容如下:
127.0.0.1
[webservers]
192.168.1.60
[dbservers]
192.168.1.80
[common:children]
dbservers
webservers
我们知道,Ansible中的Play定义了需要对哪些服务器执⾏哪些操作,也就是说,每⼀个Play都可以指定匹配的远程服务器。在我们这个Playbook的例⼦中,对数据库服务器安装MongoDB,对web服务器部署“应⽤“。因此,ansible-playbook命令与ansible命令的--list-hosts选项输出的结果将会⼤不相同。ansible-playbook命令的--list-hosts选项输出的结果如下:
[root@python ~]# l --list-hosts
ansible-playbook命令有⼀些特有的选项,如下所⽰:
--list-tasks:列出任务列表
--step:每执⾏⼀个任务后停⽌,等待⽤户确认
--syntax-check:检查Playbook语法
-C --check:检查当前这个Playbook是否会修改远程服务器,相当于预测Playbook的执⾏结果。
这⾥的⼏个选项,除了--step以外,其他⼏个选项都不会执⾏Playbook中的任务。这些选项存在主要是为了便于调试Playbook。例如,--list-tasks选项,该选项⽤来显⽰当前Playbook中的任务列表。当Playbook⽐较⼤时,可以通过这个⽅式快速查看任务列表。如下所⽰:
[root@python ~]# l --list-tasks
playbook: l
play #1 (dbservers): dbservers  TAGS: []
tasks:
install mongodb  TAGS: []
play #2 (webservers): webservers TAGS: []
tasks:
copy file TAGS: []
change mode  TAGS: []
当我们查看任务列表时,任务的名称就是task的name字段。因此,name的定义需要具有较好的描述性,让使⽤者通过名字就能知道该任务需要做什么事情。
--step选项类似于编程语⾔中的单步调试。当我们使--step选项执⾏Playbook时,ansible-playbook在每⼀个任务之前都会停住,等侍⽤户输⼊yes,、no或continue。如下所⽰:
[root@python ~]# l --step
[DEPRECATION WARNING]: 'include' for playbook includes. You should use
'import_playbook' instead. This feature will be removed in version 2.12.
Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.
PLAY [dbservers] ***************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: *********************
TASK [Gathering Facts] *********************************************************
ok: [192.168.1.80]
Perform task: TASK: install mongodb (N)o/(y)es/(c)ontinue: y
Perform task: TASK: install mongodb (N)o/(y)es/(c)ontinue: *********************
TASK [install mongodb] *********************************************************
changed: [192.168.1.80]
PLAY [webservers] **************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: *********************
TASK [Gathering Facts] *********************************************************
ok: [192.168.1.60]
Perform task: TASK: copy file (N)o/(y)es/(c)ontinue: y
Perform task: TASK: copy file (N)o/(y)es/(c)ontinue: ***************************
TASK [copy file] ***************************************************************
changed: [192.168.1.60]
Perform task: TASK: change mode (N)o/(y)es/(c)ontinue: y
Perform task: TASK: change mode (N)o/(y)es/(c)ontinue: *************************
TASK [change mode] *************************************************************
changed: [192.168.1.60]
PLAY RECAP *********************************************************************
192.168.1.60        : ok=3  changed=2  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
192.168.1.80        : ok=2  changed=1  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
输⼊yes以后,任务将会继续执⾏,并在下⼀个任务时停⽌,等待⽤户继续输⼊。当我们输⼊continue时,Ansible会执⾏完当前这个Play,当执⾏到下⼀个Play时再停⽌,并等待⽤户输⼊。
⼆、Playbook的详细语法
到⽬前为⽌,我们已经学习了如何编写Playbook以及如何运⾏Playbook。但是,我们仅仅介绍了最简单的Playbook。在这⼀节中,我们将会介绍Playbook是如何通过不同的选项提供丰富多样的功能。灵活使⽤这些选项,能够编写出形式各异的Playbook,以此应对⾃动部署中的各种情况。
在定义Play时,只有hosts与tasks是必选选项,其他选项都是根据需要添加的。在这⼀⼩节中。我们将介绍Playbook提供的不同功能,以Playbook的功能为线索,介绍Play与task中可以使⽤的选项。
(1)权限
在Ansible中,默认使⽤当前⽤户连接远程服务器执⾏操作。我们也可以在anaible.cfg⽂件中配置连接远程服务器的默认⽤户。此外,如果是不同的⽤户使⽤不同类型的远程服务器,那么也可以在Playbook的Play定义中指定连接远程服务器的⽤户。例如,我们可以指定执⾏Play的⽤户:
---
- hosts: webservers
remote_user: root
⽤户可以细分每⼀个task,如下所⽰:
---
- hosts: werbservers
remote_user: root
tasks:
- name: test connection
ping:
remote_user: yourname
很多时候,我们需要的不是以某个特定⽤户连接远程服务器,⽽是在需要更⾼级别的权限时,使⽤管理员⾝份去执⾏操作。在ansible中,可以通过become与become_ method选项实现:
---
- hosts: werbservers
remote_user: root
become: yes
与remote_user选项类似,我们也可以为单个任务使⽤管理员权限,如下所⽰:
---
- hosts: werbservers
remote_user: yourname
tasks:
- name: installed nginx
service: name=nginx state=started
become: yes
become_method: sudo
实例
先修改远程服务器中test的权限为(0:0)
[root@192 ~]# vim /etc/passwd
test:x:0:0::/home/test:/bin/bash
[root@python ~]# chown test:root /etc/ansible/*
[root@python ~]# su test
[test@python root]$ cd
[test@python ~]$ vim hosts
[db]
192.168.1.60
[test@python ~]$ l
---
- hosts: db
remote_user: root      #远程服务器登陆的⽤户
tasks:
- name: installed nginx
become: yes
become_method: sudo
ping:
remote_user: root    #远程服务器登陆的⽤户
python安装教程非常详细
[test@python ~]$ vim /etc/ansible/ansible.cfg
[defaults]
inventory = /home/test/hosts
执⾏⼀下
[test@python ~]$ sudo l --step
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for test:
PLAY [db] **********************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: *********************
TASK [Gathering Facts] *********************************************************
Enter passphrase for key '/root/.ssh/id_rsa':
ok: [192.168.1.60]
Perform task: TASK: installed nginx (N)o/(y)es/(c)ontinue:
Perform task: TASK: installed nginx (N)o/(y)es/(c)ontinue: *********************
PLAY RECAP *********************************************************************
192.168.1.60        : ok=1  changed=0  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
(2)通知

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。