云计算架构学习之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 ### ![](https://i-blog.csdnimg.cn/direct/343288fb2f4f45299a06481226ad3c7a.png) ### ![](https://i-blog.csdnimg.cn/direct/d07e69c6a2d142af8275ac574aae9a97.png) ### 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 ![](https://i-blog.csdnimg.cn/direct/be850284aae0449eafbc937130a45309.png) ### 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 \`\`\` ![](https://i-blog.csdnimg.cn/direct/6ea131ffec804b44a2d2205e7b2f7a18.png) ### ![](https://i-blog.csdnimg.cn/direct/571b9613537340cb8eb90c72cc193004.png) ### 4.Playbook重构nginx-php ![](https://i-blog.csdnimg.cn/direct/003288e2a7754161bb5ddda215db1fa2.png) \`\`\`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 ![](https://i-blog.csdnimg.cn/direct/62ccf560716140df9f37bdc5212f9002.png) ### 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 ![](https://i-blog.csdnimg.cn/direct/30bd0d3a7a3540ceb60f521efe12f3f5.png) ### 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 \`\`\` ![](https://i-blog.csdnimg.cn/direct/8d13ff0dc83e4ca0a454b66b88442e8e.png) ![](https://i-blog.csdnimg.cn/direct/5a88133027ba476ebe7913da42cdd916.png) ![](https://i-blog.csdnimg.cn/direct/08770a4239fd4d1aa2298c2236d9106e.png) ![](https://i-blog.csdnimg.cn/direct/f17c33d92b684eaf8b1fb656035c77af.png) ## 二、Ansible-流程控制 ### 1.vars变量定义方法 ### 2.vars变量定义方法 ### 3.变量注册 ### 4.when判断语法格式 ### 5.when判断案例 ### 6.handlers模块 ### 7.nfs服务重构 ## 三、Ansible-字典循环-roles角色 ### 1.字典循环 ### 2.tasks任务整合到一个文件 ### 3.jinja2的循环和判断语法 ### 4.rsync使用jinja2模版重构 ### 5.Roles角色重新编排rsync ### 6.Roele角色重新编排nfs

相关推荐
知识分享小能手24 分钟前
R语言入门学习教程,从入门到精通,R语言日期和时间序列(6)
开发语言·学习·r语言
噜噜噜阿鲁~1 小时前
python学习笔记 | 9.2、模块-安装第三方模块
笔记·python·学习
南境十里·墨染春水2 小时前
linux学习进展 守护进程
linux·服务器·学习
Ares-Wang2 小时前
AI》》 监督学习,无监督学习,半监督学习、强化学习 、深度学习 统计学的常用方法
人工智能·深度学习·学习
Bechamz2 小时前
大数据开发学习Day31
大数据·学习·ajax
-SOLO-3 小时前
Python 爬取小红书 文章标题和内容 仅供学习
android·python·学习
科技林总3 小时前
【系统分析师】14.6 测试策略与过程
学习
森屿~~4 小时前
CMA-ES:从搜索分布自适应到协方差矩阵学习
学习·elasticsearch·矩阵
程序员卷卷狗5 小时前
Claude Code工作原理学习笔记:从Agent Loop到工具调用
chrome·笔记·学习
li星野5 小时前
滑动窗口五题通关:从最小覆盖子串到水果成篮(Python + C++)
c++·python·学习