ansible-playbook之roles实现,ansible-playbook


ansible-playbook之roles实现

一、目标

两台主机:192.168.50.138(安装ansible)、192.168.50.139

[webservers]
192.168.50.138 httpd_point=80
192.168.50.139 httpd_point=8080

实现在两台主机上同时安装apache,提供配置文件,并启动。在139主机上提供mysql服务。

- hosts: all
  remote_user: root
  roles:
  - apache

- hosts: 192.168.50.139
  remote_user: root
  roles:
  - apache
  - mysql

二、roles之apache

1、创建目录

 mkdir -pv /ansible_playbooks/roles/{apache,mysql}/{tasks,files,templates,meta,handlers,vars}

2、复制一份httpd配置文件作为template

cp /etc/httpd/conf/httpd.conf /ansible_playbooks/roles/apache/templates/

编辑此配置文件做变量引用

Listen {{ httpd_point }}

3、编写tasks任务

[root@node1 apache]# vim tasks/main.yml 

- name: install httpd
  yum: name=httpd state=latest
- name: install config
  template: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  notify:
  - restart apache
- name: start apache
  service: name=httpd state=started

4、编写handlers

[root@node1 apache]# vim handlers/main.yml 

- name: restart apache
  service: name=httpd state=restarted

三、roles之mysql

1、复制一份mysql配置文件到files文件中

cp /etc/my.cnf /ansible_playbooks/roles/mysql/files/
2、编写tasks任务
[root@node1 mysql]# vim /ansible_playbooks/roles/mysql/tasks/main.yml 

- name: install mysql
  yum: name=mariadb state=latest
- name: install config-mysql
  copy: src=my.cnf dest=/etc/my.cnf
- name: start mariadb
  service: name=mariadb state=started

四、编写剧本

[root@node1 mysql]# vim /ansible_playbooks/site.yml 

- hosts: all
  remote_user: root
  roles:
  - apache

- hosts: 192.168.50.139
  remote_user: root
  roles:
  - apache
  - mysql

五、执行剧本

[root@node1 handlers]# ansible-playbook /ansible_playbooks/site.yml 

PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]
ok: [192.168.50.138]

TASK [apache : install httpd] ************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.138]
ok: [192.168.50.139]

TASK [apache : install config] ***********************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]
ok: [192.168.50.138]

TASK [apache : start apache] *************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]
ok: [192.168.50.138]

PLAY [192.168.50.139] ********************************************************************************************************************************************************************************************************************************************************

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

TASK [apache : install httpd] ************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]

TASK [apache : install config] ***********************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]

TASK [apache : start apache] *************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]

TASK [mysql : install mysql] *************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]

TASK [mysql : install config-mysql] ******************************************************************************************************************************************************************************************************************************************
changed: [192.168.50.139]

TASK [mysql : start mariadb] *************************************************************************************************************************************************************************************************************************************************
changed: [192.168.50.139]

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
192.168.50.138             : ok=4    changed=0    unreachable=0    failed=0   
192.168.50.139             : ok=11   changed=2    unreachable=0    failed=0   

成功!!!

相关内容

    暂无相关文章