ansible搭建+ansible常用模块

ansible搭建

管理机安装ansible,被管理节点必须打开ssh服务

1.管理机安装ansible

yum -y install ansible

2.查看版本

ansible --version
ansible 2.9.27

3.查找配置文件

find /etc/ -name "*ansible*"
/etc/ansible
/etc/ansible/ansible.cfg

4.三台被管理机(192.168.118.20,192.168.118.30,192.168.118.200),其中两台做免密

ssh-keygen
ls ./.ssh/
id_rsa  id_rsa.pub  known_hosts
ssh-copy-id -i 192.168.118.20
ssh-copy-id -i 192.168.118.30

5.定义主机组

vim /etc/ansible/hosts 

6.  ansible   主机ip|域名|组名|别名    -m   ping|copy|....  '参数'

7.没有做免密登录的主机可以设置用户和密码

ansible常用模块

hostname模块:

修改主机名

ansible other -m hostname -a 'name=web'

[root@web01 ~]# hostname
web

file模块:

file模块⽤于对⽂件相关的操作(创建, 删除, 软硬链接等)

创建一个目录

ansible group01 -m file -a 'path=/tmp/abc state=directory'
创建一个文件

 ansible group02 -m file -a 'path=/tmp/abc/def state=touch'

递归修改owner,group,mode
 ansible group02 -m file -a 'path=/tmp/abc recurse=yes owner=bin group=daemon mode=1777'

 recurse表示递归

删除⽬录(连同⽬录⾥的所有⽂件)

ansible group02 -m file -a 'path=/tmp/abc state=absent'

创建⽂件并指定owner,group,mode等

ansible group02 -m file -a 'path=/tmp/abc state=touch owner=bin group=daemon mode=1777'

[root@web01 ~]# ls -l /tmp/
总用量 0
-rwxrwxrwt. 1 bin  daemon  0 8月  16 14:21 abc

软连接指向硬链接,硬链接指向文件

ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx2 state=hard'
ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx state=link'

lrwxrwxrwx. 1 root root  10 8月  16 14:29 xxx -> /etc/fstab
-rw-r--r--. 2 root root 647 5月  25 18:38 xxx2

path:文件的地址

state:方法-----directory(创建目录)touch(创建文件)absent(删除文件) link(创建软连接) hard(创建硬链接)

recurse:是否允许递归操作

copy模块:

copy模块⽤于对⽂件的远程拷⻉操作(如把本地的⽂件拷⻉到远程的机器上)

ansible group02 -m copy -a 'src=./tst dest=~'

注意:使⽤content参数直接往远程⽂件⾥写内容(会覆盖原内容)

使⽤force参数控制是否强制覆盖
     如果⽬标⽂件已经存在,则不覆盖   force=no
     如果⽬标⽂件已经存在,则会强制覆盖   force=yes
使⽤backup参数控制是否备份⽂件
backup=yes表示如果拷⻉的⽂件内容与原内容不⼀样,则会备份⼀份
copy模块拷⻉时要注意拷⻉⽬录后⾯是否带"/"符号
/etc/yum.repos.d后⾯不带/符号,则表示把/etc/yum.repos.d整个⽬录拷⻉到/tmp/⽬录下
/etc/yum.repos.d/后⾯带/符号,则表示把/etc/yum.repos.d/⽬录⾥的所有⽂件拷⻉到/tmp/⽬录下

fetch模块:

fetch模块与copy模块类似,但作⽤相反。⽤于把远程机器的⽂件拷⻉到本地。

把被管理机的内容收到管理机:相当于收作业

 ansible group02 -m fetch -a 'src=/etc/sysconfig/network-scripts/ifcfg-ens33 dest=/tmp'

[root@nat ~]# tree /tmp
/tmp
├── 192.168.118.20
│   └── etc
│       └── sysconfig
│           └── network-scripts
│               └── ifcfg-ens33
├── 192.168.118.30
│   └── etc
│       └── sysconfig
│           └── network-scripts
│               └── ifcfg-ens33
├── other
│   └── etc
│       └── sysconfig
│           └── network-scripts
│               └── ifcfg-ens33

 

user模块:

user模块⽤于管理⽤户账号和⽤户属性
创建aaa⽤户,默认为普通⽤户,创建家⽬录
ansible group01 -m user -a ‘name=aaa state=present’
创建bbb系统⽤户,并且登录shell环境为/sbin/nologin
ansible group01 -m user -a ‘name=bbb state=present system=yes shell="/sbin/nologin"’
创建ccc⽤户, 使⽤uid参数指定uid, 使⽤password参数传密码
ansible group01 -m user -a 'name=ccc uid=2000 state=present password="ccc"'
创建⼀个普通⽤户叫hadoop,并产⽣空密码密钥对
ansible group01 -m user -a 'name=hadoop generate_ssh_key=yes'
删除aaa⽤户,但家⽬录默认没有删除
ansible group01 -m user -a 'name=aaa state=absent'
删除bbb⽤户,使⽤remove=yes参数让其删除⽤户的同时也删除家⽬录
ansible group01 -m user -a 'name=bbb state=absent remove=yes'

yum模块:

yum模块⽤于使⽤yum命令来实现软件包的安装与卸载

state=latest表示安装最新版本

ansible other -m yum -a 'name=ntpdate state=present'   //下载

ansible other -m yum -a 'name=ntpdate state=absent'   //卸载

cron模块:

cron模块⽤于管理周期性时间任务

创建⼀个cron任务,不指定user的话,默认就是root,如果minute,hour,day,month,week不指定的话,默认都为*

ansible other -m cron -a 'name="abc" user=root job="usr/sbin/nptdate cn.ntp.org.cn" hour=2'
[root@web01 ~]# crontab -l
#Ansible: abc
* 2 * * * usr/sbin/nptdate cn.ntp.org.cn

删除cron任务
ansible other -m cron -a 'name="abc" state=absent'

servicec模块:

service模块⽤于控制服务的启动,关闭,开机⾃启动等
关闭防火墙,并设置为开机不自动启动

ansible other -m service -a 'name=firewalld state=stopped enabled=false'

启动防火墙,并设置为开机自动启动
ansible other -m service -a 'name=firewalld state=started enabled=on'

script模块:

script模块⽤于在远程机器上执⾏本地脚本
[root@m0 ~]# vim test000.sh
#!/bin/bash
mkdir /tmp/three
touch /tmp/three/test
echo 'i am echo,at mttt' > /tmp/three/test
echo 'well done'
[root@m0 ~]# source test000.sh 
well done
[root@m0 ~]# ansible group02 -m script -a './test000.sh'

# 验证

[root@s0 ~]# ls /tmp/
111                                                                      three

command模块:

ansible  group01 -m command -a "useradd user2"

  • 15
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值