2019-04-24ansible模块

1,查看使用帮助

ansible-doc -l        =============>相当于Linux里面的命令

查模块的参数

ansible-doc -s ==========>>接模块名查看模块参数信息

===================>查模块信息就相当于查Linux里面的命令,模块就是ansible里的命令


2,command模块知识

=======================================================

参数   chdir=/tmp  配置相当于cd /tmp

[root@BOSS ~]# ansible oldboy -m command -a "pwd chdir=/etc"

172.16.1.31 | CHANGED | rc=0 >>

/etc

172.16.1.41 | CHANGED | rc=0 >>

/etc

=======================================================

参数   creates=/etc  相当于条件测试

[root@BOSS ~]# ansible oldboy -m command -a "pwd creates=/etc"

172.16.1.31 | SUCCESS | rc=0 >>

skipped, since /etc exists

172.16.1.41 | SUCCESS | rc=0 >>

skipped, since /etc exists

==============================================================

参数   removes=/root   相当于条件测试

[root@BOSS ~]# ansible oldboy -m command -a "ls /root removes=/etc"

172.16.1.41 | CHANGED | rc=0 >>

(date +%F %T).tar.gz

(date +%F).tar.gz

===================================>>省略若干

参数   warn=False   忽略警告

[root@BOSS ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts"

[WARNING]: Consider using the file module with mode rather than running 'chmod'.  If you need to use command because file is insufficient you can add 'warn: false' to

this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

172.16.1.41 | CHANGED | rc=0 >>

172.16.1.31 | CHANGED | rc=0 >>    ======================>>会出现警告

[root@BOSS ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts warn=False"

172.16.1.41 | CHANGED | rc=0 >>

172.16.1.31 | CHANGED | rc=0 >>===============>>>不出现警告

[root@BOSS ~]# ansible oldboy -m command -a "ls -la /etc/hosts"

172.16.1.41 | CHANGED | rc=0 >>

----------. 1 root root 332 Apr 12 15:08 /etc/hosts

172.16.1.31 | CHANGED | rc=0 >>

----------. 1 root root 332 Apr 12 15:08 /etc/hosts     ========>>>记得更改回来,不要太皮


3,shell模块功能介绍

shell  执行一个命令在远程节点上

=================================================================

[root@BOSS ~]# ansible oldboy -m shell -a "echo oldboy >/tmp/a.txt"

172.16.1.31 | CHANGED | rc=0 >>

172.16.1.41 | CHANGED | rc=0 >>

[root@BOSS ~]# ansible oldboy -m shell -a "cat /tmp/a.txt"

172.16.1.41 | CHANGED | rc=0 >>

oldboy

172.16.1.31 | CHANGED | rc=0 >>

oldboy

========================================>>>实践增加文本

[root@BOSS ~]# ansible oldboy -m shell -a "sh -x /server/scripts/bak.sh"

172.16.1.41 | FAILED | rc=127 >>

sh: /server/scripts/bak.sh: No such file or directorynon-zero return code

172.16.1.31 | CHANGED | rc=0 >>

++ date +%F

+ /bin/cp /etc/hosts /opt/hosts_2019-04-24

===========================================>>>远程执行脚本条件是必须存在

[root@NFS /server/scripts]# scp bak.sh 172.16.1.41:/server/scripts

------------------------------------------------------->>>使用scp拷贝

[root@BOSS ~]# ansible oldboy -m shell -a "sh -x /server/scripts/bak.sh"

172.16.1.31 | CHANGED | rc=0 >>

++ date +%F

+ /bin/cp /etc/hosts /opt/hosts_2019-04-24

172.16.1.41 | CHANGED | rc=0 >>

++ date +%F

+ /bin/cp /etc/hosts /opt/hosts_2019-04-24

=============================================>>>在执行就成功了

===============================================================


4,copy模块功能说明

复制文件到远程主机

常见参数说明

src    拷贝什么东西的原路径

dest   往哪儿拷贝

owner   属组

group   属组

mode   权限

backup   需不需要操作前备份

content    

force   

-----------------------------------------------------------------------------------------------------------

实践:

[root@BOSS ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/opt mode=ugo+x"

172.16.1.31 | CHANGED => {

    "changed": true,

    "checksum": "e52c528913b5c22d388cc2a18f6943641c8442c3", 

------------------------------------------------------>>>省略若干

172.16.1.41 | CHANGED => {

    "changed": true,

    "checksum": "e52c528913b5c22d388cc2a18f6943641c8442c3", 

--------------------------------------------------------->>>省略若干

查看:

[root@BOSS ~]# ansible oldboy -m shell -a "ls -ld /opt/hosts"

172.16.1.31 | CHANGED | rc=0 >>

-r-x--x--x 1 root root 332 Apr 24 10:34 /opt/hosts

172.16.1.41 | CHANGED | rc=0 >>

-r-x--x--x 1 root root 332 Apr 24 10:34 /opt/hosts

---------------------------------------------------------------------------->>>拷贝成功


5,script模块功能说明

远程节点上运行本地脚本

实践:

[root@BOSS ~]# vim ff.sh

  1 echo 666 ------------------------->>>创建脚本

执行脚本:

[root@BOSS ~]# ansible oldboy -m script -a "ff.sh"

172.16.1.31 | CHANGED => {

--------------------------------->省略若干

"stdout_lines": [

        "666"

172.16.1.41 | CHANGED => {

----------------------------------->省略若干

"stdout_lines": [

        "666"

======================================>>>执行成功


6,file模块功能说明

主要用于创建文件或目录数据,以及多存在的文件或目录权限属性进行修改管理

src     源

path   路径

owner   属主

group     属主

mode    权限

state    状态

-------------------------------------------------------------------------------------------------------------------------

实践:

创建目录:

[root@BOSS ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory" 

查看:

[root@BOSS ~]# ansible oldboy -m command -a "ls -l /tmp/"

172.16.1.41 | CHANGED | rc=0 >>

drwxr-xr-x 2 root dengli 6 Apr 25 00:27 oldboy_dir

172.16.1.31 | CHANGED | rc=0 >>

drwxr-xr-x 2 root dengli 6 Apr 25 00:27 oldboy_dir

------------------------------------------------------------------------------------->>>创建成功

用command实现:

[root@BOSS ~]# ansible oldboy -m command -a "mkdir -p /tmp/oldboy_dir1 warn=false"

172.16.1.31 | CHANGED | rc=0 >>

172.16.1.41 | CHANGED | rc=0 >>

查看:

[root@BOSS ~]# ansible oldboy -m command -a "ls -l /tmp/"

172.16.1.41 | CHANGED | rc=0 >>

drwxr-xr-x 2 root dengli 6 Apr 25 00:35 oldboy_dir1

172.16.1.31 | CHANGED | rc=0 >>

drwxr-xr-x 2 root dengli 6 Apr 25 00:35 oldboy_dir1

--------------------------------------------------------------------------------->>>实现成功

创建文件:

[root@BOSS ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch" 

-------------------------------------------------------------------------------------------------------------------

删除文件:

[root@BOSS ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_file state=absent"

-----------------------------------------------------------------------------------------------------------------------

创建链接文件:

[root@BOSS ~]# ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/link_file state=link"

-----------------------------------------------------------------------------------------------------------------------


7,yum模块

示例:

[root@BOSS ~]# ansible oldboy -m yum -a "name=inotify-tools state=installed"

------------------------------------------------------------>>>安装

-------------------------------------------------------------------------------------------------------------------------


8,systemd模块


9,cron模块

参数

minute   运行job分钟信息

hour     运行job小时时间信息

day     运行job日期时间信息

month   运行job月份时间信息

weekday   运行job周期时间信息

job     具体的任务

name     名字,描述信息

state      状态

disabled        静止

---------------------------------------------------------------------------------------------------------------------

创建定时任务:

[root@BOSS ~]# ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdata time.nist.gov >/dev/null 2>&1'"

------------------------------------------------------------------>>>命令行执行成功

查看:

[root@BOSS ~]# ansible oldboy -m command -a "crontab -l"

172.16.1.41 | CHANGED | rc=0 >>

#Ansible: sync time

00 00 * * * /usr/sbin/ntpdata time.nist.gov >/dev/null 2>&1

172.16.1.31 | CHANGED | rc=0 >>

#Ansible: sync time

00 00 * * * /usr/sbin/ntpdata time.nist.gov >/dev/null 2>&1

----------------------------------------------------------------------------------------------->>>成功

你可能感兴趣的:(2019-04-24ansible模块)