Ansible入门笔记(2)之常用模块

Ansible常用模块

1.1、Ansible Ad-hoc

什么事ad-hoc???就是临时的命令,不会保存,ansible的模式有两种,分别是ad-hoc(命令行模式)和playbook模式。ad-hoc主要用于日常查询和拷贝文件的常用操作。

ad-hoc命令行格式:

ansible  bgx      -m      command      -a      'df -h'
命令   主机名称  指定模块   模块名称   模块动作   具体命令

ad-hoc执行的状态返回信息:

  • 绿色:执行成功并且不需要做改变的动作
  • 黄色:执行成功并且对目标主机做变更
  • 红色:执行失败

ad-hoc的执行过程:

  1. 加载自己的配置文件,默认/etc/ansible/ansible.cfg
  2. 加载自己对应的模块文件,如command ping
  3. 通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器
  4. 给文件+x权限
  5. 执行并返回结果
  6. 删除临时py文件,sleep 10 退出

1.2、Ansible的基础命令

ansible ansible-doc ansible-playbook ansible-vault ansible-console ansible-galaxy ansible-pull

Ansible-doc 显示模块帮助
ansible-doc [options] [module...]

-a 显示所有模块的文档

-l --list 列出可用模块

-s --snippet 显示制定模块的playbook片段

示例:

ansible-doc -l 列出所有模块

ansible-doc ping 查看制定模块帮助用法

ansible-doc -s ping 查看制定模块帮助用法

Ansible命令用法
ansible <host-pattern> [-m module_name] [-a args]

--version 显示版本

-m module 制定模块默认为 command

-v 详细过程 -vv -vvv更详细

--list-hosts 显示主机列表 可以简写为--list

-k --ask-pass 提示数据ssh连接密码 默认key验证

-K --ask-become-pass 提示输入sodu的口令

-C check 检查并不执行

-T --timeout=TIMEOUT 执行命令的超时时间 默认10s

-u --user=REMOTE_USER 执行远程执行的用户

-b --become 代替旧版的sudo切换

1.3、常用模块

1.3.1、command模块

# 默认模块,执行命令
[root@ansible ~]# ansible web -m command -a "hostname"
192.168.0.135 | CHANGED | rc=0 >>
ansible

192.168.0.101 | CHANGED | rc=0 >>
localhost.localdomain

1.3.2、shell模块

# 和command类似,但是在执行带有管道命令执行时,则需要用shell模块,如下
[root@ansible ~]# ansible web -m shell -a "ifconfig eth0 | awk 'NR==2 {print $2}'"
192.168.0.101 | CHANGED | rc=0 >>
        inet 192.168.0.101  netmask 255.255.255.0  broadcast 192.168.0.255

192.168.0.135 | CHANGED | rc=0 >>
        inet 192.168.0.135  netmask 255.255.255.0  broadcast 192.168.0.255

1.3.3、script模块

[root@ansible ~]# mkdir ansible
[root@ansible ~]# cd ansible/
[root@ansible ansible]# ls
[root@ansible ansible]# vim 1.sh
#!/bin/bash
hostname
[root@ansible ansible]# chmod +x 1.sh

#在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[root@ansible ansible]# ansible web -m script -a 1.sh 

1.3.4、copy模块

#拷贝文件模块
[root@ansible ~]# ansible web -m copy -a "src=/etc/hosts dest=/tmp/test.txt"
[root@ansible ~]# ansible web -a "ls /tmp/test.txt"
192.168.0.135 | CHANGED | rc=0 >>
/tmp/test.txt

192.168.0.116 | CHANGED | rc=0 >>
/tmp/test.txt

#除了基础的拷贝,还可以在拷贝的时候对目标主机的文件进行备份,更改所属和权限等等。
src             #推送数据的源文件信息
dest            #推送数据的目标路径
backup          #对推送传输过去的文件,进行备份
content         #直接批量在被管理端文件中添加内容
group           #将本地文件推送到远端,指定文件属组信息
owner           #将本地文件推送到远端,指定文件属主信息
mode            #将本地文件推送到远端,指定文件权限信息

[root@ansible ~]# ansible web -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes mode=755 owner=bin"
[root@ansible ~]# ansible web -a 'ls -l /tmp/test.txt'
192.168.0.135 | CHANGED | rc=0 >>
-rwxr-xr-x 1 bin root 158 Nov 15 13:56 /tmp/test.txt

