ansible 自动化运维工具

 ansible的主机清单

1、 直接指明主机地址或主机名:
    # green.example.com
    # blue.example.com
    # 192.168.100.1
    # 192.168.100.10
2、 定义一个主机组[组名]把地址或主机名加进去
    [mysql_test]
    192.168.253.159
    192.168.253.160
    192.168.253.153
    192.168.153.[199:202]
    ansibel host --list-hosts  
[root@server ~]# vim /etc/ansible/hosts
[web]
192.168.37.122
192.168.37.133

命令格式

命令格式:
ansible <host-pattern> [-f forks] [-m module_name] [-a args]
ansible 主机清单中的主机组 -f 开启线程数 -m 模块 -a 模块需要的参数
ansible -i hosts web -m shell -a '' 

配置公钥

#1.生成私钥[root@server ~]# ssh-keygen 

#2.向主机分发私钥[root@server ~]# ssh-copy-id root@192.168.37.122

[root@server ~]# ssh-copy-id root@192.168.37.133 

报错就安装这个包yum -y install openssh-clients

主机通行

ansible -i ./hostfile web -m ping

ansible -i [主机清单] [组] -m ping

ansible的命令模块

shell

例如:

ansible -i ./hostfile web -m shell -a 'cat /etc/passwd | grep "root"'


copy
ansible -i ./hostfile web -m copy -a 'src=~/hello dest=/data/hello'

file
 ansible -i ./hostfile web -m file -a 'path=/data/app state=directory'
fetch
ansible web -m fetch -a 'src=/data/hello dest=/data' 
cron
ansible -i ./hostfile web -m cron -a 'name="ntp update every 5 min" 
yum
ansible -i./hostfile web -m yum -a 'name=htop state=present'
service
ansible -i ./hostfile web -m service -a 'name=nginx state=started 
grup

 ansible -i ./hostfile  web -m group -a 'name=sanguo gid=12222'
script
ansible -i ./hostfile  web -m script -a '/tmp/df.sh'
get_url
ansible -i /etc/ansible/hosts  zabbix -m get_url -a "url=ftp://10.3.131.50/soft/wechat.py dest=/tmp"
unarchive
ansible -i /etc/ansible/hosts web -m unarchive -a 'src=/root/easy-springmvc-maven.zip dest=/tmp'

playbook剧本

定义

[root@server ~]# cd /etc/ansible
[root@server ansible]# vim nginx.yml
---
- hosts: web
  remote_user: root
  tasks:
    - name: install nginx
      yum: name=nginx state=present
    - name: copy nginx.conf
      copy: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf backup=yes
      notify: reload    #当nginx.conf发生改变时,通知给相应的handlers
      tags: reloadnginx   #打标签
    - name: start nginx service
      service: name=nginx state=started
      tags: startnginx   #打标签

  handlers:  #注意,前面没有-,是两个空格
    - name: reload
      service: name=nginx state=restarted  #为了在进程中能看出来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值