参考官方链接:

https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html#parallelism-and-shell-commands

  • shell模块使用

    提权操作:

[root@ansible-test ~]# ansible test1 -m shell -a "ls /root" -u zhouyuqiang -k --become -K

SSH password:

SUDO password[defaults to SSH password]:

test1 | CHANGED | rc=0 >>

anaconda-ks.cfg

GateOne-master

master.zip

  • copy操作:

    官方:

    $ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"

实际:

[root@ansible-test ~]# ansible web1 -m copy -a "src=/root/redis-5.0.3.tar.gz dest=/usr/local/src"

查询:

[root@ansible-test ~]# ansible web1 -m shell -a "ls /usr/local/src"

172.16.188.191 | CHANGED | rc=0 >>

redis-5.0.3.tar.gz


  • file模块:

创建文件夹:

state 一定需要指定:

状态:

创建文件夹:directory

(递归)删除文件:absent

创建一个文件:touch

[root@ansible-test ansible-yaml]# ansible web -m file -a "dest=/etc/yum.repos.d/nginx.repo state=absent"

test2 | CHANGED => {

"changed": true,

"path": "/etc/yum.repos.d/nginx.repo",

"state": "absent"

}

test1 | CHANGED => {

"changed": true,

"path": "/etc/yum.repos.d/nginx.repo",

"state": "absent"

}

test3 | CHANGED => {

"changed": true,

"path": "/etc/yum.repos.d/nginx.repo",

"state": "absent"

}


  • yum模块

state 一定需要

状态:

具体版本:present

最新版本:latest

name 是指定需要安装的包名

