当前位置: 首页 > article >正文

云计算架构学习之Ansible-playbook实战、Ansible-流程控制、Ansible-字典循环-roles角色

一、Ansible-playbook实战

1.Ansible-playbook安装软件

```bash
#编写yml
[root@ansible ansible]# cat wget.yml
- hosts: backup
  tasks:
    - name: Install wget
      yum:
        name: wget
        state: present
        
#检查playbook的语法
[root@ansible ansible]# ansible-playbook  --syntax-check wget.yml
playbook: wget.yml


#执行playbook
[root@ansible ansible]# ansible-playbook wget.yml

2.Playbook重构backup服务

```bash
1.定义主机清单
[root@ansible ansible]# cat /etc/ansible/hosts
nfs ansible_ssh_host=10.0.0.31
backup ansible_ssh_host=10.0.0.41

2.写playbook重构
[root@ansible ansible]# cat backup.yml
- hosts: backup
  tasks:
    - name: Install Rsync Server
      yum:
        name: rsync
        state: present

    - name: Configure Rsync Server
      copy:
        src: rsyncd.conf
        dest: /etc/rsyncd.conf

    - name: Create www Group
      group:
        name: www
        gid: 666

    - name: Create User www
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: false
     
    - name: Configure passwd file
      copy:
        content: rsync_backup:123456
        dest: /etc/rsync.passwd
        mode: 0600

    - name: Create Dir /backup
      file:
        path: /backup
        state: directory
        owner: www
        group: www

    - name: Start Rsync Server
      systemd:
        name: rsyncd
        state: started
        enabled: yes

3.playbook重构nfs服务


```bash
1.定义主机清单
[root@ansible ansible]# cat /etc/ansible/hosts
nfs ansible_ssh_host=10.0.0.31
backup ansible_ssh_host=10.0.0.41

2.打通免秘钥
[root@ansible ~]# ssh-copy-id 10.0.0.31
3.写playbook
[root@ansible ansible]# cat nfs.yml
- hosts: nfs
  tasks:
    - name: Install NFS Server
      yum:
        name: nfs-utils
        state: present

    - name: Configure nfs Server
      copy:
        src: exports
        dest: /etc/

    - name: Create www Group
      group:
        name: www
        gid: 666

    - name: Create User www
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: false

    - name: Create /data/wp
      file:
        path: /data/wp
        state: directory
        owner: www
        group: www

    - name: Start NFS Server
      systemd:
        name: nfs
        state: started
        enabled: yes

客户端挂载:
[root@ansible ansible]# cat web.yml
- hosts: web01
  tasks:
    - name: Install nfs-utils
      yum:
        name: nfs-utils
        state: present

    - name: mount nfs /data/wp-->wordpress
      mount:
        src: 172.16.1.31:/data/wp
        path: /code/wordpress/wp-content/uploads/
        state: mounted
        fstype: nfs


```


4.Playbook重构nginx-php

```bash
1.定义主机清单
[root@ansible ~]# cat /etc/ansible/hosts
nfs ansible_ssh_host=10.0.0.31
backup ansible_ssh_host=10.0.0.41
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8

2.免秘钥
[root@ansible ~]# ssh-copy-id 10.0.0.8

3.playbook
[root@ansible ansible]# cat nginx.yml
- hosts: web02
  tasks:
    - name: Nginx Repo 
      yum_repository:
        name: nginx
        description: Nginx YUM repo
        baseurl: http://nginx.org/packages/centos/7/$basearch/
        gpgcheck: no
        enabled: yes


    - name: Install Nginx Server
      yum:
        name: nginx
        state: present

    - name: Configure Nginx Server
      copy:
        src: nginx.conf
        dest: /etc/nginx/

    - name: create group www
      group:
        name: www
        gid: 666

    - name: Create www user
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: false

    - name: Start Nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes

5.Playbook重构mariadb

[root@ansible ansible]# cat php.yml
- hosts: web02
  tasks:
    - name: Install PHP Server
      yum:
        name: php,php-bcmath,php-cli,php-common,php-devel,php-embedded,php-fpm,php-gd,php-intl,php-mbstring,php-mysqlnd,php-opcache,php-pdo,php-process,php-xml,php-json
        state: present


    - name: Configure PHP Server
      copy:
        src: www.conf
        dest: /etc/php-fpm.d/


    - name: Start PHP Server
      systemd:
        name: php-fpm
        state: started
        enabled: yes

6.整合playbook文件

[root@ansible ansible]# cat mysql.yml
- hosts: db01
  tasks:
    - name: Install mariadb Server
      yum:
        name: mariadb-server,python3-mysqlclient
        state: present

    - name: Start mariadb Server
      systemd:
        name: mariadb
        state: started
        enabled: yes

    - name: copy all.sql to 51
      copy:
        src: all.sql
        dest: /root/

    - name: Configure Mmriadb Server
      mysql_db:
        login_user: root
        login_host: localhost
        login_port: 3306
        name: all
        target: /root/all.sql
        state: import       

    - name: Restart mariadb
      systemd: 
        name: mariadb
        state: restarted
```

