ansible的搭建配置

Ansible简介
    ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
Windows下Ansible工作模式
Ansible 从1.7+版本开始支持Windows,但前提是管理机必须为Linux系统,远程主机的通信方式也由SSH变更为PowerShell,同时管理机必须预安装Python的Winrm模块,方可和远程Windows主机正常通信,但PowerShell需3.0+版本且Management Framework 3.0+版本,实测Windows 7 SP1和Windows Server 2008 R2及以上版本系统经简单配置可正常与Ansible通信。简单总结如下:
(1)    管理机必须为Linux系统且需预安装Python Winrm模块
(2)    底层通信基于PowerShell,版本为3.0+,Management Framework版本为3.0+
(3)    远程主机开启Winrm服务
Ansible管理机部署安装
1). 对管理主机的要求
    目前,只要机器上安装了 Python 2.6 或 Python 2.7 (windows系统不可以做控制主机),都可以运行Ansible.
主机的系统可以是 Red Hat, Debian, CentOS, OS X, BSD的各种版本,等等.
这个节点就是安装ansible的节点。
Shell(NMC)># yum -y install epel-release
Shell(NMC)># yum -y install ansible.noarch


配置管理主机:vim /etc/ansible/hosts
免密登陆设置:
   49  ssh-keygen -t rsa -P
  
   52  find / -name ssh-keygen
   53  cd /usr/bin/
   57  ansible sdyy02 -m ping


   59  ansible 192.168.7.7 -m ping
   60  ssh-keygen -t rsa
   61  cd $HOME/.ssh
   63  ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.7.7
   64  ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.7.8
   65  vi /etc/ansible/ansible.cfg 
   66  ansible all -m ping
2)控制机安装Python winrm模块
tar zxvf pywinrm-0.2.2.tar.gz 
  211  tar -zxvf isodate-0.6.0.tar.gz 
  212  tar -zxvf xmlwitch-0.3.tar.gz 
  213  cd pywinrm-0.2.2/
  214  ls
  215  python setup.py install
  216  python
  217  ansible windows -m win_ping
Ansible使用命令
群组名称:sdyy
批量传输文件并备份: ansible sdyy -s -m copy -a 'src=/home/master/upload/list.jsp dest=目的路径 owner=webadmin group=webadmin backup=yes'
批量删除 :ansible sdyy -m shell -a "rm -rf /opt/cs.txt"
批量启动tomcat: ansible sdyy -m shell -a " nohup /opt/tomcat1.sh start owner=webadmin group=webadmin"   
批量关闭tomcat: ansible sdyy -m shell -a "/opt/tomcat1.sh stop owner=webadmin group=webadmin"


windows:
ping :ansible windows -m win_ping
拷贝:ansible windows -m win_copy -a 'src=/opt/cs.txt dest=D:\'
删除:ansible windows -m win_file -a "path=D:\cs.txt state=absent"


Windows配置:
和Linux发版版稍有区别,远程主机系统如为Windows需预先如下配置:
安装Framework 3.0+
更改powershell策略为remotesigned
升级PowerShell至3.0+
设置Windows远端管理,英文全称WS-Management(WinRM)
(1)安装Framework 3.0+
下载链接为:http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe。 下载至本地后双击左键安装即可,期间可能会多次重启,电脑需正常连接Internet。
(2)更改powershell策略为remotesigned
    set-executionpolicy remotesigned
(3)升级PowerShell至3.0+
Window 7和Windows Server 2008 R2默认安装的有PowerShell,但版本号一般为2.0版本,所以我们需升级至3.0+,如下图中数字1部分表示PowerShell版本过低需3.0+版本,数字2部分表示当前PowerShell版本为2.0。
 
(4)设置Windows远端管理(WS-Management,WinRM)
winrm service 默认都是未启用的状态,先查看状态;如无返回信息,则是没有启动;
winrm enumerate winrm/config/listener
针对winrm service 进行基础配置:
winrm quickconfig
查看winrm service listener:
winrm e winrm/config/listener
为winrm service 配置auth:
winrm set winrm/config/service/auth @{Basic="true"}
为winrm service 配置加密方式为允许非加密:
winrm set winrm/config/service @{AllowUnencrypted="true"}


