栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

ansible常用模块使用详解

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ansible常用模块使用详解

ansible常用模块使用详解
    • ansible常用模块使用详解
      • 1.ansible常用模块之ping
      • 2. ansible常用模块之command
      • 3. ansible常用模块之raw
      • 4.ansible常用模块之shell
      • 5.ansible常用模块之script
      • 6. ansible常用模块之template
      • 7. ansible常用模块之yum
      • 8. ansible常用模块之copy
      • 9. ansible常用模块之group
      • 10. ansible常用模块之user
      • 11. ansible常用模块之service

ansible常用模块使用详解 1.ansible常用模块之ping
[root@hm1 ~]# ansible all -m ping
192.168.70.138 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

2. ansible常用模块之command
[root@hm1 ~]# ansible 192.168.70.138 -m command -a 'mkdir 123'
[WARNING]: Consider using the file module with state=directory rather than
running 'mkdir'.  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.
192.168.70.138 | CHANGED | rc=0 >>
[root@hm1 ~]# ansible 192.168.70.138 -m command -a 'ls '
192.168.70.138 | CHANGED | rc=0 >>
123
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
initial-setup-ks.cfg

[root@hm2 ~]# ls
123   模板  图片  下载  桌面             initial-setup-ks.cfg
公共  视频  文档  音乐  anaconda-ks.cfg
支持linux的全部命令
command模块不支持管道符,不支持重定向
3. ansible常用模块之raw
//支持重定向
[root@ansible ~]# ansible 172.16.103.129 -m raw -a 'echo "hello world" > /tmp/test'
172.16.103.129 | SUCCESS | rc=0 >>
Shared connection to 172.16.103.129 closed.


[root@ansible ~]# ansible 172.16.103.129 -a 'cat /tmp/test'
172.16.103.129 | SUCCESS | rc=0 >>
hello world


//支持管道符
[root@ansible ~]# ansible 172.16.103.129 -m raw -a 'cat /tmp/test|grep -Eo hello'
172.16.103.129 | SUCCESS | rc=0 >>
hello
Shared connection to 172.16.103.129 closed.
4.ansible常用模块之shell

shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。

//查看受控机上的脚本
[root@localhost ~]# ll /scripts/
总用量 4
-rwxr-xr-x 1 root root 52 9月   7 22:49 test.sh


//使用shell模块在受控机上执行受控机上的脚本
[root@ansible ~]# ansible 172.16.103.129 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
172.16.103.129 | SUCCESS | rc=0 >>


[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'cat /tmp/test'
172.16.103.129 | SUCCESS | rc=0 >>
1
2
3
4
5
6
7
8
9
10
5.ansible常用模块之script

script模块用于在受控机上执行主控机上的脚本

[root@hm1 ~]# ll /etc/ansible/scripts/
总用量 4
-rw-r--r--. 1 root root 61 9月   8 18:59 a.sh
[root@hm1 ~]# ansible 172.16.103.129 -m script -a '/etc/ansible/scripts/a.sh &>/tmp/a'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 172.16.103.129 closed.rn",
    "stderr_lines": [
        "Shared connection to 172.16.103.129 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}


//查看受控机上的/tmp/a文件内容
[root@hm1 ~]# ansible 172.16.103.129 -m shell -a 'cat /tmp/a'
172.16.103.129 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
....此处省略N行
jerry:x:1000:1000::/home/jerry:/bin/bash

//由此可见确是在受控机上执行了主控机上的脚本,且输出记录到了受控机上。因为此处 
//的jerry用户是在受控机上才有的用户
6. ansible常用模块之template

template模块用于生成一个模板,并可将其传输至远程主机上。

[root@hm1 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-SIG-ansible-29.repo  redhat.repo
[root@hm1 ~]# ansible 192.168.70.138 -m template -a 'src=/etc/yum.repos.d/redhat.repo dest=/etc/yum.repos.d/'
192.168.70.138 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "b5dcc866d5312968c494ec06ddd01527163c9f26",
    "dest": "/etc/yum.repos.d/redhat.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "c07c46acc942d53433ccafe16a631548",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 358,
    "src": "/root/.ansible/tmp/ansible-tmp-1653742555.0658815-317993-54476399021047/source",
    "state": "file",
    "uid": 0
}
[root@hm1 ~]# ansible 192.168.70.138 -m command -a 'ls /etc/yum.repos.d/'
192.168.70.138 | CHANGED | rc=0 >>
CentOS-Base.repo
epel-modular.repo
epel.repo
epel-testing-modular.repo
epel-testing.repo
redhat.repo

7. ansible常用模块之yum

yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个

name:要管理的包名
state:要进行的操作
state常用的值:

latest:安装软件
installed:安装软件
present:安装软件 *
removed:卸载软件
absent:卸载软件 *
若想使用yum来管理软件,请确保受控机上的yum源无异常。

//在受控机上查询看vsftpd软件是否安装
[root@hm2 ~]# rpm -qa|grep vsftpd
[root@hm2 ~]#


//在ansible主机上使用yum模块在受控机上安装vsftpd
[root@hm1 ~]# ansible 172.16.103.129 -m yum -a 'name=vsftpd state=present'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "msg": "warning: /var/cache/yum/x86_64/7Server/base/packages/vsftpd-3.0.2-22.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEYnImporting GPG key 0xF4A80EB5:n Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) "n Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5n From       : http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7n",
    "rc": 0,
    "results": [
        "Loaded plugins: product-id, search-disabled-repos, subscription-managernThis system is not registered with an entitlement server. You can use subscription-manager to register.nResolving Dependenciesn--> Running transaction checkn---> Package vsftpd.x86_64 0:3.0.2-22.el7 will be installedn--> Finished Dependency ResolutionnnDependencies Resolvednn================================================================================n Package          Arch             Version                 Repository      Sizen================================================================================nInstalling:n vsftpd           x86_64           3.0.2-22.el7            base           169 knnTransaction Summaryn================================================================================nInstall  1 PackagennTotal download size: 169 knInstalled size: 348 knDownloading packages:nPublic key for vsftpd-3.0.2-22.el7.x86_64.rpm is not installednRetrieving key from http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7nRunning transaction checknRunning transaction testnTransaction test succeedednRunning transactionn  Installing : vsftpd-3.0.2-22.el7.x86_64                                   1/1 n  Verifying  : vsftpd-3.0.2-22.el7.x86_64                                   1/1 nnInstalled:n  vsftpd.x86_64 0:3.0.2-22.el7                                                  nnComplete!n"
    ]
}


//查看受控机上是否安装了vsftpd
[root@hm2 ~]# rpm -qa|grep vsftpd
vsftpd-3.0.2-22.el7.x86_64
8. ansible常用模块之copy
copy模块用于复制文件至远程受控机。

[root@ansible ~]# ls /etc/ansible/scripts/
a.sh
[root@ansible ~]# ansible 172.16.103.129 -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts/'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "checksum": "83f66f804c195247885b013912cf9dc649f36391",
    "dest": "/scripts/a.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "a63e880a932bba1160f329836cbfd730",
    "mode": "0644",
    "owner": "root",
    "size": 61,
    "src": "/root/.ansible/tmp/ansible-tmp-1536406467.26-35192956264311/source",
    "state": "file",
    "uid": 0
}


