设为首页 加入收藏

TOP

ansible Ad-Hoc YAML剧本(一)
2023-08-26 21:10:21 】 浏览:75
Tags:ansible Ad-Hoc YAML 剧本

ansible、Ad-Hoc、YAML剧本

1.简介

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

2.部署

1.dns resolve

ansible服务器:192.168.70.42 配置阿里yum源
ansible客户机:192.168.70.35 192.168.70.36 配置阿里yum源

服务器和客户机都做好域名解析

vim /etc/hosts
192.168.70.35 host1
192.168.70.36 host2
192.168.70.42 ansible

2.install ansible (ansible服务器)

yum install ansible -y

3.ssh-key免密连接

ssh-keygen   //一路回车 有yes填yes

ssh-copy-id root@192.168.70.35	//发送公钥
ssh-copy-id root@192.168.70.36

ssh root@192.168.70.35   //测试连接
ssh root@192.168.70.36

4.ansible基础

1.自定义主机清单 (ansible机器)

vim /etc/ansible/hosts
host1
host2

2.测试连通性

ansible localhost -m ping  //测试host1连通性 -m指定模块

ansible host1 -m ping o  //一行简简洁输出

3.know_hosts

ansible host2 -m ping -u root -k -o //没有配置ssh免密可以交互式密码验证登录 如果是第一次连接会报错 需使用ansible host2 -m ping 交互yes之后才能

`去掉(yes/no)的询问`
vim /etc/ssh/ssh_config
StrictHostKeyChecking no
systemctl restart sshd

ansible host2 -m ping -u root -k -o //再次测试

4.请注意ping和ssh

ping的通不代表ansible连接的上,ping ICMP:网际消息管理协议
ansible的ping事探测ssh程序是否连接。不是icmp协议,如果关比sshd服务,ping可以成功,ansible测试会依旧失败。

5.Inventory主机清单

1.增加主机组

vim /etc/ansible/hosts
[webserver]
host1 ansible_ssh_user='root' 
host2 ansible_ssh_user='root'

ansible webserver -m ping -o

2.增加用户名没密码(没设置免密登录,可以增加用户名密码)

vim /etc/ansible/hosts
host1 ansible_ssh_user='root' ansible_ssh_pass='123456'
host2 ansible_ssh_user='root' ansible_ssh_pass='123456'

//或者
host[1:2] ansible_ssh_user='root' ansible_ssh_pass='123456'

3.增加端口(host)

vim /etc/ssh/sshd_config
Port 22 改成Port 2222
systemctl restart sshd

测试连接
ansible host1 -m ping  //会发现失败 ansible默认sshd端口还是22

回到ansible 修改用户host的端口
vim /etc/ansible/hosts
host1 ansible_ssh_user='root' ansible_ssh_pass='123456' ansible_ssh_port='2222'  //指定ssh端口

再次测试
ansible host1 -m ping

4.组:变量

[webserver]
host1 ansible_ssh_port='2222'
host2 ansible_ssh_port='2222'

[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123456'

5.子分组 (将不同的分组进行组合) 为了实验方便我们先把host的sshd 端口改回默认

vim /etc/ansible/hosts
[apache]
host1
[nginx]
host2
[webserver:children]
apache
nginx

ansible webserver -m ping -o  //测试

6.自定义主机列表

vim hostlist
[dockers]
host1
host2

ansible -i hostlist dockers -m ping -o  //测试

6.Ad-Hoc点对点模式

临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。

1.shell模块

ansible webserver -m shell -a 'hostname' -o  //查看主机名称

ansible webserver -m shell -a 'yum install mariadb -y' -o  //安装mariadb

ansible webserver -m shell -a 'touch /tmp/777.txt' -o  //创建文件

2.复制模块

ansible-doc copy  //查看帮助文档

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt'  //copy 如果和原先文件一样 不会进行拷贝

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/3.txt owner=root group=root mode=777'

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/3.txt owner=root group=root mode=777 backup=yes'

3.用户模块

ansible-doc user  //查看帮助文档

ansible webserver -m user -a 'name=xux state=present'  //创建用户

echo '123456' | openssl passwd -1 -stdin  //系统存储的是密文 需要将输入的密码转化为加密后的密文
ansible webserver -m user -a 'name=xux password="$1$g93P3CoY$YPuV8anNPa8HBhnfMncB60"'  //修改密码

ansible webserver -m user -a 'name=xux shell=/sbin/nologin append=yes'  //修改用户属性

ansible webserver -m user -a 'name=xux state=absent'  //删除用户

4.软件包管理

ansible-doc yum  //查看帮助文档
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇linux环境编程(1): 实现一个单元.. 下一篇SELinux 入门 pt.1

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目