ansible模块

3.练习模块
3.1 ansible-doc模块
- 模块的手册相当与shell的man,很重要

- ansible-doc -l  	#列出所有模块
- ansible-doc modulename	#查看帮助

3.2 ping模块
- 测试网络连通行,ping模块没有参数
- 注:测试ssh的连通性

[root@ansible ~]# ansible all -m ping

3.3 command模块

[root@ansible ~]# ansible all -m command -a 'date "+%F %T"'
[root@ansible ~]# ansible web -m command -a 'uptime'

3.4 shell模块

[root@ansible ~]# ansible all -m shell -a 'ps -ef  | grep sshd'
[root@ansible ~]# ansible web2 -m shell -a "echo ${HOSTNAME}"
web2 | SUCCESS | rc=0 >>
ansible
[root@ansible ~]# ansible web2 -m shell -a 'echo ${HOSTNAME}'
web2 | SUCCESS | rc=0 >>
web2
[root@ansible ~]# ansible web2 -m shell -a 'echo \${HOSTNAME}'
web2 | SUCCESS | rc=0 >>
${HOSTNAME}
[root@ansible ~]# ansible web1,db2 -m shell -a 'useradd nb'
[root@ansible ~]# ansible web1,db2 -m shell -a 'echo 123 | passwd --stdin nb'	

3.5 script模块
- 命令太复杂
- 在本地写脚本,然后使用script模块批量执行

[root@ansible ~]# vim a.sh

#/bin/bash
id nb
if [ $? != 0 ];then
useradd wk
kaolecho 456 | passwd --stdin wk
fi

[root@ansible ~]#  ansible web -m script -a a.sh

3.6 yum模块
使用yum包管理器来管理软件包
name:要进行操作的软件包名字
state:动作(installed,removed)

[root@ansible ~]# ansible db -m yum -a 'state=installed name=mariadb-server'
[root@ansible ~]# ansible cache -m yum -a 'state=removed name=lrzsz'

3.7 service模块
- name:必选项,服务名称
- enabled:是否开机启动yes|no
- sleep:执行restarted,会在stop和start之间沉睡几秒钟
- state:对当前服务执行启动,停止,重启,重新加载等操作(started,stopped,restarted,reloaded)

[root@ansible ~]#   ansible db -m service -a 'name=mariadb state=started enabled=yes'

3.8 copy模块

[root@ansible ~]#  cp /etc/resolv.conf ./
[root@ansible ~]#  vim resolv.conf 
[root@ansible ~]# ansible all -m shell -a 'cat /etc/resolv.conf'
[root@ansible ~]# ansible all -m copy -a 'src=resolv.conf dest=/etc/resolv.conf' 
[root@ansible ~]# ansible all -m shell -a 'cat /etc/resolv.conf'

练习

  • 给所有db主机开启binlog日志

  • log_bin=mysql-bin

  • binlog-format=mixed

    [root@ansible ~]# ansible db -m yum -a 'name=mariadb-server state=removed'
    [root@ansible ~]# ansible db -m yum -a 'name=mariadb-server state=installed'
    
    [root@db1 ~]# scp /etc/my.cnf root@192.168.1.40:/etc/my.cnf
    
    [root@ansible ~]# vim /etc/my.cnf
    [mysqld]
    log_bin=mysql-bin
    binlog-format=mixed
    ...
    :wq
    
    [root@ansible ~]# ansible db -m copy -a 'src=/etc/my.cnf dest=/etc/my.cnf'
    [root@ansible ~]# ansible db -m service -a 'name=mariadb state=started enabled=yes'
    
    [root@db1 ~]# vim /etc/my.cnf
    [mysqld]
    log_bin=mysql-bin
    binlog-format=mixed
    :wq
    [root@db1 ~]# netstat -ntlup | grep :3306
    

3.9 lineinfile模块
- 类似sed的一种行编辑替换模块
- path 目标文件文件
- regexp 正则表达式,要修改的行
- line 最终修改的结果

  • 例如修改my.cnf,中binlog-format的格式
  • mixed–>row
[root@ansible ~]# ansible db -m lineinfile -a 'path=/etc/my.cnf regexp="mixed$" line="binlog-format=row"'

3.10 replace模块
- 类似sed的一种行编辑替换模块

  • path 目标文件文件

  • regexp 正则表达式

  • replace 替换后的结果

  • 替换指定字符 row—> mined

[root@ansible ~]# ansible db -m shell -a 'grep "^binlog-" /etc/my.cnf '
[root@ansible ~]# ansible db -m replace -a 'path=/etc/my.cnf regexp="row" replace="mixed"'

3.11 setup模块
- 主要用户用于获取主机信息,playbooks里经常会用的另一个参数gather_facts与该模块相关,setup模块下经常用的是filter参数
- filter过滤所需信息

[root@ansible ~]# ansible cache -m setup -a 'filter=ansible_distribution'
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值