-a MODULE_ARGS, --args=MODULE_ARGS
-m MODULE_NAME, --module-name=MODULE_NAME
1.测试ping
[root@eddy ~]# ansible all -m ping
eddy | success >> {
"changed": false,
"ping": "pong"
}
[root@eddy ~]# ansible all -m ping -u eddy
#以用户eddy去ping所有主机
[root@eddy ~]# ansible all -m ping -u eddy --sudo
#以用户eddy用root身份去ping所有主机
2.查看模块帮助
可以通过ansible-doc进行查看
[root@eddy ~]# ansible-doc shell
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
> SHELL
The [shell] module takes the command name followed by a list of
space-delimited arguments. It is almost exactly like the [command]
module but runs the command through a shell (`/bin/sh') on the
remote node.
Options (= is mandatory):
- chdir
cd into this directory before running the command [Default:
None]
- creates
a filename, when it already exists, this step will *not* be
run. [Default: None]
- executable
change the shell used to execute the command. Should be an
absolute path to the executable. [Default: None]
= free_form
The shell module takes a free form command to run, as a
string. There's not an actual option named "free form". See
the examples! [Default: None]
- removes
a filename, when it does not exist, this step will *not* be
run. [Default: None]
- warn
if command warnings are on in ansible.cfg, do not warn about
this particular line if set to no/false. [Default: True]
Notes: If you want to execute a command securely and predictably, it may be
better to use the [command] module instead. Best practices
when writing playbooks will follow the trend of using
[command] unless [shell] is explicitly required. When running
ad-hoc commands, use your best judgement. To sanitize any
variables passed to the shell module, you should use "{{ var |
quote }}" instead of just "{{ var }}" to make sure they don't
include evil things like semicolons.
EXAMPLES:
# Execute the command in remote shell; stdout goes to the specified
# file on the remote.
- shell: somescript.sh >> somelog.txt
# Change the working directory to somedir/ before executing the command.
- shell: somescript.sh >> somelog.txt chdir=somedir/
# You can also use the 'args' form to provide the options. This command
# will change the working directory to somedir/ and will only run when
# somedir/somelog.txt doesn't exist.
- shell: somescript.sh >> somelog.txt
args:
chdir: somedir/
creates: somelog.txt
3.并行性和shell命令
[root@eddy ~]# ansible eddy -a '/sbin/reboot' -f 10 重启eddy组下的所有主机10次 默认情况下使用command模块,但不支持管道和shell变量,shell模块支持
[root@eddy ~]# ansible eddy -m shell -a 'echo $PATH'
[root@eddy ~]# ansible eddy -m command -a 'uptime'
eddy | success | rc=0 >>
11:34:27 up 17 days, 22:43, 2 users, load average: 0.08, 0.02, 0.01
等价于
[root@eddy ~]# ansible eddy -a 'uptime'
eddy | success | rc=0 >>
11:34:44 up 17 days, 22:43, 2 users, load average: 0.06, 0.02, 0.00
4.传输文件
[root@eddy ~]# ansible eddy -m copy -a 'src=/etc/hosts dest=/opt/hosts'
eddy | success >> {
"changed": true,
"checksum": "4cd5e5c28fa400e428c592f89b05e2d3feadb3e0",
"dest": "/opt/hosts",
"gid": 0,
"group": "root",
"md5sum": "6acaf54c3f3c419e70ce09e6dc7956f7",
"mode": "0644",
"owner": "root",
"size": 152,
"src": "/root/.ansible/tmp/ansible-tmp-1447823306.59-157076705372231/source",
"state": "file",
"uid": 0
}
5.修改权限
[root@eddy ~]# ll /opt/hosts
-rw-r--r-- 1 root root 152 Nov 18 13:08 /opt/hosts
[root@eddy ~]# ansible eddy -m file -a 'dest=/opt/hosts mode=600'
eddy | success >> {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0600",
"owner": "root",
"path": "/opt/hosts",
"size": 152,
"state": "file",
"uid": 0
}
[root@eddy ~]# ll /opt/hosts
-rw------- 1 root root 152 Nov 18 13:08 /opt/hosts
创建目录
[root@eddy ~]# ansible eddy -m file -a 'dest=/opt/eddy/yys/linux mode=755 owner=eddy group=eddy state=directory'
eddy | success >> {
"changed": true,
"gid": 500,
"group": "eddy",
"mode": "0755",
"owner": "eddy",
"path": "/opt/eddy/yys/linux",
"size": 4096,
"state": "directory",
"uid": 500
}
删除文件或者目录
[root@eddy ~]# ansible eddy -m file -a 'dest=/opt/eddy/yys/linux state=absent'
eddy | success >> {
"changed": true,
"path": "/opt/eddy/yys/linux",
"state": "absent"
}
[root@eddy ~]# ansible eddy -m file -a 'dest=/opt/hosts state=absent'
eddy | success >> {
"changed": true,
"path": "/opt/hosts",
"state": "absent"
}
6.管理软件包
EXAMPLES:
- name: install the latest version of Apache
yum: name=httpd state=latest
- name: remove the Apache package
yum: name=httpd state=absent
- name: install the latest version of Apache from the testing repo
yum: name=httpd enablerepo=testing state=present
- name: install one specific version of Apache
yum: name=httpd-2.2.29-1.4.amzn1 state=present
- name: upgrade all packages
yum: name=* state=latest
- name: install the nginx rpm from a remote repo
yum: name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present
- name: install nginx rpm from a local file
yum: name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present
- name: install the 'Development tools' package group
yum: name="@Development tools" state=present
[root@eddy ~]# ansible eddy -m yum -a 'name=httpd state=present'
确认httpd已经安装,但不更新
[root@eddy ~]# rpm -qa|grep httpd
[root@eddy ~]# ansible eddy -m yum -a 'name=httpd state=present'
eddy | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: security\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-45.el6.centos will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-45.el6.centos for package: httpd-2.2.15-45.el6.centos.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-45.el6.centos.x86_64\n--> Running transaction check\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-45.el6.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.2.15-45.el6.centos centos 829 k\nInstalling for dependencies:\n apr-util-ldap x86_64 1.3.9-3.el6_0.1 centos 15 k\n httpd-tools x86_64 2.2.15-45.el6.centos centos 77 k\n\nTransaction Summary\n================================================================================\nInstall 3 Package(s)\n\nTotal download size: 921 k\nInstalled size: 3.1 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal 1.1 MB/s | 921 kB 00:00 \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : httpd-tools-2.2.15-45.el6.centos.x86_64 1/3 \n\r Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64 2/3 \n\r Installing : httpd-2.2.15-45.el6.centos.x86_64 3/3 \n\r Verifying : httpd-2.2.15-45.el6.centos.x86_64 1/3 \n\r Verifying : apr-util-ldap-1.3.9-3.el6_0.1.x86_64 2/3 \n\r Verifying : httpd-tools-2.2.15-45.el6.centos.x86_64 3/3 \n\nInstalled:\n httpd.x86_64 0:2.2.15-45.el6.centos \n\nDependency Installed:\n apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 \n httpd-tools.x86_64 0:2.2.15-45.el6.centos \n\nComplete!\n"
]
}
[root@eddy ~]# rpm -qa|grep httpd
httpd-tools-2.2.15-45.el6.centos.x86_64
httpd-2.2.15-45.el6.centos.x86_64
[root@eddy ~]# ansible eddy -m yum -a 'name=httpd-2.2 state=present'
确认安装一个特定版本
ansible eddy -m yum -a 'name=httpd-2.2 state=last'
确认安装最新版本
[root@eddy ~]# ansible eddy -m yum -a 'name=httpd state=absent'
卸载一个已安装
[root@eddy ~]# ansible eddy -m yum -a 'name=httpd state=absent'
eddy | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: security\nSetting up Remove Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-45.el6.centos will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n httpd x86_64 2.2.15-45.el6.centos @centos 2.9 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package(s)\n\nInstalled size: 2.9 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Erasing : httpd-2.2.15-45.el6.centos.x86_64 1/1 \n\r Verifying : httpd-2.2.15-45.el6.centos.x86_64 1/1 \n\nRemoved:\n httpd.x86_64 0:2.2.15-45.el6.centos \n\nComplete!\n"
]
}
7.服务管理
root@eddy ~]# ansible eddy -m service -a 'name=nginx state=running,started,stopped,restarted,reloaded'
8.搜索系统信息
[root@eddy ~]# ansible eddy -m setup
eddy | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.162.61.94",
"112.124.120.242"
],
"ansible_all_ipv6_addresses": [],
"ansible_architecture": "x86_64",
"ansible_bios_date": "12/16/2014",
"ansible_bios_version": "4.0.1",
"ansible_cmdline": {
"KEYBOARDTYPE": "pc",
"KEYTABLE": "us",
"LANG": "en_US.UTF-8",
"SYSFONT": "latarcyrheb-sun16",
"quiet": true,
"rd_NO_DM": true,
"rd_NO_LUKS": true,
"rd_NO_LVM": true,
"rd_NO_MD": true,
"rhgb": true,
"ro": true,
"root": "UUID=94e4e384-0ace-437f-bc96-057dd64f42ee"
},
"ansible_date_time": {
"date": "2015-11-18",
"day": "18",
"epoch": "1447825984",
"hour": "13",
"iso8601": "2015-11-18T05:53:04Z",
"iso8601_micro": "2015-11-18T05:53:04.737741Z",
"minute": "53",
"month": "11",
"second": "04",
"time": "13:53:04",
"tz": "CST",
"tz_offset": "+0800",
"weekday": "Wednesday",
"year": "2015"
},
"ansible_default_ipv4": {
"address": "X.X.X.X",
"alias": "eth1",
"gateway": "X.X.X.X",
"interface": "eth1",
"macaddress": "00:16:3e:00:40:1c",
"mtu": 1500,
"netmask": "X.X.X.X",
"network": "X.X.X.X",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_devices": {
"xvda": {
"holders": [],
"host": "",
"model": null,
"partitions": {
"xvda1": {
"sectors": "41940992",
"sectorsize": 512,
"size": "20.00 GB",
"start": "2048"
}
},
"removable": "0",
"rotational": "0",
"scheduler_mode": "cfq",
"sectors": "41943040",
"sectorsize": "512",
"size": "20.00 GB",
"support_discard": "0",
"vendor": null
}
},
"ansible_distribution": "CentOS",
"ansible_distribution_major_version": "6",
"ansible_distribution_release": "Final",
"ansible_distribution_version": "6.5",
"ansible_domain": "",
"ansible_env": {
"CVS_RSH": "ssh",
"G_BROKEN_FILENAMES": "1",
"HOME": "/root",
"LANG": "C",
"LC_CTYPE": "C",
"LESSOPEN": "|/usr/bin/lesspipe.sh %s",
"LOGNAME": "root",
"MAIL": "/var/mail/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin",
"PWD": "/root",
"SHELL": "/bin/bash",
"SHLVL": "2",
"SSH_CLIENT": "X.X.X.X 44220 XXXX",
"SSH_CONNECTION": "X.X.X.X 44220 X.X.X.X XXXX",
"USER": "root",
"_": "/usr/bin/python"
},
"ansible_eth0": {
"active": true,
"device": "eth0",
"ipv4": {
"address": "X.X.X.X",
"netmask": "X.X.X.X",
"network": "X.X.X.X"
},
"macaddress": "00:16:3e:00:0f:62",
"module": "xen_netfront",
"mtu": 1500,
"promisc": false,
"type": "ether"
},
"ansible_eth1": {
"active": true,
"device": "eth1",
"ipv4": {
"address": "X.X.X.X",
"netmask": "X.X.X.X",
"network": "X.X.X.X"
},
"macaddress": "00:16:3e:00:40:1c",
"module": "xen_netfront",
"mtu": 1500,
"promisc": false,
"type": "ether"
},
"ansible_fips": false,
"ansible_form_factor": "Other",
"ansible_fqdn": "eddy",
"ansible_hostname": "eddy",
"ansible_interfaces": [
"lo",
"eth1",
"eth0"
],
"ansible_kernel": "2.6.32-431.23.3.el6.x86_64",
"ansible_lo": {
"active": true,
"device": "lo",
"ipv4": {
"address": "X.X.X.X",
"netmask": "X.X.X.X",
"network": "X.X.X.X"
},
"mtu": 16436,
"promisc": false,
"type": "loopback"
},
"ansible_lsb": {
"codename": "Final",
"description": "CentOS release 6.5 (Final)",
"id": "CentOS",
"major_release": "6",
"release": "6.5"
},
"ansible_machine": "x86_64",
"ansible_memfree_mb": 138,
"ansible_memory_mb": {
"nocache": {
"free": 761,
"used": 233
},
"real": {
"free": 138,
"total": 994,
"used": 856
},
"swap": {
"cached": 0,
"free": 0,
"total": 0,
"used": 0
}
},
"ansible_memtotal_mb": 994,
"ansible_mounts": [
{
"device": "/dev/xvda1",
"fstype": "ext4",
"mount": "/",
"options": "rw,barrier=0",
"size_available": 17495089152,
"size_total": 21136797696,
"uuid": "94e4e384-0ace-437f-bc96-057dd64f42ee"
}
],
"ansible_nodename": "eddy",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
"ansible_processor": [
"GenuineIntel",
"Intel(R) Xeon(R) CPU E5-2630 0 @ 2.30GHz"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 1,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 1,
"ansible_product_name": "HVM domU",
"ansible_product_serial": "8ffbe0b4-0f8e-4341-af50-6166468d70f6",
"ansible_product_uuid": "8FFBE0B4-0F8E-4341-AF50-6166468D70F6",
"ansible_product_version": "4.0.1",
"ansible_python_version": "2.6.6",
"ansible_selinux": false,
"ansible_ssh_host_key_dsa_public": "AAAAB3NzaC1kc3MAAACBAJKuO5QUWmpOv5tVL24HJWQkASwuLnqxISxLZlfUakbUtOOzZPqJCdyRCjrs+8CtE5yfrq2BdIA4KRmXNJiz7attp6HHz0zDzAeBbUO/ttt3SpeIhEzX1e5pUorinoIpD5K1DghLaDk9s1nY+pHyxFkI5hrtPF35PbKZDP38WtRvAAAAFQDvcLJa2GAhGoGmcVfi9F0lnkgGtwAAAIBZCo1E/LL5SBPpy2gcCH00/VXyvWkRKuHpcTJc6YSlvaWttOLqQf21an80EoTGvVJxThKKjIOf1lXFqtrqkS2EVIc2cuDOVszC70NqNw0ou8L9VOiLDXHuXAElar/Ic3sgQhPpzzUWDzMIBB6YLqyWKfYorMSEAWP0/bSNSJYB9gAAAIA2qLHzF52/scTIscC60RCgE2K3YYPCI4p61YNmYuNgyjGeSVH+4vD1JI4czbxKbLZHqpnbN3LrMuQg/6MjfFkUeH5X5RmtUCd5hlPsky06Si29ltrFyDVXnykr4/g+g6P7T7U8ojgRnA8mvrcQthizZG9aflk3OzgYhVQSUJiAbA==",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAABIwAAAQEAprbgXfwUE2ZIQ1GsO9fOFb46oZGJ4V1+7nthfFvof8dbS1ll8cTxzvC6RPbSmZFwwbA47jt6wf2fTlTW7iHcR7atx6SPj5l0BODFqw6+2q4PxebCN65XuigX8p+gphGhe5exh9i6sPl7zRGGPeKdLNnZp17DyqumnsB6zRiWU24QmFMepldlgDvK8ETVYHAD+/GjKYrqfJYb6u5+F8VR7FsCfuNGTC41dVt4vGx8xdIboRbDnrzODm16rWpDGf1cNmfjjaCVuR9bF1ZmS89Y94BTVkwXgpICYmL7uUS2TaDHtM9MJ5Az+j/k+YnSnS0a8LcTljXIhqi5HgFH3U61Nw==",
"ansible_swapfree_mb": 0,
"ansible_swaptotal_mb": 0,
"ansible_system": "Linux",
"ansible_system_vendor": "Xen",
"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",
"ansible_virtualization_type": "xen",
"module_setup": true
},
"changed": false
}
[root@eddy ~]# ansible eddy -m setup --tree /tmp/facts
把信息以主机名的方式存储到/tmp/facts目录下
[root@eddy ~]# ansible eddy -m setup -a 'filter=ansible_*_mb'
搜索内存信息
eddy | success >> {
"ansible_facts": {
"ansible_memfree_mb": 136,
"ansible_memory_mb": {
"nocache": {
"free": 760,
"used": 234
},
"real": {
"free": 136,
"total": 994,
"used": 858
},
"swap": {
"cached": 0,
"free": 0,
"total": 0,
"used": 0
}
},
"ansible_memtotal_mb": 994,
"ansible_swapfree_mb": 0,
"ansible_swaptotal_mb": 0
},
"changed": false
}
[root@eddy ~]# ansible eddy -m setup -a 'filter=ansible_eth[0-9]'
搜索网卡信息
eddy | success >> {
"ansible_facts": {
"ansible_eth0": {
"active": true,
"device": "eth0",
"ipv4": {
"address": "X.X.X.X",
"netmask": "X.X.X.X",
"network": "X.X.X.X"
},
"macaddress": "00:16:3e:00:0f:62",
"module": "xen_netfront",
"mtu": 1500,
"promisc": false,
"type": "ether"
},
"ansible_eth1": {
"active": true,
"device": "eth1",
"ipv4": {
"address": "X.X.X.X",
"netmask": "X.X.X.X",
"network": "X.X.X.X"
},
"macaddress": "00:16:3e:00:40:1c",
"module": "xen_netfront",
"mtu": 1500,
"promisc": false,
"type": "ether"
}
},
"changed": false
}
转载于:https://my.oschina.net/eddylinux/blog/531969