1. 命令行参数

-v,–verbose   				详细模式,如果命令执行成功,输出详细的结果(-vv –vvv -vvvv)
-i PATH,–inventory=PATH      指定host文件的路径,默认是在/etc/ansible/hosts(生产环境经常用到)
-f NUM,–forks=NU NUM是指定一个整数,默认是5,指定fork开启同步进程的个数。
-m NAME,–module-name=NAME 指定使用的module名称,默认是command
-m DIRECTORY,–module-path=DIRECTORY 指定module的目录来加载module,默认是/usr/share/ansible,
-a,MODULE_ARGS 指定module模块的参数
-k,-ask-pass 提示输入ssh的密码,而不是使用基于ssh的密钥认证
-sudo 指定使用sudo获得root权限(生产环境经常用到)
-K,-ask-sudo-pass 提示输入sudo密码,与–sudo一起使用 (生产环境经常用到)
-u USERNAME,-user=USERNAME 指定移动端的执行用户
-C,-check 测试此命令执行会改变什么内容,不会真正的去执行

2. command模块测试环境

[root@temp01 ~]# ansible all -a "pwd"
192.168.3.70 | SUCCESS | rc=0 >>
/root 192.168.3.60 | SUCCESS | rc=0 >>
/root

 [root@temp01 ~]# ansible all -m command -a "pwd"    #-m command 原来是可以省略的,ansible 默认模块就是command
 192.168.3.70 | SUCCESS | rc=0 >>
 /root

 192.168.3.60 | SUCCESS | rc=0 >>
 /root

3. 再用ping模块

#-f 参数用法
[root@temp01 ~]# ansible all -m ping -f 1    #all 表示hosts里面所有主机 -m 调用模块 -f 指定同步进程数
192.168.3.60 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.3.70 | SUCCESS => {
"changed": false,
"ping": "pong"
}
#-C参数用法
[root@temp01 ~]# ansible all -m ping -f 1 -C  
192.168.3.60 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.3.70 | SUCCESS => {
"changed": false,
"ping": "pong"
}

4. user模块

[root@temp01 ~]# ansible db -m user -a 'name=DBA uid=505 home=/home/dba shell=/sbin/nologin'
192.168.3.70 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 505,
"home": "/home/dba",
"name": "DBA",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 505
}
[root@temp01 ~]# ansible db -m user -a 'name=DBA state=absent'    #state属性有:present表示添加,absend表示删除,state属性默认是present
192.168.3.70 | SUCCESS => {
"changed": true,
"force": false,
"name": "DBA",
"remove": false,
"state": "absent"
}

修改用户密码

[root@temp01 ansible_playbook]# ansible all -m shell -a 'echo "123456" | passwd  foo --stdin'
192.168.3.60 | SUCCESS | rc=0 >>
Changing password for user foo.
passwd: all authentication tokens updated successfully. 192.168.3.70 | SUCCESS | rc=0 >>
Changing password for user foo.
passwd: all authentication tokens updated successfully.

5. 看看下面这个坑

[root@temp01 ~]# ansible all -m cron -a "name=cat minute='*/1' job='ls /root'"
192.168.3.70 | FAILED! => {
"changed": false,
"failed": true,
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}
192.168.3.60 | FAILED! => {
"changed": false,
"failed": true,
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}
[root@temp01 ~]# ansible all -m copy -a "src=/etc/ansible/hosts dest=/root mode=600"
192.168.3.60 | FAILED! => {
"changed": false,
"checksum": "8add7a4e56ae6f7a1c6d0218ce757510d195bb3c",
"failed": true,
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}
192.168.3.70 | FAILED! => {
"changed": false,
"checksum": "8add7a4e56ae6f7a1c6d0218ce757510d195bb3c",
"failed": true,
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}

需要在客户端安装libselinux-python库

方法如下,随便介绍下yum模块,哈哈

