ansible--模块

目录

查看模块说明ansible-doc -s 模块名

Command

shell

Script

copy

Fetch

Lineinfile(单行替换、追加)

replace(全文替换)

File

Hostname

Cron

Yum

Service

User

Group

setup

unarchive

archive


查看模块说明
ansible-doc -s 模块名


Command

在远程主机执行命令,默认模块,可忽略-m选项
 ansible appsrvs -m command -a 'service httpd start'
 此命令不支持特殊符号

[root@ansible ~]# ansible web -m command -a "systemctl status firewalld"
10.10.10.3 | CHANGED | rc=0 >>
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since Fri 2021-06-18 11:59:42 EDT; 18h ago
 Main PID: 841 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─841 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Jun 18 11:59:42 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
10.10.10.4 | CHANGED | rc=0 >>
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since Fri 2021-06-18 11:59:45 EDT; 18h ago
 Main PID: 844 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─844 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Jun 18 11:59:45 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.

参数:

chdir    执行命令前先切换文件夹

creates     如果存在,则不执行后续命令 

removes  如果不存在,则不执行后续命令


shell

和command相似,用shell执行命令
 ansible appsrvs -m shell -a 'echo mage |passwd -stdin wang'
 调用bash执行脚本可能会失败,解决方法:把脚本copy到远程,执行,再把需要的结果拉回执行命令的机器

[root@ansible ~]# ansible web -m shell -a "ls /root"
10.10.10.3 | CHANGED | rc=0 >>
anaconda-ks.cfg
10.10.10.4 | CHANGED | rc=0 >>
anaconda-ks.cfg

Script

运行脚本
 -a "/PATH/TO/SCRIPT_FILE"
 ansible appsrvs -m script -a f1.sh

[root@ansible ~]# ansible web -m script -a f1.sh 
10.10.10.3 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 10.10.10.3 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 10.10.10.3 closed."
    ], 
    "stdout": "how are you!\r\n", 
    "stdout_lines": [
        "how are you!"
    ]
}
10.10.10.4 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 10.10.10.4 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 10.10.10.4 closed."
    ], 
    "stdout": "how are you!\r\n", 
    "stdout_lines": [
        "how are you!"
    ]
}

copy

从服务器复制文件到客户端
 ansible all -m copy -a "src=/root/f1.sh dest=/root/f1.sh owner=root mode=660 backup=yes"
 如目标存在,默认覆盖,此处指定先备份

[root@ansible ~]# ansible all -m copy -a "src=/root/f1.sh dest=/root/f1.sh owner=root mode=660 backup=yes"  
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "b3b5655a054c6c8cdb872c91969bc847ec8f292b", 
    "dest": "/root/f1.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "b7ec8982c505bd06282ea06042df9513", 
    "mode": "0660", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 55, 
    "src": "/root/.ansible/tmp/ansible-tmp-1623934770.44-10114-13832924604287/source", 
    "state": "file", 
    "uid": 0
}
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup_file": "/root/f1.sh.11606.2021-06-19@07:14:00~", 
    "changed": true, 
    "checksum": "b3b5655a054c6c8cdb872c91969bc847ec8f292b", 
    "dest": "/root/f1.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "b7ec8982c505bd06282ea06042df9513", 
    "mode": "0660", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 55, 
    "src": "/root/.ansible/tmp/ansible-tmp-1623934770.44-10112-168939570821837/source", 
    "state": "file", 
    "uid": 0
}
[root@ansible ~]# ansible all -m command -a "ls /root"
10.10.10.3 | CHANGED | rc=0 >>
abc.txt
anaconda-ks.cfg
f1.sh
f1.sh.11606.2021-06-19@07:14:00~
10.10.10.4 | CHANGED | rc=0 >>
abc.txt
anaconda-ks.cfg
f1.sh

ansible all -m copy -a 'content="hello mage\n" dest=/data/f1.txt'
 利用内容,直接生成目标文件

