Linux-关于ansible(一)

本文介绍了Ansible作为自动化运维工具的基础使用,包括通过SSH连接远程主机、拷贝模块、脚本执行、文件管理以及常用模块如command、shell、file、copy、lineinfile等的使用。重点讲解了各模块的特点和应用场景,如command与shell模块的区别,以及如何实现幂等性操作。
摘要由CSDN通过智能技术生成

模块(py脚本)

管理端-------------------ssh--------------被管理端
(海量脚本:2800+)
1.ansible ssh 远程被管理端主机
2.拷贝模块到被管理端主机
3.再被管理端主机执行脚本
4.执行完后,删除脚本,断开连接

案例一:
做域名解析

[root@control ~]# vim /etc/hosts

生成密钥

[root@control ~]# ssh-keygen

配置密钥(远程别人不需要密码)

[root@control ~]#for i in node1 node2 node3 node4 node5
> do 
> ssh-copy-id $i
> done

放入软件包

[root@control ~]# tar -xf ansible_soft.tar.gz 
anaconda-ks.cfg  ansible_soft  ansible_soft.tar.gz
[root@control ~]# cd  ansible_soft/
[root@control ansible_soft]# ls
[root@control ansible_soft]# dnf -y install *

[root@control ansible_soft]# ansible --version


[root@control ~]# mkdir ~/ansible
[root@control ~]# vim ansible/ansible.cfg
[defaults]
inventory = ~/ansible/inventory
#forks = 5                 #ask_pass = True                             
#host_key_checking = False


[root@control ~]# vim ansible/inventory

[test]
node1
[proxy]
node2
[webserver]
node[3:4]
[database]
node5
[cluster:children]
webserver
database     

查看主机列表

[root@control ~]# cd ansible
[root@control ansible]# ansible all --list-hosts    进入到ansible目录输入命令

测试远程主机是否能pIng通

[root@control ansible]# ansible webserver -m ping

·模块
多数脚本都支持模块,默认模块为command

[root@control ansible]# ansible node1 -m command -a "uptime"

**不加command也行,因为是默认模块**
[root@control ansible]# ansible-doc -l | grep yum
[root@control ansible]# ansible-doc -l 
[root@control ansible]# ansible-doc yum

command和shell模块的区别,command模块的命令不启动shell,直接通过ssh执行命令,command不支持bash的特性,如管道和重定向等功能,所有需要调用shell的功能都无法使用。不可以使用shell模块执行交互命令,如vim、top等。
shell模块

[root@control ansible]# ansible node1 -m shell -a "who"
[root@control ansible]# ansible node1 -m shell -a "ps aux | wc -l"
[root@control ansible]# ansible node1 -m shell -a "touch 1.txt"

使用shell模块创建文件有警告是正常的

creates:如果存在就不再执行命令
removes:如果不存在就不执行

[root@control ansible]# ansible test -m shell -a "touch 1.txt creates=1.txt"
[root@control ansible]# ansible test -m shell -a "unzip xx.zip removes=/bin/unzip"

script模块

[root@control an
sible]# cat test.sh 
#!/bin/bash
dnf -y install httpd
systemctl start httpd
[root@control ansible]# ansible test -m script -a "./test.sh"
#test是主机组的名称,-m调用script模块

幂等性:任意次执行所产生的影响均与一次执行的影响相同
file模块
file模块可以创建文件、目录、链接;修改权限与属性。

[root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=touch"

state=touch 创建文件   state=directory   创建目录
[root@control ansible]# ansible test -m file -a "path=/zjx state=directory" 


[root@control ansible]# ansible test -m file -a "path=/tmp/file.txt owner=sshd group=adm mode=0777"

[root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=absent"  
[root@control ansible]# ansible test -m file -a "path=/zjx state=absent"
absent为删除选项

[root@control ansible]# ansible test -m file -a "src=/etc/hosts path=/etc/host.txt state=link"
link为创建软连接

copy模块

[root@control ansible]# ansible test -m copy -a "src=/a3.txt dest=/root/"
拷贝本机文件到指定机器

[root@control ansible]# ansible test -m copy -a "content='hellow\n' dest=/root/new.txt"
直接写入文件内容

fatch模块
与copy模块相反,拷贝其他主机文件拷贝到本地,如果要下载目录,可以先打包后再下载

[root@control ansible]# ansible all -m fetch -a "src=/etc/hostname dest=~/"

lineinfile模块

[root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='hello world'"


[root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue line='insert' insertafter=Kernel"


[root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue regexp='hello' line=nihao"

[root@control ansible]# ansible test -m lineinfile -a "path=/etc/issue.net line='sert' insertbefore='Ocean'"

replace模块

[root@control ansible]# ansible test -m replace -a "path=/etc/issue.net regexp=Kernel replace=Ocean"

user模块

[root@control ansible]# ansible all -m user -a "name=tuser2 uid=1010 group=adm groups=daemon,root home=/home/tuser2"


[root@control ansible]# ansible test -m user -a "name=tuser3 password={{'abc' | password_hash('sha512')}}"

[root@control ansible]# ansible test -m user -a "name=tuser2 groups=root"

[root@control ansible]# ansible test -m user -a "name=tuser1 state=absent"


[root@control ansible]# ansible test -m user -a "name=tuser2 state=absent remove=true"
#删除tuser2账户同时删除家目录、邮箱

yum_repository模块

[root@control ansible]# ansible test -m yum_repository -a "name=myyum description=hello baseurl=ftp://192.168.4.254/centos gpgcheck=no"

[root@control ansible]# ansible test -m yum_repository -a "name=myyum state=absent"

yum模块

state: present(安装)|absent(卸载)|latest(升级)。
[root@control ansible]# ansible test -m yum  -a "name=unzip state=present"

[root@control ansible]# ansible test -m yum -a "name=unzip state=absent"

service模块

[root@control ansible]# ansible test -m service -a "name=httpd state=started"

lvg模块

[root@control ansible]# ansible test -m yum -a "name=lvm2"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值