[root@temp01 ~]# ansible all -m yum -a 'name=libselinux-python state=present'
192.168.3.70 | SUCCESS => {
"changed": true,
"msg": "warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY\nImporting GPG key 0xC105B9DE:\n Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>\n Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5)\n From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror, security\nLoading mirror speeds from cached hostfile\n * base: mirrors.zju.edu.cn\n * extras: centos.ustc.edu.cn\n * updates: mirrors.zju.edu.cn\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package libselinux-python.x86_64 0:2.0.94-7.el6 will be installed\n--> Processing Dependency: libselinux = 2.0.94-7.el6 for package: libselinux-python-2.0.94-7.el6.x86_64\n--> Running transaction check\n---> Package libselinux.x86_64 0:2.0.94-5.3.el6_4.1 will be updated\n--> Processing Dependency: libselinux = 2.0.94-5.3.el6_4.1 for package: libselinux-utils-2.0.94-5.3.el6_4.1.x86_64\n---> Package libselinux.x86_64 0:2.0.94-7.el6 will be an update\n--> Running transaction check\n---> Package libselinux-utils.x86_64 0:2.0.94-5.3.el6_4.1 will be updated\n---> Package libselinux-utils.x86_64 0:2.0.94-7.el6 will be an update\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n libselinux-python x86_64 2.0.94-7.el6 base 203 k\nUpdating for dependencies:\n libselinux x86_64 2.0.94-7.el6 base 109 k\n libselinux-utils x86_64 2.0.94-7.el6 base 82 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package(s)\nUpgrade 2 Package(s)\n\nTotal download size: 394 k\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal 210 kB/s | 394 kB 00:01 \nRetrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Updating : libselinux-2.0.94-7.el6.x86_64 1/5 \n\r Installing : libselinux-python-2.0.94-7.el6.x86_64 2/5 \n\r Updating : libselinux-utils-2.0.94-7.el6.x86_64 3/5 \n\r Cleanup : libselinux-utils-2.0.94-5.3.el6_4.1.x86_64 4/5 \n\rCleanup : libselinux-2.0.94-5.3.el6_4.1.x86_64 5/5 \n\r Verifying : libselinux-python-2.0.94-7.el6.x86_64 1/5 \n\r Verifying : libselinux-utils-2.0.94-7.el6.x86_64 2/5 \n\r Verifying : libselinux-2.0.94-7.el6.x86_64 3/5 \n\r Verifying : libselinux-2.0.94-5.3.el6_4.1.x86_64 4/5 \n\r Verifying : libselinux-utils-2.0.94-5.3.el6_4.1.x86_64 5/5 \n\nInstalled:\n libselinux-python.x86_64 0:2.0.94-7.el6 \n\nDependency Updated:\n libselinux.x86_64 0:2.0.94-7.el6 libselinux-utils.x86_64 0:2.0.94-7.el6 \n\nComplete!\n"
]
}
192.168.3.60 | SUCCESS => {
"changed": true,
"msg": "warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY\nImporting GPG key 0xC105B9DE:\n Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>\n Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5)\n From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror, security\nLoading mirror speeds from cached hostfile\n * base: mirrors.cn99.com\n * extras: mirrors.cn99.com\n * updates: mirrors.cn99.com\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package libselinux-python.x86_64 0:2.0.94-7.el6 will be installed\n--> Processing Dependency: libselinux = 2.0.94-7.el6 for package: libselinux-python-2.0.94-7.el6.x86_64\n--> Running transaction check\n--->Package libselinux.x86_64 0:2.0.94-5.3.el6_4.1 will be updated\n--> Processing Dependency: libselinux = 2.0.94-5.3.el6_4.1 for package: libselinux-utils-2.0.94-5.3.el6_4.1.x86_64\n---> Package libselinux.x86_64 0:2.0.94-7.el6 will be an update\n--> Running transaction check\n---> Package libselinux-utils.x86_64 0:2.0.94-5.3.el6_4.1 will be updated\n---> Package libselinux-utils.x86_64 0:2.0.94-7.el6 will be an update\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n libselinux-python x86_64 2.0.94-7.el6 base 203 k\nUpdatingfor dependencies:\n libselinux x86_64 2.0.94-7.el6 base 109 k\n libselinux-utils x86_64 2.0.94-7.el6 base 82 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package(s)\nUpgrade 2 Package(s)\n\nTotal download size: 394 k\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal 70 kB/s | 394 kB 00:05 \nRetrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Updating : libselinux-2.0.94-7.el6.x86_64 1/5 \n\r Installing : libselinux-python-2.0.94-7.el6.x86_64 2/5 \n\r Updating : libselinux-utils-2.0.94-7.el6.x86_64 3/5 \n\r Cleanup : libselinux-utils-2.0.94-5.3.el6_4.1.x86_64 4/5 \n\r Cleanup : libselinux-2.0.94-5.3.el6_4.1.x86_64 5/5 \n\r Verifying : libselinux-python-2.0.94-7.el6.x86_64 1/5 \n\r Verifying : libselinux-utils-2.0.94-7.el6.x86_64 2/5 \n\r Verifying : libselinux-2.0.94-7.el6.x86_64 3/5 \n\r Verifying : libselinux-2.0.94-5.3.el6_4.1.x86_64 4/5 \n\r Verifying : libselinux-utils-2.0.94-5.3.el6_4.1.x86_64 5/5 \n\nInstalled:\n libselinux-python.x86_64 0:2.0.94-7.el6 \n\nDependency Updated:\n libselinux.x86_64 0:2.0.94-7.el6 libselinux-utils.x86_64 0:2.0.94-7.el6 \n\nComplete!\n"
]
}