[root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=present"

卸载一个版本:

指定状态就可以:absent

[root@ansible-test opt]# ansible web1 -m yum -a "name=memcached state=absent"


  • user模块

    创建一个用户并设置密码:

    [root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=1"

查询:

[root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=1"

删除用户:

指定状态即可:absent

[root@ansible-test ~]# ansible web1 -m user -a "name=user1 state=absent"

指定创建用户不能登录,一般是服务用户:

[root@ansible-test ~]# ansible web1 -m user -a "name=user1 password=123 shell=/sbin/nologin "

如下:

user1❌1002:1003::/home/user1:/sbin/nologin


  • git 模块

    需要指定用户:-uroot

    repo指定git仓库地址

    目标主机需要有git命令,没有进行安装

    [root@ansible-test ~]# ansible web1 -m yum -a "name=git state=latest"

[root@ansible-test ~]# ansible web1 -vvvv -m git -a "repo=https://github.com/ansible/ansible.git dest=/opt/ansibleå-source" -uroot


  • Managing Services启动服务模块

    启动服务:

    state=started

    state=started

    [root@ansible-test ~]# ansible web -m service -a "name=memcached state=started" -uroot

关闭服务:

state=stopped

[root@ansible-test ~]# ansible web -m service -a "name=memcached state=stopped" -uroot

重新启动:

state=restarted

[root@ansible-test ~]# ansible web -m service -a "name=memcached state=restarted" -uroot

加入开机启动:

enabled=true

[root@ansible-test ~]# ansible web -m service -a "name=memcached enabled=true" -uroot


  • setup模块

    [root@ansible-test ~]# ansible web -m setup

如:

"ansible_system_capabilities_enforced": "True",

"ansible_system_vendor": "VMware, Inc.",

"ansible_uptime_seconds": 82564,

"ansible_user_dir": "/root",

"ansible_user_gecos": "root",

"ansible_user_gid": 0,

"ansible_user_id": "root",

"ansible_user_shell": "/bin/bash",

"ansible_user_uid": 0,

"ansible_userspace_architecture": "x86_64",

"ansible_userspace_bits": "64",

"ansible_virtualization_role": "guest",

  进行过滤操作:

  "filter=ansible_user_shell"

  [root@ansible-test ~]# ansible web -m setup  -a "filter=ansible_user_shell"

test2 | SUCCESS => {

"ansible_facts": {

"ansible_user_shell": "/bin/bash"

    [root@ansible-test ~]# ansible web -m setup  -a "filter=ansible_all_ipv4_addresses"

test2 | SUCCESS => {

"ansible_facts": {

"ansible_all_ipv4_addresses": [

"172.16.188.191"

]

},

"changed": false

}

可以通过统配符进行匹配:

如:filter=ipv4

[root@ansible-test ~]# ansible web -m setup -a "filter=ipv4"

test2 | SUCCESS => {

"ansible_facts": {

"ansible_all_ipv4_addresses": [

"172.16.188.191"

],

"ansible_default_ipv4": {

"address": "172.16.188.191",

"alias": "eth0",

"broadcast": "172.16.188.255",

"gateway": "172.16.188.1",


  • playbook的模块

yaml的语法,采用yaml语法,具体语法其实很简单,主要是空格区分目录。每个目录层级是2个空格,每个:后面一定要需要空格,否则语法报错

ansible的模块使用说明的更多相关文章

  1. Ansible 常见模块介绍

    目录 Ansible 常见模块介绍 ping 模块 command 模块 cron 模块 user 模块 group 模块 copy 模块 file 模块 service 模块 shell 模块 sc ...

  2. ansible服务模块和组模块使用

    本篇文章主要是介绍ansible服务模块和组模块的使用. 主要模块为ansible service module和ansible group moudle,下面的内容均是通过实践得到,可以直接运行相关 ...

  3. ansible常用模块用法

    ansible常用模块用法 2015-07-21 10:25 24458人阅读 评论(1) 收藏 举报  分类: Linux(44)   ansible 版权声明:本文为博主原创文章,未经博主允许不得 ...

  4. Linux之ansible 常用模块

    目的 代码发布系统 代码发布:把本地的代码通过某些方式弄到线上,可以供别人访问 部署 前戏 ansible 批量在远程主机上执行命令 puppet ruby ansible saltstack pyt ...

  5. Ansible常用模块介绍及使用(week5_day1_part2)--技术流ken

    Ansible模块 在上一篇博客<Ansible基础认识及安装使用详解(一)--技术流ken>中以及简单的介绍了一下ansible的模块.ansible是基于模块工作的,所以我们必须掌握几 ...

  6. Ansible常用模块命令

    Ansible常用模块命令 一.安装ansible yum install epel-release yum install ansible 二.配置文件配置 vi /etc/ansible/ansi ...

  7. ansible 常用模块的使用

    安装 yum -y install ansible 配置文件/etc/ansible/hosts 模块介绍与使用 ping模块 [root@node1 config]# ansible k8s -m ...

  8. Ansible 常用模块(一)

    一.Ansible简介 Ansible是新出现的自动化运维工具,基于python开发,集合了众多运维工具(puppet(ruby).cfengine.chef.func.fabric.)的优点,实现了 ...

  9. ansible核心模块playbook介绍

    ansible的playbook采用yaml语法,它简单地实现了json格式的事件描述.yaml之于json就像markdown之于html一样,极度简化了json的书写.在学习ansible pla ...

随机推荐

  1. Android文件的流操作工具类

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...

  2. HTML 介绍及标签

    HTML htyper text markup language 即超文本标记语言 超文本: 就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素. 标记语言: 标记(标签)构成的语言. 标签 ...

  3. 如何为你的 Vue 项目添加配置 Stylelint

    如何为你的 Vue 项目添加配置 Stylelint 现在已经是 9102 年了,网上许多教程和分享帖都已经过期,照着他们的步骤来会踩一些坑,如 stylelint-processor-html 已经 ...

  4. 个人练习:使用HTML+CSS制作二级菜单

    最近一直在学习HTML+CSS,刚看完如果制作下拉菜单部分,就想着做一个练练手. 先上成品图: 就是上面这个效果,横向菜单选项能点击,鼠标放在上面也能展开二级菜单,二级菜单也能点击,点击后就会在底下的 ...

  5. Python变量和简单数据类型

    变量的命名和使用 在Python中使用变量时 ,需要遵守一定的规则和指南. 变量名只能包含字母‘数字和下划线 变量名不能包含空格,但可以用下划线分割其中单词 不要将Python关键字和函数名用作变量名 ...

  6. Android为TV端助力 关于JNI的使用方法

    1首先在java里面定义你需要的native方法 2打开cmd,进入doc窗口,如果是android项目就进入到你当前项目的bin目录下,在doc里面输入cd E:\workspace\Test1 也 ...

  7. [Android][Framework]裁剪SystemServer服务以及关闭SystemFeature

    本文链接 http://wossoneri.github.io/2018/08/30/[Android][Framework]crop-SystemServer-and-SystemFeature/ ...

  8. CsQuery中文编码乱码问题

    一.问题描述 InnerHTML 中文显示为Модель 二.解决方法 在初始化CQ对象前,先设置执行以下语句: CsQuery.Config.HtmlEncoder = CsQuery.HtmlEn ...

  9. flask based on tornado

    from flask import Flask from tornado.wsgi import WSGIContainer from tornado.httpserver import HTTPSe ...

  10. mysql练习----SUM and COUNT/zh图(二)

    世界国家概况 GROUP BY 和 HAVING 通过包括一个GROUP BY子句功能, SUM并将COUNT 其应用于共享值的项目组.当你指定 GROUP BY continent 结果是每个不同的 ...