[root@ansible ~]# ansible all -m copy -a 'content="hello world\n" dest=/root/test.txt'
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511", 
    "dest": "/root/test.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "6f5902ac237024bdd0c176cb93063dc4", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 12, 
    "src": "/root/.ansible/tmp/ansible-tmp-1623935702.72-10780-119282756277989/source", 
    "state": "file", 
    "uid": 0
}
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511", 
    "dest": "/root/test.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "6f5902ac237024bdd0c176cb93063dc4", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 12, 
    "src": "/root/.ansible/tmp/ansible-tmp-1623935702.7-10778-1032768701368/source", 
    "state": "file", 
    "uid": 0
}
[root@ansible ~]# ansible web -m shell -a "cat /root/test.txt"
10.10.10.3 | CHANGED | rc=0 >>
hello world
10.10.10.4 | CHANGED | rc=0 >>
hello world

Fetch

从客户端取文件至服务器端,copy相反,目录可先tar
 ansible all -m fetch -a 'src=/root/test.txt dest=/root/'

[root@ansible ~]# ansible web -m fetch -a "src=/root/test.txt dest=/root/" 
10.10.10.4 | CHANGED => {
    "changed": true, 
    "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511", 
    "dest": "/root/10.10.10.4/root/test.txt", 
    "md5sum": "6f5902ac237024bdd0c176cb93063dc4", 
    "remote_checksum": "22596363b3de40b06f981fb85d82312e8c0ed511", 
    "remote_md5sum": null
}
10.10.10.3 | CHANGED => {
    "changed": true, 
    "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511", 
    "dest": "/root/10.10.10.3/root/test.txt", 
    "md5sum": "6f5902ac237024bdd0c176cb93063dc4", 
    "remote_checksum": "22596363b3de40b06f981fb85d82312e8c0ed511", 
    "remote_md5sum": null
}
[root@ansible ~]# tree 
.
├── 10.10.10.3
│   └── root
│       └── test.txt
├── 10.10.10.4
│   └── root
│       └── test.txt
├── anaconda-ks.cfg
├── f1.sh
├── hosts
└── ssh.yml

Lineinfile(单行替换、追加)

相当于sed,可以修改文件内容;如果内容不存在,则在文本最后追加

参数

path(dest, destfile, name):目标文件

line:修改或添加后的字符

regexp:匹配的字符,支持正则表达式

state:present替换(默认),state=absent删除

backrefs:yes或no

                默认值为no,如果没有匹配,则添加一行line。如果匹配了,则把匹配的内容替换为line的内容。值为yes时,表示开启后向引用,如果没有匹配,则文件保持不变。如果匹配了,把匹配内容替被换为line的内容。

backup:yes或no,修改前备份

create:yes或no,当文件不存在时是否创建

        

ansible web -m lineinfile -a "path=目标文件 state=操作 regexp="匹配字符""

[root@ansible ~]# ansible web -m lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-eno16777736 state=absent regexp="^IPV6""
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "found": 6, 
    "msg": "6 line(s) removed"
}
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "found": 6, 
    "msg": "6 line(s) removed"
}

ansible web -m lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-eno16777736 regexp="^DNS1" line="DNS1=8.8.8.8" backup=yes

[root@ansible ~]# ansible web -m lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-eno16777736 regexp="^DNS1" line="DNS1=8.8.8.8" backup=yes" 
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "/etc/sysconfig/network-scripts/ifcfg-eno16777736.2577.2021-06-25@11:10:04~", 
    "changed": true, 
    "msg": "line replaced"
}
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "/etc/sysconfig/network-scripts/ifcfg-eno16777736.2549.2021-06-25@11:10:04~", 
    "changed": true, 
    "msg": "line replaced"
}
[root@ansible ~]# ansible web -m command -a "cat /etc/sysconfig/network-scripts/ifcfg-eno16777736"
10.10.10.3 | CHANGED | rc=0 >>
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eno16777736
UUID=aba468c1-963d-44c2-bac4-5695ffc85163
ONBOOT=yes
IPADDR=10.10.10.3
PREFIX=24
GATEWAY=10.10.10.1
DNS1=8.8.8.8
10.10.10.4 | CHANGED | rc=0 >>
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eno16777736
UUID=aba468c1-963d-44c2-bac4-5695ffc85163
ONBOOT=yes
DNS1=8.8.8.8
IPADDR=10.10.10.4
PREFIX=24
GATEWAY=10.10.10.1
[root@ansible ~]# ansible web -m shell -a "ls /etc/sysconfig/network-scripts/ |grep "^ifcfg-eno16*""       
10.10.10.3 | CHANGED | rc=0 >>
ifcfg-eno16777736
ifcfg-eno16777736.2577.2021-06-25@11:10:04~
10.10.10.4 | CHANGED | rc=0 >>
ifcfg-eno16777736
ifcfg-eno16777736.2549.2021-06-25@11:10:04~