6. service 模块

[root@temp01 ~]# ansible all -m service -a 'name=sshd state=restarted'  #state属性有running,started,stopped,restarted,reloaded
192.168.3.60 | SUCCESS => {
"changed": true,
"name": "sshd",
"state": "started"
}
192.168.3.70 | SUCCESS => {
"changed": true,
"name": "sshd",
"state": "started"
}

7. yum模块还是介绍下

[root@temp01 ~]# ansible all -m yum -a 'name=ntp state=present'    #state属性有absent,present,installed,removed,latest
192.168.3.70 | SUCCESS => {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"ntp-4.2.6p5-1.el6.centos.x86_64 providing ntp is already installed"
]
}
192.168.3.60 | SUCCESS => {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"ntp-4.2.6p5-1.el6.centos.x86_64 providing ntp is already installed"
]
}

8. script模块 远程执行脚本

[root@temp01 ~]# cat ansible.sh              #脚本内容
#!/bin/bash
echo "Hello world" >>/root/ansible.log
[root@temp01 ~]# ansible all -m script -a 'ansible.sh'
192.168.3.70 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "",
"stdout": "",
"stdout_lines": []
}
192.168.3.60 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "",
"stdout": "",
"stdout_lines": []
} [root@example_02 ~]# pwd          #跳转到客户端验证
/root
[root@example_02 ~]# cat ansible.log
Hello world

9. 忘了一个重要的模块shell模块

shell 模块与command模块区别 ,shell支持管道符和变更,下面这个例子说明问题

[root@temp01 ~]# ansible all -m mommand -a 'cat /etc/passwd | grep root'
ERROR! this task 'mommand' has extra params, which is only allowed in the following modules: command, win_command, shell, win_shell, script, include, include_vars, add_host, group_by, set_fact, raw, meta
[root@temp01 ~]# ansible all -m shell -a 'cat /etc/passwd | grep root'
192.168.3.70 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin 192.168.3.60 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

10. file模块  

(1)修改文件属性

[root@temp01 ~]# ansible all -m file -a 'path=/root/ansible.log owner=root mode=777 group=root'      #修改文件属性
192.168.3.60 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "",
"owner": "root",
"path": "/root/ansible.log",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 12,
"state": "file",
"uid": 0
}
192.168.3.70 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "",
"owner": "root",
"path": "/root/ansible.log",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 12,
"state": "file",
"uid": 0
}

(2)新建目录:类 mkdir -p

[root@temp01 ~]# ansible all -m file -a 'dest=/root/1/2/3/4/ mode=755 owner=root group=root state=directory'
192.168.3.60 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "",
"owner": "root",
"path": "/root/1/2/3/4/",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 4096,
"state": "directory",
"uid": 0
}
192.168.3.70 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "",
"owner": "root",
"path": "/root/1/2/3/4/",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 4096,
"state": "directory",
"uid": 0
} [root@example_02 ~]# ll
total 48
-rw-------. 1 root root 1106 Jul 11 05:26 anaconda-ks.cfg
-rwxrwxrwx. 1 root root 12 Dec 30 02:53 ansible.log
-rw-------. 1 root root 1052 Dec 30 01:09 hosts
-rw-r--r--. 1 root root 23089 Jul 11 05:26 install.log
-rw-r--r--. 1 root root 6240 Jul 11 05:24 install.log.syslog
[root@example_02 ~]# cd 1/2/3/4/                        #转到客户端验证
You have new mail in /var/spool/mail/root
[root@example_02 4]# pwd
/root/1/2/3/4

(3)删除目录文件

[root@temp01 ~]# ansible all -m file -a 'dest=/root/1/2/3/4/  state=absent'
192.168.3.60 | SUCCESS => {
"changed": true,
"path": "/root/1/2/3/4/",
"state": "absent"
}
192.168.3.70 | SUCCESS => {
"changed": true,
"path": "/root/1/2/3/4/",
"state": "absent"
} [root@example_02 ~]# cd 1/2/3/4            #跳转验证
-bash: cd: 1/2/3/4: No such file or directory
[root@example_02 ~]#

