- 安装
查看该目录/etc/yum.repos.d下是否存在 Centos-7.repo, epel.repo 如果没有,安装base源:curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 然后执行以下命令进行安装: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum install ansible -y
- 功能体验
1. 克隆虚拟机并启动,修改配置 修改主机名:hostnamectl set-hostname 主机名 修改:cd /etc/sysconfig/network-scripts/ifcfg-eth0 删除UUID一行 IPADDR=100.0.0.200 修改 /etc/hosts: 10.0.0.200 主机名 重启网卡: systemctl restart network 2. 连接: 生成密钥并推送: ssh-keygen ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.200 测试: ssh 10.0.0.200 date 显示时间 配置主机的清单: vim /etc/ansible/hosts [web] 10.0.0.100 10.0.0.200 给自己发送密钥: ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.100 ping测试: ansible web -m ping 显示绿色 ansible all -m ping(测试所有机器) ansible all -m shell -a 'df -h'(可查看磁盘空间) 3. ansible playbook自动化安装nginx: a: vim /opt/playbook_nginx.yml # 要严格按照格式编写 - hosts: web remote_user: root vars: http_port: 80 tasks: - name: Add Nginx Yum Repository yum_repository: name: nginx description: Nginx Repository baseurl: http://nginx.org/packages/centos/7/$basearch/ gpgcheck: no - name: Install Nginx Server yum: name=nginx state=present - name: Configure Nginx Server template: src=./default.conf.template dest=/etc/nginx/conf.d/default.conf notify: Restart Nginx Server - name: Start Nginx Server service: name=nginx state=started enabled=yes handlers: - name: Restart Nginx Server service: name=nginx state=restarted b: vim /opt/default.conf.template server { listen {{ http_port }}; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } } 4. 检查语法: ansible-playbook --syntax playbook_nginx.ym 5. 模拟执行: ansible-playbook -C playbook_nginx.yml 6. 执行: ansible-playbook playbook_nginx.yml