[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'ls /scripts/'
172.16.103.129 | SUCCESS | rc=0 >>
a.sh
test.sh
9. ansible常用模块之group
group模块用于在受控机上添加或删除组。

//在受控机上添加一个系统组,其gid为306,组名为mysql
[root@ansible ~]# ansible 172.16.103.129 -m group -a 'name=mysql gid=306 state=present'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "gid": 306,
    "name": "mysql",
    "state": "present",
    "system": false
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'grep mysql /etc/group'
172.16.103.129 | SUCCESS | rc=0 >>
mysql:x:306:


//删除受控机上的mysql组
[root@ansible ~]# ansible 172.16.103.129 -m group -a 'name=mysql state=absent'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "name": "mysql",
    "state": "absent"
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'grep mysql /etc/group'
172.16.103.129 | FAILED | rc=1 >>
non-zero return code
10. ansible常用模块之user
user模块用于管理受控机的用户帐号。

//在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@ansible ~]# ansible 172.16.103.129 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 306,
    "home": "/home/mysql",
    "name": "mysql",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 306
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'grep mysql /etc/passwd'
172.16.103.129 | SUCCESS | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin

[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'ls /home'
172.16.103.129 | SUCCESS | rc=0 >>
jerry


//修改mysql用户的uid为366
[root@ansible ~]# ansible 172.16.103.129 -m user -a 'name=mysql uid=366'
172.16.103.129 | SUCCESS => {
    "append": false,
    "changed": true,
    "comment": "",
    "group": 306,
    "home": "/home/mysql",
    "move_home": false,
    "name": "mysql",
    "shell": "/sbin/nologin",
    "state": "present",
    "uid": 366
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'grep mysql /etc/passwd'
172.16.103.129 | SUCCESS | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin


//删除受控机上的mysql用户
[root@ansible ~]# ansible 172.16.103.129 -m user -a 'name=mysql state=absent'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "mysql",
    "remove": false,
    "state": "absent"
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'grep mysql /etc/passwd'
172.16.103.129 | FAILED | rc=1 >>
non-zero return code
11. ansible常用模块之service
service模块用于管理受控机上的服务。

//查看受控机上的vsftpd服务是否启动
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-active vsftpd'
172.16.103.129 | FAILED | rc=3 >>
unknownnon-zero return code

//启动受控机上的vsftpd服务
[root@ansible ~]# ansible 172.16.103.129 -m service -a 'name=vsftpd state=started'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "name": "vsftpd",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        ....此处省略N行
}

//查看受控机上的vsftpd服务是否启动
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-active vsftpd'
172.16.103.129 | SUCCESS | rc=0 >>
active


//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-enabled vsftpd'
172.16.103.129 | FAILED | rc=1 >>
disablednon-zero return code

//设置受控机上的vsftpd服务开机自动启动
[root@ansible ~]# ansible 172.16.103.129 -m service -a 'name=vsftpd enabled=yes'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "enabled": true,
    "name": "vsftpd",
    "status": {
        "ActiveEnterTimestamp": "六 2018-09-08 00:02:39 EDT",
        ....此处省略N行
}

//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-enabled vsftpd'
172.16.103.129 | SUCCESS | rc=0 >>
enabled


//停止受控机上的vsftpd服务
[root@ansible ~]# ansible 172.16.103.129 -m service -a 'name=vsftpd state=stopped'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "name": "vsftpd",
    "state": "stopped",
    "status": {
        "ActiveEnterTimestamp": "六 2018-09-08 00:02:39 EDT",
        ....此处省略N行
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-active vsftpd'
172.16.103.129 | FAILED | rc=3 >>
inactivenon-zero return code

[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'ss -antl'
172.16.103.129 | SUCCESS | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN     0      128          *:22                       *:*
LISTEN     0      100    127.0.0.1:25                       *:*
LISTEN     0      128         :::22                      :::*
LISTEN     0      100        ::1:25                      :::*
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/941523.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号