Ansible之常用模块介绍

环境

ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS -C -f forks
ssh-keygen -t rsa -P ""
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.51
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.52
cat /etc/ansible/hosts
[webservers]
10.0.0.51
10.0.0.52

[dbservers]
10.0.0.51

1.ping

# 检查指定节点机器是否还能连通,用法很简单,不涉及参数
-C, --check:不作任何改变,只是干跑一遍命令
-a MODULE_ARGS
ansible all -m ping --list-hosts

2.user & group

使用模块时,帮助信息中写等号的,是必须得手动写明,其它的都是可选的,有默认值
present是创建,absent是删除
ansible-doc -s group
ansible -m group -a "gid=3000 name=testgrp state=present system=no"
ansible -m user -a "uid=5000 name=testuser state=present group=testgrp shell=/bin/tcsh"

3.copy

  – src:要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径,如果路径使用”/”来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync.

ansible all -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=644'
ansible all -m copy -a "content='hi ansible
' dest=/tmp/hello.txt"
fetch - name: Fetches a file from remote nodes 从远程节点获取文件
ansible dbservers -m fetch -a "src='/root/hi.txt' dest='/root'"

4.command

# 不是键值对,直接给出命令即可
ansible all -m command -a 'date'
# 如果不加-m模块,默认运行command模块
ansible all -a 'ls /'
# command无法给用户生成密码,只能把命令当字符串echo出来
ansible all -m command -a "echo mowang|passwd --stdin testuser"

5.shell

在远程主机上调用shell解释器执行命令,支持shell的各种功能

ansible all -m shell -a "echo mowang|passwd --stdin testuser"

6.file

该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等
ansible all -m file -a "path=/var/tmp/hello stste=directory"
ansible all -m file -a "path=/var/tmp/fstab path=/var/tmp/fstab.link stste=link"

7.cron

ansible all -m cron -a 'minute="*/3" job="/usr/sbin/update 10.0.0.50 &> /dev/null" name="tongbushijian"'

8.yum

ansible all -m yum -a 'name=nginx state=installed'

9.service

ansible all -m service -a 'name=nginx enabled=true state=started'

10.script

cat test.sh
#!/bin/bash
echo "this is test script" > /opt/script.txt
ansible all -m script -a '/tmp/test.sh'
原文地址:https://www.cnblogs.com/fawaikuangtu123/p/10889536.html