10,ansible10

ansible-doc: 类似linux命令在命令行操作命令。

什么场景下会用playbook?
	是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。
	将多个play组织在一个playbook中,即可以让它们联同起来按事先编排的机制同唱一台大戏。下面是一个简单示例。
---
- hosts: webservers
  tasks:          
  - name: installed	apache
    yum: name=httpd state=latest
	
  - name: started apache
    service: name=httpd state=started
	  

什么场景下会用roles?
	假如我们现在有3个被管理主机,第一个要配置成httpd,第二个要配置成php服务器,第三个要配置成MySQL服务器。我们如何来定义playbook?
	第一个play用到第一个主机上,用来构建httpd,第二个play用到第二个主机上,用来构建php,第三个play用到第三个主机上,用来构建MySQL。
	这些个play定义在playbook中比较麻烦,将来也不利于模块化调用,不利于多次调。比如说后来又加进来一个主机,这个第4个主机既是httpd服务器,又是php服务器,我们只能写第4个play,上面写上安装httpd和php。
	这样playbook中的代码就重复了。

	为了避免代码重复,roles能够实现代码重复被调用。定义一个角色叫websrvs,第二个角色叫phpappsrvs,第三个角色叫dbsrvs。那么调用时如下来调用:
---
- hosts: nginx 
  gather_facts: no
  roles:
    - role: ngsrvs

- hosts: mysql
  gather_facts: no
  roles:
    - role: dbsrvs

- hosts: php
  gather_facts: no
  roles:
    - role: phpsrvs
    - role: ngsrvs

这样代码就可以重复利用了,每个角色可以被独立重复调用。




[root@centos7 ~]# tree demo/
demo/
├── hosts
├── roles
│   ├── common
│   │   ├── files
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   ├── templates
│   │   └── vars
│   ├── dbsrvs
│   │   ├── files
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   └── vars
│   ├── ngsrvs
│   │   ├── files
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   └── vars
│   └── phpsrvs
│       ├── files
│       ├── handlers
│       ├── meta
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       └── vars
└── site.yml

29 directories, 5 files
[root@centos7 ~]# 
[root@centos7 ~]# ll demo/
total 8
-rw-r--r--. 1 root root 199 Nov 11 20:30 hosts
drwxr-xr-x. 6 root root  63 Nov 11 20:37 roles
-rw-r--r--. 1 root root 211 Nov 11 20:47 site.yml
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# cat demo/hosts 
[nginx]
192.168.0.11 ansible_ssh_user=root ansible_ssh_pass=123456
[php]
192.168.0.12 ansible_ssh_user=root ansible_ssh_pass=123456
[mysql]
192.168.0.13 ansible_ssh_user=root ansible_ssh_pass=123456
[root@centos7 ~]# 

[root@centos7 ~]# 
[root@centos7 ~]# ll demo/roles/common/tasks/
total 0
[root@centos7 ~]# cat demo/roles/dbsrvs/tasks/main.yml 
- name: db task test
  debug: msg="db task test"
[root@centos7 ~]# 
[root@centos7 ~]# cat demo/roles/ngsrvs/tasks/main.yml 
- name: nginx task test
  debug: msg="nginx  task test"
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# cat demo/roles/phpsrvs/tasks/main.yml 
- name: php task test
  debug: msg="php task test"
[root@centos7 ~]# 
[root@centos7 ~]# cat demo/site.yml 
---
- hosts: nginx 
  gather_facts: no
  roles:
    - role: ngsrvs

- hosts: mysql
  gather_facts: no
  roles:
    - role: dbsrvs

- hosts: php
  gather_facts: no
  roles:
    - role: phpsrvs
    - role: ngsrvs
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# ansible-playbook -i demo/hosts demo/site.yml --syntax-check

playbook: demo/site.yml
[root@centos7 ~]# 
[root@centos7 ~]# ansible-playbook -i demo/hosts demo/site.yml

PLAY [nginx] *************************************************************************************************************************************************************************************

TASK [ngsrvs : nginx task test] ******************************************************************************************************************************************************************
ok: [192.168.0.11] => {
    "msg": "nginx  task test"
}

PLAY [mysql] *************************************************************************************************************************************************************************************

TASK [dbsrvs : db task test] *********************************************************************************************************************************************************************
ok: [192.168.0.13] => {
    "msg": "db task test"
}

PLAY [php] ***************************************************************************************************************************************************************************************

TASK [phpsrvs : php task test] *******************************************************************************************************************************************************************
ok: [192.168.0.12] => {
    "msg": "php task test"
}

TASK [ngsrvs : nginx task test] ******************************************************************************************************************************************************************
ok: [192.168.0.12] => {
    "msg": "nginx  task test"
}

PLAY RECAP ***************************************************************************************************************************************************************************************
192.168.0.11               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.0.12               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.0.13               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@centos7 ~]# 

  

posted @ 2020-11-19 00:15  pwcc  阅读(113)  评论(0)    收藏  举报