ansible windows -m win_ping




错误说明
 
pip list
  229  ansible windows -m win_ping
  230  sudo pip uninstall request
  231  sudo pip uninstall requests
  232  pip install requests
  233  pip uninstall docopt
  234  sudo pip uninstall docopt
  235  sudo pip install docopt
  236  ansible windows -m win_ping
 
修改ansible的windows连接方式
vi /etc/ansible/hosts
[sdyy]
192.168.7.7
192.168.7.8
[windows]
192.168.7.11 ansible_ssh_user="Administrator" ansible_ssh_pass="SDyykj123" ansible_ssh_port=5985 ansible_connection=winrm











要使用 Ansible 搭建商城,需要先了解商城的基本架构和组成部分。一般来说,一个商城包括前端、后端、数据库、缓存、负载均衡等组件。在 Ansible 中,可以通过编写 Playbook 来自动化部署这些组件。 下面是一个简单的 Ansible Playbook 示例,可以用于搭建一个基本的商城环境: ```yaml - hosts: web_servers become: yes vars: app_name: my_shop db_name: my_shop_db db_user: my_shop_user db_password: my_shop_password redis_port: 6379 tasks: - name: Install required packages apt: name: "{{ item }}" state: present with_items: - nginx - python-pip - python-dev - build-essential - libmysqlclient-dev - redis-server - name: Configure nginx template: src: nginx.conf.j2 dest: /etc/nginx/sites-available/{{ app_name }} notify: restart nginx - name: Create MySQL database mysql_db: name: "{{ db_name }}" state: present - name: Create MySQL user mysql_user: name: "{{ db_user }}" password: "{{ db_password }}" priv: "{{ db_name }}.*:ALL" state: present - name: Configure Django settings template: src: django_settings.py.j2 dest: /opt/{{ app_name }}/{{ app_name }}/settings.py - name: Install Python requirements pip: requirements: /opt/{{ app_name }}/requirements.txt - name: Start Gunicorn command: /opt/{{ app_name }}/venv/bin/gunicorn {{ app_name }}.wsgi:application --bind 127.0.0.1:8000 --workers 3 async: true - name: Configure Redis lineinfile: path: /etc/redis/redis.conf regexp: "^port" line: "port {{ redis_port }}" notify: restart redis - hosts: db_servers become: yes vars: db_name: my_shop_db db_user: my_shop_user db_password: my_shop_password tasks: - name: Install MySQL server apt: name: mysql-server state: present - name: Configure MySQL template: src: my.cnf.j2 dest: /etc/mysql/my.cnf notify: restart mysql - name: Create MySQL database mysql_db: name: "{{ db_name }}" state: present - name: Create MySQL user mysql_user: name: "{{ db_user }}" password: "{{ db_password }}" priv: "{{ db_name }}.*:ALL" state: present ``` 在这个 Playbook 中,我们假设有两台服务器,一台用于部署 Web 服务器,另一台用于部署数据库服务器。首先,我们安装了需要的软件包,包括 Nginx、Python、MySQL 等。然后,我们配置了 Nginx 和 Django 的相关设置。接着,我们创建了 MySQL 数据库和用户,并将相关信息写入 Django 的配置文件中。最后,我们启动了 Gunicorn 应用服务器和 Redis 缓存服务器。 注意,在这个 Playbook 中,我们使用了一些模板文件,例如 nginx.conf.j2、django_settings.py.j2 和 my.cnf.j2。这些模板文件可以根据实际情况进行修改,以适应不同的商城环境。 当然,这只是一个简单的示例,实际的商城环境可能更加复杂。但是,通过 Ansible 的自动化部署能力,我们可以大大简化部署过程,减少出错的可能性,提高部署的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值