##### 07.部署wordpress

```bash
[root@ansible ansible]# cat wp.yml
- hosts: web02
  tasks:
    - name: Delete Default default.conf
      file:
        path: /etc/nginx/conf.d/default.conf
        state: absent

    - name: Copy wp.conf 
      copy:
        src: wp.conf
        dest: /etc/nginx/conf.d/

    - name: unarchive wp.tar.gz
      unarchive:
        src: wp.tar.gz 
        dest: /
        creates: /code/wordpress

    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted

```

二、Ansible-流程控制

1.vars变量定义方法

```bash
案例1.定义单个变量
[root@ansible ansible]# cat var.yml
- hosts: backup
  vars:
    pk: wget
  tasks:
    - name: Install package
      yum:
       name: "{{ pk }}"
       state: present

案例2.定义多个变量,使用列表的方式 -
[root@ansible ansible]# cat var.yml
- hosts: backup
  vars:
    pk: 
      - wget
      - tree
      - lrzsz
  tasks:
    - name: Install package
      yum:
       name: "{{ pk }}"
       state: present


案例3.定义路径的变量
#变量单独调用 必须加双引号
[root@ansible ansible]# cat var.yml
- hosts: backup
  vars:
    - pk1: 10.0.0.41
    - pk2: backup
  tasks:
    - name: create file
      file:
       path: "{{ pk1 }}"
       state: touch
       
#注意如果变量带路径则不需要使用""
[root@ansible ansible]# cat var.yml
- hosts: backup
  vars:
    - pk1: 10.0.0.41
    - pk2: backup
  tasks:
    - name: create file
      file:
       path: /root/"{{ pk1 }}"
       state: touch

2.vars变量定义方法

1.先创建存放变量的文件
[root@ansible ansible]# cat v.yml
pk1: lrzsz
pk2: tree

2.在play中调用比变量
[root@ansible ansible]# cat var.yml
- hosts: backup
  vars_files: v.yml
  tasks:
    - name: create file
      file:
       path: /root/{{ pk2 }}_{{ pk1 }}
       state: directory


案例: 定义变量,使用列表调用多个变量
[root@ansible ansible]# cat v.yml 
pk1: lrzsz
pk2: tree

[root@ansible ansible]# cat var.yml
- hosts: backup
  vars_files: v.yml
  tasks:
    - name: yum lrzsz wget
      yum:
       name: 
         - "{{ pk1 }}" 
         - "{{ pk2 }}"
       state: present

```

3.变量注册

```bash
内置变量通过setup模块来查看。
[root@ansible ansible]# cat var.yml 
- hosts: backup
  tasks:
    - name: Create file 
      file:
       path: /root/{{ ansible_hostname }}_{{ ansible_default_ipv4.address }} 
       state: directory

```

4.when判断语法格式

```bash
#尽量不使用此种方法
[root@ansible ansible]# cat /etc/ansible/hosts
nfs ansible_ssh_host=10.0.0.31

[lnmp]
backup ansible_ssh_host=10.0.0.41

web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
db01 ansible_ssh_host=10.0.0.51

[lnmp:vars]            # 给lnmp组定义变量
pk1=wget
pk2=lrzsz

调用
[root@ansible ansible]# cat var.yml 
- hosts: backup
  tasks:
    - name: Create file 
      file:
       path: /root/{{ pk1 }}_{{ pk2 }}
       state: directory

5.when判断案例

```bash
1.定义主机的变量
在当前目录创建host_vars目录,在host_vars下按照主机清单来定义变量文件
比如主机清单有
backup  ansible_ssh_host=10.0.0.41
则在host_vars下创建一个nfs文件,在backup文件中定义变量

[root@ansible oldboy]# cat host_vars/backup 
pk1: lrzsz
pk2: wget
[root@ansible oldboy]# cat vars.yml 
- hosts: backup
  tasks:
    - name: Install lrzsz wget
      yum:
        name:
          - "{{ pk1 }}"
          - "{{ pk2 }}"
        state: present

2.定义组的变量
主机清单:
[root@ansible oldboy]# cat /etc/ansible/hosts
nfs ansible_ssh_host=10.0.0.31
[lnmp]
backup ansible_ssh_host=10.0.0.41

web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
db01 ansible_ssh_host=10.0.0.51