ansible 基本命令学习与踩坑的更多相关文章

  1. Unity接入九游SDK学习与踩坑

    学习之路漫漫,应修之期远兮.持之以恒,方得始终. 这几日接入九游SDK,于浑浑噩噩中成长. 下面是步骤: 一:下载九游SDK 二:打开Android Studio新建一个工程,并且新建一个Androi ...

  2. Grafana Loki 学习之踩坑记

    转发自:https://mp.weixin.qq.com/s/zfXNEkdDC9Vqd9lh1ptC1g Grafana 出品的 loki 日志框架完美地与 kubernetes 的 label 理 ...

  3. 学习git踩坑之路

    第一次使用git: 1.使用CentOS Linux release 7.3.1611 (Core),内核版本3.10.0-514.el7.x86_64 2.安装git: root权限运行:yum   ...

  4. react基础学习和react服务端渲染框架next.js踩坑

    说明 React作为Facebook 内部开发 Instagram 的项目中,是一个用来构建用户界面的优秀 JS 库,于 2013 年 5 月开源.作为前端的三大框架之一,React的应用可以说是非常 ...

  5. 踩坑学习python自动化测试第二天!

    class put_out(object): def Helloword(str): print(str) Hello,Inder,Pist ="", [],{} word= st ...

  6. react学习 | 踩坑指南

    react样式模块化的"omit -loader"坑 众所周知 react样式的模块化(css modules) 是自己模块中写自己的css,与其他模块互补影响,解决了命名冲突和全 ...

  7. linux ubuntu 学习总结(day01)基本命令学习

    个人总结,请尊重版权,转载请在显眼地方注明出处:https://www.cnblogs.com/sunshine5683/p/9948259.html day(01)基本命令学习 今天开始自学linu ...

  8. 我的微信小程序入门踩坑之旅

    前言 更好的阅读体验请:我的微信小程序入门踩坑之旅 小程序出来也有一段日子了,刚出来时也留意了一下.不过赶上生病,加上公司里也有别的事,主要是自己犯懒,就一直没做.这星期一,赶紧趁着这股热乎劲,也不是 ...

  9. vue+ vue-router + webpack 踩坑之旅

    说是踩坑之旅 其实是最近在思考一些问题 然后想实现方案的时候,就慢慢的查到这些方案   老司机可以忽略下面的内容了 1)起因  考虑到数据分离的问题  因为server是express搭的   自然少 ...

随机推荐

  1. HDU 4003 Find Metal Mineral

    这个题是POJ1849的加强版. 先说一个很重要的结论,下面两种方法都是从这个结论出发的. 一个人从起点遍历一颗树,如果最终要回到起点,走过的最小权值就是整棵树的权值的2倍. 而且K个人的情况也是如此 ...

  2. Mac下Python和Pycharm之virtualenv

    一.python如何配置virtualenv   1.安装virtualenv pip3 install virtualenvpip install -i https://pypi.tuna.tsin ...

  3. Knockout v3.4.0 中文版教程-4-通过监控数组工作

    2.通过监控数组工作 1. 监控数组 如果你想检测或者响应一个对象的改变,你用observables.如果你想检测和响应一个集合的改变,使用observableArray.这个在很多情况下都非常有用, ...

  4. Ribbitmq

    rittbiMQ: 连接远程rabbitmq server sudo rabbitmqctl addser mihon mihon123 sudo rabbitmqctl set_permission ...

  5. IPy模块--IP地址处理

    Python之实用的IP地址处理模块IPy 实用的IP地址处理模块IPy 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等 别担心,Ipy模块拯救你.Ipy ...

  6. appium+python自动化-xpath定位

    基本属性定位 以淘宝app为例,定位左上角扫一扫按钮 1.可以通过text文本定位到 //*[@text='text文本属性'] # 定位text driver.find_element_by_xpa ...

  7. 九度oj 题目1534:数组中第K小的数字

    题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6]. 现在给你数组A和B ...

  8. Unity3D for iOS初级教程:Part 2/3

    转自Unity3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这篇教材是来自教程团队成员 ...

  9. 刷题总结——竞赛得分(ssoj)

    题目: 题目描述 ZZH 在经历了无数次学科竞赛的失败以后,得到了一个真理:做一题就要对一题!但是要完全正确地做对一题是要花很多时间(包括调试时间),而竞赛的时间有限.所以开始做题之前最好先认真审题, ...

  10. [NOIP2011] 洛谷P1313 计算系数

    题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别为 a ,b ,k , ...