Ansible

安装:

  • pip3 install ansible -i https://pypi.douban.com/simple

查看 ansible 的版本:

  • ansible --version

创建配置文件

  • mkdir /etc/ansible
  • cd /etc/ansible
  • touch ansible.cfg
  • touch hosts

设置配置文件默认查找位置

  • export ANSIBLE_CONFIG=/etc/ansible/ansible.cfg
  • env   通过命令查看

编辑配置文件以及资产清单

  • vim /etc/ansible/ansible.cfg
  • vim /etc/ansible/hosts

测试查看

  • ansible all --list-hosts
  • ansible localhost_test --list-hosts

Ansible ad-hoc模式

Ansible ad-hoc模式常用模块命令参数 —— https://www.cnblogs.com/keerya/p/7987886.html#_label3_0

ad-hoc模块案例练习

需求1: 主机连通性测试(ping模块)

  • ansible local -m ping

需求2: 查看主机root用户的详细信息(shell模块)

  • ansible local -m shell -a 'cat /etc/passwd |grep "root"'

需求3: 备份主机的日志文件/var/log/messages到/mnt/目录(copy模块)

  • ansible local -m copy -a 'src=/var/log/messages dest=/mnt/'

需求4: 给定文件内容"hello ansible"生成文件/mnt/ansible,并指定权限为666(copy模块)

  • ansible local -m copy -a 'content="hello ansible\n" dest=/mnt/ansible mode=666'

需求5: 主机批量安装软件gcc和nginx(yum模块)

  • ansible local -m yum -a 'name=gcc state=present'
  • ansible local -m yum -a 'name=nginx state=present'

需求6: 启动nginx服务(service模块)和判断nginx的80端口是否打开(shell模块-ss -ntl)

  • ansible local -m service -a 'name=nginx state=started'
  • ansible local -m shell -a 'ss -ntl'

需求7: 主机新建uid=8888的用户user1

  • ansible local -m user -a 'name=user1 uid=8888'

需求8: 拉取Github项目项目到主机并部署(git模块)


需求9:搜集主机信息并筛选与内存Memory相关的信息(setup模块)

  • ansible local -m setup -a ' filter="*mem*" '

 Ansible Playbook模式

同步: 所有的操作都做完,才返回给用户。
异步: 将用户请求放入消息队列,当请求执行完成之后自动通知用户

批量部署安装httpd,并设置开机启动

  • mkdir /root/Desktop/playbook.yml/template/              # 创建模板文件,用于存储批量管理的配置模板文件
  • cp /etc/httpd/conf/httpd.conf  template/
  • cd template/
  • vim httpd.conf                                                                # 将配置文件中需要更改的部分内容 ,设置为参数,便于在脚本中导入

  • vim index.html                                                               # 编辑http访问的首页

测试(管理的主机组【test】):

Ansible + Django 的项目

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值