当前位置:   article > 正文

8.20-ansible剧本的使用+roles的应用

8.20-ansible剧本的使用+roles的应用

一、设置ansible的组

  [root@m0 ~]# vim /etc/ansible/hosts
  ​
  ​
  s0 ansible_ssh_host=192.168.2.110 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
  ​
  s1 ansible_ssh_host=192.168.2.111 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
  ​
  s2 ansible_ssh_host=192.168.2.112 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
  ​
  [s]
  s0
  s1
  s2

二、使用剧本修改nginx的端口号为80

  
  [root@m0 ~]# vim /etc/ansible/playbook/nginx.yml
  ​
  ---
  -       hosts:           s
          remote_user:     root
          tasks:
          -       name:   卸载httpd
                  yum:    name=httpd   state=absent
  ​
          -       name:   安装nginx
                  yum:    name=nginx   state=present
  ​
          -       name:   修改资源文件
                  shell:  echo 'i am nginx,port is 80' > /usr/share/nginx/html/index.html
  ​
          -       name:   修改端口
                  command:        sed -i '/listen/s/80/8080/g' /etc/nginx/nginx.conf
                  notify:
                  -       restart nginx
  ​
          -       name:   启动服务
                  service:        name=nginx   state=started  enabled=yes
  ​
          handlers:
                  -       name:   restart nginx
                          service:        name=nginx   state=restarted
  ...
  ​
  ​
  [root@m0 ~]# ansible-playbook /etc/ansible/playbook/nginx.yml 

三、roles

1.环境部署

  [root@m0 ~]# cd /etc/ansible/roles
  [root@m0 roles]# mkdir nginx
  [root@m0 roles]# ls
  nginx
  [root@m0 roles]# cd nginx/
  [root@m0 nginx]# mkdir files
  ​
  [root@m0 nginx]# mkdir tasks
  ​
  [root@m0 nginx]# mkdir handlers/
  ​
  [root@m0 nginx]# mkdir handlers
  ​
  [root@m0 nginx]# mkdir templates
  ​
  [root@m0 nginx]# mkdir vars
  ​
  [root@m0 nginx]# mkdir meta
  [root@m0 nginx]# tree
  .
  ├── files
  ├── handlers
  ├── meta
  ├── tasks
  ├── templates
  └── vars
  ​
  6 directories, 0 files
  [root@m0 nginx]# touch files/main.yml
  [root@m0 nginx]# touch handlers/main.yml
  [root@m0 nginx]# touch tasks/main.yml
  [root@m0 nginx]# touch vars/main.yml
  [root@m0 nginx]# touch meta/main.yml
  ​
  [root@m0 nginx]# rm -rf files/main.yml 
  [root@m0 nginx]# tree
  .
  ├── files
  ├── handlers
  │   └── main.yml
  ├── meta
  │   └── main.yml
  ├── tasks
  │   └── main.yml
  ├── templates
  └── vars
      └── main.yml
  ​
  6 directories, 4 files
  ​
  [root@m0 nginx]# cd

2.下载nginx的主配置文件

  
  [root@m0 ~]# vim /etc/ansible/playbook/test002.yml 
  ​
  ---
  -       hosts:  s
          remote_user:    root
          roles:
                  -       nginx
  ...

3.运行文件

  
  [root@m0 ~]# ansible-playbook /etc/ansible/playbook/test002.yml 
  ​
  PLAY [s] ***************************************************************************
  ​
  TASK [Gathering Facts] *************************************************************
  ok: [s1]
  ok: [s2]
  ok: [s0]
  ​
  PLAY RECAP *************************************************************************
  s0                         : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  s1                         : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  s2                         : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  ​

4.卸载nginx

  [root@m0 ~]# ansible s -m yum -a 'name=nginx state=absent'

5.编辑配置文件-安装nginx

  
  [root@m0 ~]# vim /etc/ansible/roles/nginx/tasks/main.yml 
  ​
  ---
  ​
  -       name:   卸载httpd
          yum:    name=httpd      state=absent
  ​
  -       name:   卸载nginx
          yum:    name=nginx      state=absent
  ​
  -       name:   安装nginx
          yum:    name=nginx      state=present
  ​
  -       name:   启动服务
          service:        name=nginx      state=started   enabled=yes
  ...
  ​

6.运行main.yml配置文件

  
  [root@m0 ~]# ansible-playbook /etc/ansible/playbook/test002.yml 
  ​
  PLAY [s] ***************************************************************************
  ​
  TASK [Gathering Facts] *************************************************************
  ok: [s2]
  ok: [s1]
  ok: [s0]
  ​
  TASK [nginx : 卸载httpd] *************************************************************
  ok: [s1]
  ok: [s2]
  ok: [s0]
  ​
  TASK [卸载nginx] *********************************************************************
  ok: [s0]
  ok: [s1]
  ok: [s2]
  ​
  TASK [安装nginx] *********************************************************************
  changed: [s1]
  changed: [s2]
  changed: [s0]
  ​
  TASK [nginx : 启动服务] ****************************************************************
  changed: [s0]
  changed: [s1]
  changed: [s2]
  ​
  PLAY RECAP *************************************************************************
  s0                         : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  s1                         : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  s2                         : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