replace(全文替换)

相当于sed,可以修改文件内容

参数

path(dest, destfile, name):目标文件

regexp:匹配的字符,支持正则表达式

replace:指定最终要替换成的字符串

backup:修改前备份

 ansible web -m replace -a "path=/root/f1.sh regexp="echo" replace="set"" 

[root@ansible ~]# ansible web -m replace -a "path=/root/f1.sh regexp="echo" replace="set"" 
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "2 replacements made"
}
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "2 replacements made"
}
[root@ansible ~]# ansible web -a "cat /root/f1.sh"
10.10.10.4 | CHANGED | rc=0 >>
#!/bin/bash
#This is test script!

set "how are you!"
set "hello world"
10.10.10.3 | CHANGED | rc=0 >>
#!/bin/bash
#This is test script!

set "how are you!"
set "hello world"

File

设置文件属性(path=name=dest)
        state=  directory   创建目录
                    touch     创建文件
                    link       创建软连接
                    absent     删除
                    removed       删除

 ansible all -m file -a "path=/root/a.sh owner=wang mode=755"    ***修改a.sh文件属性

[root@ansible ~]# ansible all -m command -a "ls  -l /root/test.txt"
10.10.10.4 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 12 Jun 19 07:29 /root/test.txt
10.10.10.3 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 12 Jun 19 07:29 /root/test.txt
[root@ansible ~]# ansible all -m file -a "path=/root/test.txt mode=666" 
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0666", 
    "owner": "root", 
    "path": "/root/test.txt", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 12, 
    "state": "file", 
    "uid": 0
}
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0666", 
    "owner": "root", 
    "path": "/root/test.txt", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 12, 
    "state": "file", 
    "uid": 0
}
[root@ansible ~]# ansible all -m command -a "ls  -l /root/test.txt"       
10.10.10.3 | CHANGED | rc=0 >>
-rw-rw-rw-. 1 root root 12 Jun 19 07:29 /root/test.txt
10.10.10.4 | CHANGED | rc=0 >>
-rw-rw-rw-. 1 root root 12 Jun 19 07:29 /root/test.txt


 ansible all -m file -a "state=touch name=/root/a.sh"        ***创建a.sh文件

[root@ansible ~]# ansible web -m file -a "state=touch name=/root/a.sh" 
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/root/a.sh", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/root/a.sh", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
[root@ansible ~]# ansible all -m command -a "ls  -l /root/a.sh"    
10.10.10.3 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 0 Jun 19 07:56 /root/a.sh
10.10.10.4 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 0 Jun 19 07:56 /root/a.sh

Hostname

管理主机名
 ansible all -m hostname -a "name=appserver"

[root@ansible ~]# ansible 10.10.10.3 -m hostname -a "name=123.test.com"
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "test.com", 
        "ansible_fqdn": "123.test.com", 
        "ansible_hostname": "123", 
        "ansible_nodename": "123.test.com", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "123.test.com"
}
[root@ansible ~]# ansible 10.10.10.3 -m command -a "hostnamectl"
10.10.10.3 | CHANGED | rc=0 >>
   Static hostname: 123.test.com
         Icon name: computer
           Chassis: n/a
        Machine ID: 6a80ef7553564746a41711e951c24f0d
           Boot ID: 16a27fc68d9a41da91751d989a358abf
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-123.9.3.el7.x86_64
      Architecture: x86_64

Cron

计划任务
 支持时间:minute,hour,day,month,weekday
 

