草庐IT

Ansible常用模块

tushanbu 2023-03-28 原文

Ansible常用模块


Ansible常用模块详解

ansible常用模块有:

ping
yum
template
copy
user
group
service
raw
command
shell
script
file

ansible常用模块raw、command、shell的区别:

  1. shell模块调用的/bin/sh指令执行
  2. command模块不是调用的shell的指令,所以没有bash的环境变量
  3. raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了

ansible常用模块之ping

//将受控主机加入ansible清单
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg  hosts  roles
[root@ansible ansible]# touch inventory
[root@ansible ansible]# ls
ansible.cfg  hosts  inventory  roles
[root@ansible ansible]# vim ansible.cfg 
#inventory      = /etc/ansible/hosts    //取消注释并修改为下面这样
inventory       = /etc/ansible/inventory
[root@ansible ansible]# vim inventory 
[root@ansible ansible]# cat inventory 
[web]
192.168.222.137
[root@ansible ansible]# ansible all -m ping
192.168.222.137 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

ansible常用模块之command

command模块用于在远程主机上执行命令,ansible默认就是使用command模块。

command模块有一个缺陷就是不能使用管道符和重定向功能。

//查看受控主机的/tmp目录内容
[root@ansible ansible]# ansible 192.168.222.137 -a 'ls /tmp'
192.168.222.137 | CHANGED | rc=0 >>
a
ansible_command_payload_pgp9m_c8
systemd-private-ddc74647b1614d9f97693ed56992353e-nginx.service-LcMflb
vmware-root_901-3988228452
vmware-root_903-3979774182
//在受控主机的/tmp目录下新建一个文件test
[root@ansible ansible]# ansible 192.168.222.137 -a 'touch /tmp/test'
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  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.222.137 | CHANGED | rc=0 >>

[root@ansible ansible]# ansible 192.168.222.137 -a 'ls /tmp'
192.168.222.137 | CHANGED | rc=0 >>
a
ansible_command_payload_l91ypv4n
systemd-private-ddc74647b1614d9f97693ed56992353e-nginx.service-LcMflb
test
vmware-root_901-3988228452
vmware-root_903-3979774182
//command模块不支持管道符,不支持重定向
[root@ansible ansible]# ansible 192.168.222.137 -a "echo 'hello world' > /tmp/test"
192.168.222.137 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible ansible]# ansible 192.168.222.137 -a 'cat /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>

[root@ansible ansible]# ansible 192.168.222.137 -a 'ps -ef|grep vsftpd'
192.168.222.137 | FAILED | rc=1 >>
error: unsupported SysV option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).non-zero return code

ansible常用模块之raw

raw模块用于在远程主机上执行命令,其支持管道符与重定向

[root@ansible ansible]# ansible 192.168.222.137 -m raw -a 'echo "hello world" > /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
Shared connection to 192.168.222.137 closed.

[root@ansible ansible]# ansible 192.168.222.137 -a 'cat /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
hello world
[root@ansible ansible]# ansible 192.168.222.137 -m raw -a 'cat /tmp/test|grep -Eo hello'
192.168.222.137 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.222.137 closed.

ansible常用模块之shell

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

//查看受控机上的脚本
[root@nginx ~]# mkdir /scripts/
[root@nginx ~]# cd /scripts/
[root@nginx scripts]# vim test.sh
[root@nginx scripts]# cat test.sh 
#!/bin/bash
my_arry=$(seq 1 10)
for i in ${my_arry[@]};do
    echo $i
done
[root@nginx scripts]# chmod +x test.sh 
[root@nginx scripts]# cd
[root@nginx ~]# ll /scripts/
total 4
-rwxr-xr-x 1 root root 76 Oct 24 02:33 test.sh
//使用shell模块在主控机上执行受控机上的脚本
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>

[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'cat  /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
1
2
3
4
5
6
7
8
9
10

ansible常用模块之script

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

[root@ansible ansible]# mkdir -p /scripts
[root@ansible ansible]# vim /scripts/test.sh
[root@ansible ansible]# cat /scripts/test.sh 
#!/bin/bash
my_arry=$(seq 1 10)
for i in ${my_arry[@]};do
    echo $i
done

[root@ansible ansible]# ansible 192.168.222.137 -m script -a '/scripts/test.sh'
192.168.222.137 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.222.137 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.222.137 closed."
    ],
    "stdout": "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n",
    "stdout_lines": [
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10"
    ]
}

ansible常用模块之template

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

//将设置好的阿里源传到受控主机
[root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# rm -rf *
[root@ansible yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo     
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   3574      0 --:--:-- --:--:-- --:--:--  3574
[root@ansible yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@ansible yum.repos.d]# cd
[root@ansible ~]# ansible nginx  -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo  dest=/etc/yum.repos.d/CentOS-Base.repo'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
    "dest": "/etc/yum.repos.d/CentOS-Base.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "ed031c350da2532e6a8d09a4d9b05278",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1653,
    "src": "/root/.ansible/tmp/ansible-tmp-1666511143.7368824-130351-128775339422969/source",
    "state": "file",
    "uid": 0
}
//查看受控主机上面
[root@nginx ~]# ls /etc/yum.repos.d/
CentOS-Base.repo

ansible常用模块之yum

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

  1. name:要管理的包名

  2. state:要进行的操作
    state常用的值:

  3. latest:安装软件

  4. installed:安装软件

  5. present:安装软件

  6. removed:卸载软件

  7. absent:卸载软件
    若想使用yum来管理软件,请确保受控机上的yum源无异常。

//在受控机上查询看vsftpd软件是否安装
[root@nginx ~]# rpm -qa|grep vsftpd
[root@nginx ~]# 
//在ansible主机上使用yum模块在受控机上安装vsftpd
[root@ansible ansible]# ansible 192.168.222.137 -m yum -a 'name=vsftpd state=present'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: vsftpd-3.0.3-34.el8.x86_64"
    ]
}
//查看受控机上是否安装了vsftpd
[root@nginx ~]# rpm -qa|grep vsftpd
vsftpd-3.0.3-34.el8.x86_64

