6.ansible 基础使用

ansible 介绍

Ansible是一种常用的自动运维化工具,基于python开发,分布式,无需客户端,轻量级,配置语言采用YAML

Ansible的安装

1.yum安装:

# EPEL源:
yum -y install epel-release
yum install ansible -y

ansible 配置

配置文件位置 /etc/ansible/ansible.cfg
主机组存放位置 /etc/ansible/hosts

ansible 命令

标准命令格式
ansible 【主机组】 -m 【执行动作】 【动作参数】
动作

动作说明参数
pingping
copy拷贝源文件位置|目标文件位置
command执行命令需要执行的命令

简单使用ansible daemon

  1. 添加一个主机组
cat  >> /etc/ansible/hosts << EOF 
[test]
192.168.1.1
192.168.1.2:222 ansible_ssh_user=root ansible_ssh_pass=xxx
EOF
  1. 执行批量 ping 命令 (需要提前做好免密)
ansible test -m ping

192.168.1.1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
  1. 批量上传
ansible test -m copy -a "src=/etc/hosts dest=/root owner=root group=root mode=0777"

  1. 批量执行命令
ansible test -m command -a "ifconfig"

10.0.8.3 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.8.3  netmask 255.255.252.0  broadcast 10.0.11.255
        inet6 fe80::5054:ff:feac:5151  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:ac:51:51  txqueuelen 1000  (Ethernet)
        RX packets 153156039  bytes 64216675128 (59.8 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 118293423  bytes 16106180525 (15.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
br-508fef7e774d: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.21.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:50ff:fea2:315a  prefixlen 64  scopeid 0x20<link>
        ether 02:42:50:a2:31:5a  txqueuelen 0  (Ethernet)
        RX packets 174850994  bytes 15208890608 (14.1 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 174850994  bytes 15208890608 (14.1 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ansible Playbook

多个任务放置在一起,一块执行

编辑一个 ansible剧本并执行 实现批量拷贝一个脚本 并执行


echo 'echo `date`' > /root/tset.sh

vim /root/test.yml
---                       #固定格式
- hosts: test             #定义需要执行主机
  remote_user: root       #远程用户
  vars:                   #定义变量
    xxx: xxx       #变量
  tasks:
  - name: copy shell file
    copy: src=/root/test.sh dest=/tmp/test.sh    # 拷贝文件
  - name: exec shell file
    shell: sh /tmp/test.sh {{xxx}}  # 执行文件
    register: shell_result
  - debug: var=shell_result.stdout_lines
  - name: rm shell file
    shell: rm /tmp/test.sh  # 删除文件


ansible-playbook -i /etc/ansible/hosts /root/tset.yml
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值