【Ansible模块相关】 -- 2019-08-09 12:07:22

原文: http://106.13.73.98/__/149/

@(ansible命令相关)


command

指定目标主机执行命令:

[root@old ~]# ansible db -a 'pwd'
9.0.0.4 | SUCCESS | rc=0 >>
/root
# 命令中的 db 是一个分组

chdir,切换至指定目录后执行命令,一般在编译时使用:

[root@old ~]# ansible db -a 'chdir=/tmp pwd'
9.0.0.4 | SUCCESS | rc=0 >>
/tmp

creates,如果指定的文件存在,则 不执行 后面的操作:

[root@old ~]# ansible db -a 'creates=/tmp pwd'
9.0.0.4 | SUCCESS | rc=0 >>
skipped, since /tmp exists

# 如要注意的是,如果是像下面这样,则会执行后面的操作
t@old ~]# ansible db -a 'creates=/data mkdir /data'
 [WARNING]: Consider using file module with state=directory rather than running mkdir
9.0.0.4 | SUCCESS | rc=0 >>

removes,如果指定的文件存在,则 执行 后面的操作:

[root@old ~]# ansible db -a 'removes=/tmp pwd'
9.0.0.4 | SUCCESS | rc=0 >>
/root

command 不支持 <> | ; & $ 这些特殊字符,如果命令中需要用到这些字符,请使用 shell 命令,见下面。

shell

示例1,给user01用户设置密码:

[root@old ~]# ansible db -m shell -a 'echo "123456" | passwd --stdin user01'
9.0.0.4 | SUCCESS | rc=0 >>
Changing password for user user01.
passwd: all authentication tokens updated successfully.

示例2,执行Python脚本:

[root@old ~]# ansible db -m shell -a '/root/test.py'
9.0.0.4 | SUCCESS | rc=0 >>
Hello, world!
# 前提是该脚本必须有可执行权限


script

被管控机 上执行 管控机 中的文件:

[root@old ~]# ansible db -m script -a '/root/test.sh'
9.0.0.4 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 9.0.0.4 closed.\r\n", 
    "stdout": "", 
    "stdout_lines": []
}


copy

复制文件到远程主机:

ansible db -m copy -a 'dest=/root/test01.sh src=/root/test.sh'
# src:指定本机中的文件
# dest:指定远程主机中要生成的文件

复制文件到远程主机并备份远程文件:

ansible db -m copy -a 'dest=/root/test01.sh src=/root/test.sh backup=yes'
# backup=yes:备份

修改复制后的文件的属主和权限:

ansible db -m copy -a 'dest=/root/test01.sh src=/root/test.sh owner=user01 mode=700'
# owner:指定属主
# mode:指定权限

复制目录到远程主机:

ansible db -m copy -a 'src=/etc/init.d dest=/tmp'
# src:指定本机的目录
# dest:指定远程主机的目录

# 在src末尾加上'/'后,将复制目录中的文件到远程主机:src=/etc/init.d/

复制目录中的文件到远程主机并修改属主和权限:

ansible db -m copy -a 'src=/etc/init.d dest=/tmp owner=user01 mode=755'
# owner:指定属主
# mode:指定权限

content 指定的内容添加到远程主机的文件中:

ansible db -m copy -a 'content="Hello, World!" dest=/tmp/test.txt'
# content:指定内容
# dest:指定远程主机的文件


file

在远程主机上创建一个目录:

ansible db -m file -a 'path=/tmp/test state=directory'
# path:指定要创建的目录
# state=directory:声明这是要创建目录

在远程主机上创建一个文件:

ansible db -m file -a 'path=/tmp/test.txt state=touch'
# path:指定要创建的文件
# state=touch:声明这是要创建文件

删除远程主机上的文件或目录:

ansible db -m file -a 'path=/tmp/test state=absent'
# path:指定要删除的文件/目录
# state=absent:声明这是要删除文件/目录

创建软链接:

ansible db -m file -a 'path=/root/init.d src=/etc/init.d state=link'
# path:指定目标文件
# src:指定源文件
# state=link:声明这是要创建软链接,你也可以指定为hard来创建硬链接


fetch

用于拉取被管控机中的文件/目录:

ansible db -m fetch -a 'dest=/tmp src=/var/log/cron'
# dest:指定存储的目录
# src:指定拉取的文件/目录
# 注意:成功拉取后,会在本地保留被管控机的目录结构


yum

安装yum包:

# 在被管控机上安装lynx
ansible db -m yum -a 'name=lynx'


pip

Python的pip下载工具

# 在被管控机上安装Flask框架
ansible -m pip -a 'name=flask'


service

用于服务控制:

# 启动firewalld服务
ansible db -m service -a 'name=firewalld state=started'

# 停止firewalld服务
ansible db -m service -a 'name=firewalld state=stopped'


cron

用于管理定时任务,参数如下:

  • minute:分
  • hour:时
  • day:天
  • month:月
  • weekday:周

新增定时任务:

# 每隔10分钟执行一次job指定的动作
ansible all -m cron -a 'minute=10 job="touch /tmp/test.txt"'
# job:用于指定动作

新增带名称的定时任务:

# 每隔20分钟执行一次job指定的动作
ansible db -m cron -a 'minute=20 job="touch /tmp/text.txt" name=touchfile'
# name:指定定时任务的名称

注释掉定时任务:

ansible db -m cron -a 'minute=20 job="touch /tmp/text.txt" name=touchfile diabled=yes'
# diabled=yes:注释

删除定时任务:

ansible db -m cron -a 'minute=50 job="touch /tmp/test.txt" name="touchfile" state=absent'
# state=absent:删除
# 注意:删除操作博需指定name才能成功删除,即使是name为None,也必须指定


group

创建组:

ansible db -m group -a 'name=group01'

删除组:

ansible db -m group -a 'name=group01 state=absent'
# state=absent:删除


user

创建用户:

ansible db -m user -a 'name=user01 home=/opt/user01 groups=root uid=2000'
# home:指定宿主目录
# groups:指定附加组
# uid:指定用户ID

删除用户:

ansible db -m user -a 'name=user01 state=absent remove=yes'
# state=absent:删除
# remove=yes:删除用户家目录

原文: http://106.13.73.98/__/149/

猜你喜欢

转载自www.cnblogs.com/gqy02/p/11326420.html