#备份数据库脚本
[root@centos8 ~]#cat mysql_backup.sh 
mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip > /data/mysql_date +%F_%T.sql.gz
#创建任务
ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh'
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime"
#禁用计划任务
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime disabled=yes"
#启用计划任务
ansible websrvs   -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime disabled=no"
#删除任务
ansible websrvs -m cron -a "name='backup mysql' state=absent"
ansible websrvs -m cron -a 'state=absent name=Synctime'

Yum

管理包
 ansible all -m yum -a "name=httpd"   ***安装,默认是安装,state=latest

[root@ansible ~]# ansible web -m yum -a "name=ntpdate"
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "ntpdate"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package ntpdate.x86_64 0:4.2.6p5-18.el7.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch          Version                        Repository    Size\n================================================================================\nInstalling:\n ntpdate        x86_64        4.2.6p5-18.el7.centos          cdrom         82 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 82 k\nInstalled size: 121 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : ntpdate-4.2.6p5-18.el7.centos.x86_64                         1/1 \n  Verifying  : ntpdate-4.2.6p5-18.el7.centos.x86_64                         1/1 \n\nInstalled:\n  ntpdate.x86_64 0:4.2.6p5-18.el7.centos                                        \n\nComplete!\n"
    ]
}
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "ntpdate"
        ]
    }, 
    "msg": "Existing lock /var/run/yum.pid: another copy is running as pid 13416.\nAnother app is currently holding the yum lock; waiting for it to exit...\n  The other application is: yum\n    Memory :  27 M RSS (344 MB VSZ)\n    Started: Sat Jun 19 08:38:22 2021 - 00:28 ago\n    State  : Running, pid: 13416\n", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package ntpdate.x86_64 0:4.2.6p5-18.el7.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch          Version                        Repository    Size\n================================================================================\nInstalling:\n ntpdate        x86_64        4.2.6p5-18.el7.centos          cdrom         82 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 82 k\nInstalled size: 121 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : ntpdate-4.2.6p5-18.el7.centos.x86_64                         1/1 \n  Verifying  : ntpdate-4.2.6p5-18.el7.centos.x86_64                         1/1 \n\nInstalled:\n  ntpdate.x86_64 0:4.2.6p5-18.el7.centos                                        \n\nComplete!\n"
    ]
}


 ansible all -m yum -a "name=httpd state=ansent"   ***卸载

[root@ansible ~]# ansible web -m yum -a "name=ntpdate state=absent"            10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "removed": [
            "ntpdate"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nResolving Dependencies\n--> Running transaction check\n---> Package ntpdate.x86_64 0:4.2.6p5-18.el7.centos will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch          Version                       Repository     Size\n================================================================================\nRemoving:\n ntpdate        x86_64        4.2.6p5-18.el7.centos         @cdrom        121 k\n\nTransaction Summary\n================================================================================\nRemove  1 Package\n\nInstalled size: 121 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Erasing    : ntpdate-4.2.6p5-18.el7.centos.x86_64                         1/1 \n  Verifying  : ntpdate-4.2.6p5-18.el7.centos.x86_64                         1/1 \n\nRemoved:\n  ntpdate.x86_64 0:4.2.6p5-18.el7.centos                                        \n\nComplete!\n"
    ]
}
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "removed": [
            "ntpdate"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nResolving Dependencies\n--> Running transaction check\n---> Package ntpdate.x86_64 0:4.2.6p5-18.el7.centos will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch          Version                       Repository     Size\n================================================================================\nRemoving:\n ntpdate        x86_64        4.2.6p5-18.el7.centos         @cdrom        121 k\n\nTransaction Summary\n================================================================================\nRemove  1 Package\n\nInstalled size: 121 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Erasing    : ntpdate-4.2.6p5-18.el7.centos.x86_64                         1/1 \n  Verifying  : ntpdate-4.2.6p5-18.el7.centos.x86_64                         1/1 \n\nRemoved:\n  ntpdate.x86_64 0:4.2.6p5-18.el7.centos                                        \n\nComplete!\n"
    ]
}


 ansible all -m yum -a "list=installed"        ***显示已安装的包


Service

管理服务

ansible all -m service -a 'name=httpd state=started enabled=yes'

ansible all -m service -a 'name=httpd state=stopped'