[root@ansible oldboy]# cat group_vars/lnmp 
name: oldboy
age: 10
[root@ansible oldboy]# cat vars.yml 
- hosts: web01
  tasks:
    - name: touch file
      file:
        path: /root/{{ name }}_{{ age }} 
        state: touch


###可以对所有的组定义变量
[root@ansible oldboy]# cat group_vars/all 
name: old
age: 11

6.handlers模块

```bash
Ansible执行命令,有一些无法返回结果的
[root@ansible oldboy]# cat re.yml
- hosts: web01
  tasks:
    - name: list /root
      command: ls -l

通过变量注册返回ls -l的详细信息
[root@ansible oldboy]# cat li.yml
- hosts: web01
  tasks:
    - name:  list root
      command: ls -l
      register: ng_re


    - name: print ng_re env
      debug:
        msg: "{{ ng_re }}"




[root@ansible oldboy]# cat re.yml
- hosts: web01
  tasks:
    - name: Check Nginx Config
      command: nginx -t

      
通过变量注册返回nginx检查结果
[root@ansible oldboy]# cat re.yml
- hosts: web01
  tasks:
    - name: Check Nginx Config
      command: nginx -t
      register: ng_re


    - name: print ng_re env
      debug:
        msg: "{{ ng_re }}"


获取子集的值:
[root@ansible oldboy]# cat re.yml
- hosts: web01
  tasks:
    - name: Check Nginx Config
      command: nginx -t
      register: ng_re


    - name: print ng_re env
      debug:
        msg: "{{ ng_re.stderr_lines }}"
        
 
查看内置变量:
[root@ansible oldboy]# cat re.yml
- hosts: web01
  tasks:
    - name: Check Nginx Config
      command: nginx -t
      register: ng_re


    - name: print ng_re env
      debug:
        msg: "{{ ansible_kernel }}"

7.nfs服务重构


```bash
nfs案例
1.定义主机清单
[root@ansible ~]# cat /etc/ansible/hosts
backup ansible_ssh_host=10.0.0.41
db01 ansible_ssh_host=10.0.0.51
[lnmp]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
nfs ansible_ssh_host=10.0.0.31


[root@ansible oldboy]# cat nfs.yml
- hosts: lnmp
  vars:
    dir: /data/wp
  tasks:
    - name: Install nfs-utils
      yum:
        name: nfs-utils
        state: present

    - name: Configure nfs Server
      copy:
        src: exports
        dest: /etc/
      notify: Restart nfs Server
      when: ansible_hostname == "nfs"

    - name: create Group www
      group:
        name: www
        gid: 666

    - name: Create www User
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: false

    - name: Create "{{ dir }}"
      file:
        path: "{{ dir }}"
        state: directory
        owner: www
        group: www

    - name: Start nfs Server
      systemd:
        name: nfs
        state: started
        enabled: yes
 
    - name: web mount
      mount:
        src: 172.16.1.31:{{ dir }}
        path: /mnt
        fstype: nfs
        state: mounted
      when: ansible_hostname is search "web"


  handlers:
    - name: Restart nfs Server
      systemd:
        name: nfs
        state: restarted

三、Ansible-字典循环-roles角色

1.字典循环

2.tasks任务整合到一个文件

3.jinja2的循环和判断语法

4.rsync使用jinja2模版重构

5.Roles角色重新编排rsync

6.Roele角色重新编排nfs


http://www.kler.cn/a/553490.html

相关文章:

  • 青少年编程都有哪些比赛可以参加
  • 使用Java爬虫获取京东商品分类API接口(cat_get)的实现与解析
  • 红队视角出发的k8s敏感信息收集——日志与监控系统
  • C# 语法 vs. C++ 语法:全面对比与核心区别解析
  • 使用 NVM 随意切换 Node.js 版本
  • zookeeper有序临时结点实现公平锁的实践例子
  • 【数据挖掘】
  • python-leetcode 35.二叉树的中序遍历
  • 大数据 高并发 解决方案 Moebius
  • 什么是 BFC
  • 【第3章:卷积神经网络(CNN)——3.8 迁移学习与微调策略】
  • vue3结合后端传递过来的文件进行预览功能
  • 【RK3588嵌入式图形编程】-SDL2-构建模块化UI
  • DeepSeek(AI)如何赋能智能漏洞扫描与利用的思考
  • 掌握 ElasticSearch的 _source 过滤
  • 4.8 Hugging Face Evaluate:企业级模型评估实战指南
  • Javascript中的深拷贝详解
  • MySQL 主从复制原理
  • vscode将文件中行尾默认CRLF改为LF
  • 如何进行市场趋势分析:方法与案例指南