192.168.0.116 | CHANGED | rc=0 >>
-rwxr-xr-x 1 bin root 158 Nov 15 13:56 /tmp/test.txt

1.3.5、fetch模块

#从客户端取文件到服务端,与copy相反
[root@ansible ~]# ansible web -m fetch -a 'src=/root/2.sh dest=/tmp/'
[root@ansible ~]# tree /tmp/192.168.0.1*
/tmp/192.168.0.116
└── root
    └── 2.sh
/tmp/192.168.0.135
└── root
    └── 2.sh
#默认只支持单个文件,不支持多个文件或目录,做个文件建议放进目录内tar打包后进行拉取

1.3.6、file模块

# 更改文件属性
[root@ansible ~]# ansible web -m file -a 'path=/root/2.sh mode=755'
# 创建文件
[root@ansible ~]# ansible web -m file -a "path=/tmp/22.txt state=touch"
# 创建目录
[root@ansible ~]# ansible web -m file -a "path=/tmp/textdir state=directory"
# 创建软链接
[root@ansible ~]# ansible web -m file -a "src=/tmp/textdir path=/tmp/textdir_link state=link"

#这里的path也可以使用dest或者是name关键字进行代替,都是一样的效果,具体可以查看ansible-doc file的帮助文档

path            #指定远程主机目录或文件信息
recurse         #递归授权
state 
    directory   #在远端创建目录
    touch       #在远端创建文件
    link        #link或hard表示创建链接文件
    absent      #表示删除文件或目录
    mode        #设置文件或目录权限
    owner       #设置文件或目录属主信息
    group       #设置文件或目录属组信息

1.3.7、hostname模块

[root@ansible ~]# ansible 192.168.0.116 -m hostname -a "name=node01"
[root@ansible ~]# ansible 192.168.0.116 -a "hostname"
192.168.0.116 | CHANGED | rc=0 >>
node01

1.3.8、cron模块

#  创建计划任务
[root@ansible ~]# ansible web -m cron -a 'minute=* weekday=1,3,5 job="ntpdate ntp1.aliyun.com" name=update_time'
[root@ansible ~]# ansible web -m shell -a "crontab -l"
192.168.0.135 | CHANGED | rc=0 >>
#Ansible: update_time
* * * * 1,3,5 ntpdate ntp1.aliyun.com

192.168.0.116 | CHANGED | rc=0 >>
#Ansible: update_time
* * * * 1,3,5 ntpdate ntp1.aliyun.com

# 取消计划任务,必须是在全部命令加上disabled,否则就是新建的另一个计划任务
[root@ansible ~]# ansible web -m cron -a 'disabled=true minute=* weekday=1,3,5 job="ntpdate ntp1.aliyun.com" name=update_time'

# 再次打开计划任务
[root@ansible ~]# ansible web -m cron -a 'disabled=false job="ntpdate ntp1.aliyun.com" name=update_time'

# 删除计划任务
[root@ansible ~]# ansible web -m cron -a 'job="ntpdate ntp1.aliyun.com" name=update_time state=absent'

1.3.9、yum模块

[root@ansible ~]# ansible web -m yum -a "name=vsftpd"
name        #指定要安装的软件包名称
state       #指定使用yum的方法
    installed,present   #安装软件包
    removed,absent      #移除软件包
    latest              #安装最新软件包 
[root@ansible ~]# ansible web -m shell -a "rpm -q vsftpd"

1.3.10、service模块

# 设置服务的启动、关闭、重启以及开机自启
[root@ansible ~]# ansible web -m service -a "name=vsftpd state=started enabled=yes"
[root@ansible ~]# ansible web -m shell -a "netstat -tulnp |grep 21"

name        # 定义要启动服务的名称
state       # 指定服务状态
    started     #启动服务
    stopped     #停止服务
    restarted   #重启服务
    reloaded    #重载服务
enabled         #开机自启

1.3.11、group模块

[root@ansible ~]# ansible web -m group -a "name=testing gid=1010"
name            #指定创建的组名
gid             #指定组的gid
state
    absent      #移除远端主机的组
    present     #创建远端主机的组(默认)

1.3.12、user模块

[root@ansible ~]# ansible web -m user -a "name=kobe uid=1012 group=1010 shell=/sbin/nologin create_home=no"

uid             #指定用户的uid
group           #指定用户组名称
groups          #指定附加组名称
password        #给用户添加密码
shell           #指定用户登录shell
create_home     #是否创建家目录
原文地址:https://www.cnblogs.com/linuxk/p/11866765.html