ansible all -m service -a 'name=httpd state=reloaded’

User

管理用户

 ansible all -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1 group=root'    ***创建用户,添加comment(注释)

 ansible all -m user -a 'name=sysuser1 system=yes home=/app/sysuser1  shell=/sbin/nologin'    ***创建系统账号

 ansible all -m user -a 'name=user1 state=absent remove=yes'    ***删除用户及家目录等数据

Group

管理组

 ansible web -m group -a "name=testgroup system=yes"

 ansible web -m group -a "name=testgroup state=absent"

setup

获取主机信息

参数

filter:支持正则表达式

ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。
[root@ansible ~]# ansible web -m setup -a "filter=ansible_all_ipv4_addresses"
10.10.10.3 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.10.10.3"
        ], 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.10.10.4 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.10.10.4"
        ], 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

unarchive

解压缩

参数

copy:默认yes,将ansible主机上的压缩包传到远程主机后解压缩至特定目录;no,将远程主机上的某个压缩包解压缩到指定路径下

src:源路径

dest:远程主机上的目标绝对路径

mode:设置解压缩后的文件权限

owner:解压后文件或目录的所有者

group:解压后的目录或文件的所属组

exec:列出需要排除的目录和文件

ansible web  -m unarchive -a "src=dir.tar.gz dest=/root"

[root@ansible ~]# ansible web  -m unarchive -a "src=dir.tar.gz dest=/root"     10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/root", 
    "extract_results": {
        "cmd": [
            "/usr/bin/gtar", 
            "--extract", 
            "-C", 
            "/root", 
            "-z", 
            "-f", 
            "/root/.ansible/tmp/ansible-tmp-1624638524.98-3958-159337705303967/source"
        ], 
        "err": "", 
        "out": "", 
        "rc": 0
    }, 
    "gid": 0, 
    "group": "root", 
    "handler": "TgzArchive", 
    "mode": "0550", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 4096, 
    "src": "/root/.ansible/tmp/ansible-tmp-1624638524.98-3958-159337705303967/source", 
    "state": "directory", 
    "uid": 0
}
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/root", 
    "extract_results": {
        "cmd": [
            "/usr/bin/gtar", 
            "--extract", 
            "-C", 
            "/root", 
            "-z", 
            "-f", 
            "/root/.ansible/tmp/ansible-tmp-1624638524.98-3959-36206290823956/source"
        ], 
        "err": "", 
        "out": "", 
        "rc": 0
    }, 
    "gid": 0, 
    "group": "root", 
    "handler": "TgzArchive", 
    "mode": "0550", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 4096, 
    "src": "/root/.ansible/tmp/ansible-tmp-1624638524.98-3959-36206290823956/source", 
    "state": "directory", 
    "uid": 0
}
[root@ansible ~]# ansible web -m shell -a "ls /root |grep "10.10.10*""
10.10.10.3 | CHANGED | rc=0 >>
10.10.10.3
10.10.10.4 | CHANGED | rc=0 >>
10.10.10.3

archive

打包压缩

参数

path:源文件

dest:压缩后路径

format:指定压缩类型,包括:bz2、gz(默认)、tar、xz、zip

remove:默认为no,在打包/压缩后,不删除源文件

[root@ansible ~]# ansible web -m archive -a "path=/root/f1.sh dest=/root/f1.tar.gz"
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "archived": [
        "/root/f1.sh"
    ], 
    "arcroot": "/root/", 
    "changed": true, 
    "dest": "/root/f1.tar.gz", 
    "expanded_exclude_paths": [], 
    "expanded_paths": [
        "/root/f1.sh"
    ], 
    "gid": 0, 
    "group": "root", 
    "missing": [], 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 92, 
    "state": "file", 
    "uid": 0
}
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "archived": [
        "/root/f1.sh"
    ], 
    "arcroot": "/root/", 
    "changed": true, 
    "dest": "/root/f1.tar.gz", 
    "expanded_exclude_paths": [], 
    "expanded_paths": [
        "/root/f1.sh"
    ], 
    "gid": 0, 
    "group": "root", 
    "missing": [], 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 92, 
    "state": "file", 
    "uid": 0
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金牌收租佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值