四、使用roles搭建lnmp

1.nginx修改端口为8080

2.mariadb中创建eleme数据库

3.创建user表

字段类型约束字段名称
idint主键约束、自增编号
usernamevarchar非空主键账号
passwordvarchar非空主键密码
remarkvarchar没有主键账号权限说明

1.主配置文件

  
  [root@m0 ~]# vim /etc/ansible/playbook/test003.yml
  ​
  ---
  -       hosts: s
          remote_user: root
          roles:
                  -       mariadb
  ...
  ​

2.拷贝文件nginx且改名为mariadb

  
  [root@m0 ~]# cd /etc/ansible/roles/
  [root@m0 roles]# ls
  nginx
  [root@m0 roles]# cp -r nginx mariadb
  [root@m0 roles]# ls
  mariadb  nginx
  [root@m0 roles]# cd mariadb/
  [root@m0 mariadb]# ls
  files  handlers  meta  tasks  templates  vars

3.创建script目录

  [root@m0 mariadb]# cd ..
  [root@m0 roles]# mkdir script
  [root@m0 roles]# cd script/

4.写一个数据库的脚本

  
  [root@m0 script]# vim dbtable.sh
  ​
  #!/bin/bash
  mysql << EOF
  create database if not exists eleme charset utf8mb4;
  ​
  use eleme;
  create table user(id int primary key auto_increment,username varchar(45) not null,
  password varchar(45) not null,remark varchar(45));
  quit
  EOF

5.修改配置文件

  [root@m0 mariadb]# vim /etc/ansible/roles/mariadb/tasks/main.yml
  ​
  ---
  ​
  -       name:   卸载mariadb
          yum:    name=mariadb,mariadb-server,mariadb-devel      state=absent
  ​
  -       name:   安装mariadb
          yum:    name=mariadb,mariadb-server,mariadb-devel      state=present
  ​
  -       name:   启动mariadb服务
          service:        name=mariadb      state=started   enabled=yes
  ​
  -       name:   执行脚本,创建数据库和数据表
          script: /etc/ansible/roles/script/dbtable.sh
  ...

6.运行:

  
  [root@m0 script]# cd
  [root@m0 ~]# ansible-playbook /etc/ansible/playbook/test003.yml
  ​
  PLAY [s] ***************************************************************************
  ​
  TASK [Gathering Facts] *************************************************************
  ok: [s1]
  ok: [s0]
  ok: [s2]
  ​
  TASK [卸载mariadb] *******************************************************************
  changed: [s2]
  changed: [s0]
  changed: [s1]
  ​
  TASK [安装mariadb] *******************************************************************
  changed: [s0]
  changed: [s2]
  changed: [s1]
  ​
  TASK [启动mariadb服务] *****************************************************************
  changed: [s0]
  changed: [s2]
  changed: [s1]
  ​
  TASK [mariadb : 执行脚本,创建数据库和数据表] ****************************************************
  changed: [s0]
  changed: [s2]
  changed: [s1]
  ​
  PLAY RECAP *************************************************************************
  s0                         : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  s1                         : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  s2                         : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

7.在被测试主机上进行验证

  
  # s0主机
  [root@s0 ~]# mysql
  Welcome to the MariaDB monitor.  Commands end with ; or \g.
  Your MariaDB connection id is 3
  Server version: 5.5.68-MariaDB MariaDB Server
  ​
  Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  ​
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  ​
  MariaDB [(none)]> show databases;
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | eleme              |
  | mysql              |
  | performance_schema |
  | test               |
  +--------------------+
  5 rows in set (0.00 sec)
  ​
  MariaDB [(none)]> use eleme;
  Reading table information for completion of table and column names
  You can turn off this feature to get a quicker startup with -A
  ​
  Database changed
  MariaDB [eleme]> show tables;
  +-----------------+
  | Tables_in_eleme |
  +-----------------+
  | user            |
  +-----------------+
  1 row in set (0.00 sec)
  ​
  MariaDB [eleme]> select * from user;
  Empty set (0.00 sec)
  ​
  # s1主机
  [root@s1 ~]# mysql
  Welcome to the MariaDB monitor.  Commands end with ; or \g.
  Your MariaDB connection id is 3
  Server version: 5.5.68-MariaDB MariaDB Server
  ​
  Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  ​
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  ​
  MariaDB [(none)]> show databases;
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | eleme              |
  | mysql              |
  | performance_schema |
  | test               |
  +--------------------+
  5 rows in set (0.00 sec)
  ​
  MariaDB [(none)]> use eleme;
  Reading table information for completion of table and column names
  You can turn off this feature to get a quicker startup with -A
  ​
  Database changed
  MariaDB [eleme]> show tables;
  +-----------------+
  | Tables_in_eleme |
  +-----------------+
  | user            |
  +-----------------+
  1 row in set (0.00 sec)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/1011161
推荐阅读
相关标签
  

闽ICP备14008679号