ansible常用模块之copy

copy模块用于复制文件至远程受控机。

[root@ansible ansible]# ls xbz
ssh  ssh.sh
[root@ansible ansible]# ansible 192.168.222.137 -m copy -a 'src=/etc/ansible/xbz/ssh.sh dest=/xbz/'192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/xbz/ssh.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "src": "/root/.ansible/tmp/ansible-tmp-1666551783.8760731-1363195-260276005042864/source",
    "state": "file",
    "uid": 0
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'ls /xbz/'
192.168.222.137 | CHANGED | rc=0 >>
ssh.sh

ansible常用模块之group

group模块用于在受控机上添加或删除组。

//在受控机上添加一个系统组,其gid为306,组名为mysql
[root@ansible ansible]# ansible 192.168.222.137 -m group -a 'name=mysql gid=306 state=present'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 306,
    "name": "mysql",
    "state": "present",
    "system": false
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/group'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:306:
//删除受控机上的mysql组
[root@ansible ansible]# ansible 192.168.222.137 -m group -a 'name=mysql state=absent'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "mysql",
    "state": "absent"
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/group'
192.168.222.137 | FAILED | rc=1 >>
non-zero return code

ansible常用模块之user

user模块用于管理受控机的用户帐号。

//在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@ansible ansible]# ansible 192.168.222.137 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "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]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'ls /home'
192.168.222.137 | CHANGED | rc=0 >>
nginx
//修改mysql用户的uid为366
[root@ansible ansible]# ansible 192.168.222.137 -m user -a 'name=mysql uid=366'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "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]# ansible 192.168.222.137  -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
//删除受控机上的mysql用户
[root@ansible ansible]# ansible 192.168.222.137  -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
[root@ansible ansible]# ansible 192.168.222.137 -m user -a 'name=mysql state=absent'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "mysql",
    "remove": false,
    "state": "absent"
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | FAILED | rc=1 >>
non-zero return code

ansible常用模块之service

service模块用于管理受控机上的服务。

//查看受控机上的vsftpd服务是否启动
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-active vsftpd'
192.168.222.137 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ansible]# ansible 192.168.222.137 -m service -a 'name=vsftpd state=started'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "vsftpd",
    "state": "started",
    "status": {
        "ActiveState": "inactive",
//查看受控机上的vsftpd服务是否启动
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-active vsftpd'
192.168.222.137 | CHANGED | rc=0 >>
active
//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-enabled vsftpd'
192.168.222.137 | FAILED | rc=1 >>
disablednon-zero return code
//设置受控机上的vsftpd服务开机自动启动
[root@ansible ansible]# ansible 192.168.222.137 -m service -a 'name=vsftpd enabled=yes'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-enabled vsftpd'
192.168.222.137 | CHANGED | rc=0 >>
enabled
//停止受控机上的vsftpd服务
[root@ansible ansible]# ansible 192.168.222.137 -m service -a 'name=vsftpd state=stopped'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-active vsftpd'
192.168.222.137 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'ss -antl'
192.168.222.137 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:80        0.0.0.0:*          
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*          

有关Ansible常用模块的更多相关文章

  1. ruby - 在 Ruby 中使用匿名模块 - 2

    假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

  2. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  5. ruby - 当使用::指定模块时,为什么 Ruby 不在更高范围内查找类? - 2

    我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or

  6. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  7. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  8. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  9. ruby-on-rails - Controller 中的 Rails 辅助模块 - 2

    我有一个Controller,我想为这个Controller创建一个助手,我可以在不包含它的情况下使用它。我尝试像这样创建一个与Controller同名的助手classCars::EnginesController我创建的助手是moduleCars::EnginesHelperdefcheck_fuellogger.debug("chekingfuel")endend我得到的错误是undefinedlocalvariableormethod`check_fuel'for#有没有我遗漏的约定? 最佳答案 如果你真的想在Controll

  10. ruby-on-rails - 具有同名的模块和类 - 2

    我有一个模块stat存在于目录结构中:lib/stat_creator/stat/在lib/stat_creator/stat.rb中,我在lib/stat_creator/stat/目录中有我需要的文件,以及:moduleStatCreatormoduleStatendend当我使用该模块时,我将这些类称为StatCreator::Stat::Foo.new现在我想要一个存在于应用程序中的根Stat类。我在app/models中制作了我的Stat类,并在routes.rb中进行了设置。但是,如果我转到Rails控制台并尝试在应用程序/模型中使用Stat类,例如:Stat.by_use

随机推荐