Ansible 笔记,安装+模块介绍

本文详细介绍了使用Ansible进行自动化运维的全过程,包括从源码和YUM安装Ansible,配置主机列表,实现免密码登录,以及通过Ansible模块如copy、file、cron等进行远程文件操作、服务管理、计划任务设定等高级应用。

从源码安装的步骤

https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

$ git clone git://github.com/ansible/ansible.git --recursive

$ cd ./ansible

source ./hacking/env-setup

82ad61ab7a624d8b9e7a880562966ecf

yum install -y epel-release wget

yum install -y python-setuptools

sudo easy_install pip

pip install paramiko PyYAML Jinja2 httplib2 six

9908b9633b42451d86d489352ac9e428

验证下文件

git pull --rebase

git submodule update --init --recursive

从yum安装

yum install -y epel

yum install -y ansible

我是先看到二进制源码安装的,没想到yum这么粗暴,如果您跟着我的笔记做过源代码安装,那么恭喜你,你玩过2种安装方式鸟

host中添加本机,然后测试下

echo "127.0.0.1" > ~/ansible_hosts

export ANSIBLE_HOSTS=~/ansible_hosts

建立主机列表

cat << EOF > /etc/ansible/hosts

[k8s]

192.168.10.151

192.168.10.152

192.168.10.153

192.168.10.154

EOF

免密码登录

ssh-keygen

ssh-copy-id root@192.168.10.151

1c7f6856d7a24c88a55dd73ba264ae7f

测试下主机连接

ansible -m ping all

cbc82909c47c47ecb72d986c48837779

ansible常用的13个模块

copy模块

file模块

cron模块

group模块

user模块

yum模块

service模块

script模块

ping模块

command模块

raw模块

get_url模块

synchronize模块

copy模块:

把主控端/root目录下的a.sh文件拷贝到到指定节点上

ansible 192.168.10.151 -m copy -a 'src=/usr/a.txt dest=/tmp/'

7ab9889633a440d880c73b57268a0ed2

70ecfdf38822405c9fd52df4585b0c73

file模块

更改指定节点上/tmp/t.sh的权限为755,属主和属组为root

ansible all -m file -a "dest=/tmp/a.txt mode=755 owner=root group=root"

d46d1a3e41a44da09fcf49e6e5afd4da

因为只有192.168.10.151有这个文件

ansible k8s -m command -a 'touch /usr/a.txt'

038e48b8ddfb4c49bc976da9c2e1dcf7

4804fa4b780b47588d7ff29b0842e590

都改了权限了

yum模块:

在指定节点上安装 ntp服务

ansible all -m yum -a "state=present name=ntp"

d075bedbe0db46caab2802ca81d49775

bffcff8c60b04424a7274f49f118c074

批量同步时间

ansible k8s -m command -a 'ntpdate 0.asia.pool.ntp.org'

55820f3e040a4e9b8e69e8327a28c530

cron模块:

在指定节点上定义一个计划任务,每隔12小时到主控端更新一次时间

ansible all -m cron -a 'name="NTP job" minute=* hour=*/12 day=* month=* weekday=* job="/usr/sbin/ntpdate 133.243.238.243"'

1a3ca9cb84e3477798d9246247fcec0f

4台主机都有了

9391967626da4acfb4684d2b86f85274

8652376d4ade448a8ebfac8f870caeee

1e72b8fd24994d3eb96e00c1c9fd6305

7dccaef600e244ffb498894188294576

group模块:

在所有节点上创建一个组名为nolinux,gid为2014的组

ansible all -m group -a 'gid=2014 name=nolinux'

2563873bee204fb3babc9cf041dc420b

user模块:

在指定节点上创建一个用户名为nolinux,组为nolinux的用户

ansible all -m user -a 'name=nolinux groups=nolinux state=present'

d33a18bb3bb7437dbc619538350f5249

用户删除

ansible all -m user -a 'name=nolinux groups=nolinux state=absent remove=yes'

a2d6a3dccc79473f8e06b349b7746b02

service模块:

启动指定节点上的 puppet 服务,并让其开机自启动

ansible all -m service -a 'name=ntpdate state=restarted enabled=yes'

f9fd787118da40128079e580dea59826

script模块:

在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的)

ansible all -m script -a '/root/a.sh'

command模块:

在指定节点上运行hostname命令

ansible all -m command -a 'hostname'

7d9b347e25674d84858ef5f733cb7833

get_url模块:

将http://1.1.1.1/favicon.ico文件下载到指定节点的/tmp目录下

ansible 1.1.1.1 -m get_url -a 'url=http://1.1.1.1/favicon.ico dest=/tmp'

sync模块

https://docs.ansible.com/ansible/latest/modules/synchronize_module.html

先确定有没有安装rsync

ansible all -m yum -a "state=present name=rsync"

ansible all -m synchronize -a 'src=/usr/b.txt dest=/tmp/ compress=yes'

ansible all -m command -a 'ls /tmp/b.txt'

3a91b4cbc251455e89b8463a3950b071

同步2个目录在多个主机中

touch /tmp/3.txt

ansible all -m synchronize -a 'src=/tmp/ dest=/tmp/ compress=yes'

可以看到其他主机中3.txt已经同步过去了

09ee7194993b4b038752fecc69123331

pip install --upgrade pyls https://pypi.python.org/simple

sudo easy_install pip

ansible all -m ping --ask-pass

设置dns

cat << EOF >> /etc/hosts

192.168.10.151 K8S-M1

192.168.10.152 K8S-M2

192.168.10.153 K8S-M3

192.168.10.154 K8S-M4

EOF

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值