2.提升为sudoer
root@jlive:~#ansible
linux -m shell -a "echo 'foo ALL=(ALL)
ALL' >/etc/sudoers.d/foo"
root@jlive:~#ansible
linux -a "cat /etc/sudoers.d/foo"
192.168.8.150 | success | rc=0 >>
foo ALL=(ALL) ALL
192.168.8.100 | success | rc=0 >>
foo ALL=(ALL) ALL
3.测试sudoer
root@jlive:~#ansible
linux -m shell -a 'uname -a' --sudo --sudo-user=foo
--ask-sudo-pass
sudo password:
192.168.8.150 | success | rc=0 >>
Linux localhost 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15
03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
192.168.8.100 | success | rc=0 >>
Linux ct7.example.com 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6
11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
File
Transfer
1.copy文件到远程主机--copy
root@jlive:~#ansible
linux -m
copy -a "src=/etc/hosts
dest=/etc/hosts"
192.168.8.150 | success >> {
"changed":
true,
"dest":
"/etc/hosts",
"gid":
0,
"group":
"root",
"md5sum":
"2f131edf44c364aeac30910565134633",
"mode":
"0644",
"owner":
"root",
"size":
315,
"src":
"/root/.ansible/tmp/ansible-tmp-1453391426.44-3002780506597/source",
"state":
"file",
"uid": 0
}
192.168.8.100 | success >> {
"changed":
true,
"dest":
"/etc/hosts",
"gid":
0,
"group":
"root",
"md5sum":
"2f131edf44c364aeac30910565134633",
"mode":
"0600",
"owner":
"root",
"secontext":
"system_u:object_r:net_conf_t:s0",
"size":
315,
"src":
"/root/.ansible/tmp/ansible-tmp-1453391426.58-11006037536347/source",
"state":
"file",
"uid": 0
}
-
src
Local path
to a file to copy to the remote server; can be absolute or
relative. If path is a directory, it is copied recursively. In this case, if path
ends
with "/",
only inside contents of that directory are copied to destination.
Otherwise, if it does not end with "/", the directory itself with
all contents
is copied.
This behavior is similar to Rsync.
2.修改文件权限--file
root@jlive:~#ansible
linux -m
file -a
"dest=/etc/hosts mode=644 owner=root
group=root"
192.168.8.100 | success >> {
"changed":
true,
"gid":
0,
"group":
"root",
"mode":
"0644",
"owner":
"root",
"path":
"/etc/hosts",
"secontext":
"system_u:object_r:net_conf_t:s0",
"size":
315,
"state":
"file",
"uid": 0
}
192.168.8.150 | success >> {
"changed":
false,
"gid":
0,
"group":
"root",
"mode":
"0644",
"owner":
"root",
"path":
"/etc/hosts",
"size":
315,
"state":
"file",
"uid": 0
}
3.创建目录
root@jlive:~#ansible
linux -m file -a "dest=/tmp/test/foo mode=755 owner=foo
group=foo state=directory"
192.168.8.150 | success >> {
"changed":
true,
"gid":
1001,
"group":
"foo",
"mode":
"0755",
"owner":
"foo",
"path":
"/tmp/test/foo",
"size":
4096,
"state":
"directory",
"uid": 1001
}
192.168.8.100 | success >> {
"changed":
true,
"gid":
1000,
"group":
"foo",
"mode":
"0755",
"owner":
"foo",
"path":
"/tmp/test/foo",
"secontext":
"unconfined_u:object_r:user_tmp_t:s0",
"size":
6,
"state":
"directory",
"uid": 1000
}
4.递归删除文件
root@jlive:~#ansible
linux -m file -a
"dest=/tmp/test/foo state=absent"
192.168.8.150 | success >> {
"changed":
true,
"path":
"/tmp/test/foo",
"state": "absent"
}
192.168.8.100 | success >> {
"changed":
true,
"path":
"/tmp/test/foo",
"state": "absent"
}
Manage
Packages
1.yum
root@jlive:~#ansible
centos -m
yum -a
"name=screen state=present"
192.168.8.100 | success >> {
"changed":
true,
"msg":
"",
"rc":
0,
"results": [
"Loaded plugins: fastestmirror
Loading mirror
speeds from cached hostfile
Resolving Dependencies
--> Running
transaction check
---> Package screen.x86_64
0:4.1.0-0.19.20120314git3c2946.el7 will be installed
-->
Finished Dependency Resolution
Dependencies
Resolved
================================================================================
Package Arch
Version
Repository
Size
================================================================================
Installing:
screen
x86_64
4.1.0-0.19.20120314git3c2946.el7
centos7-media
550 k
Transaction
Summary
================================================================================
Install
1 Package
Total download size: 550 k
Installed size: 914
k
Downloading packages:
Running transaction check
Running
transaction test
Transaction test succeeded
Running
transaction
Installing :
screen-4.1.0-0.19.20120314git3c2946.el7.x86_64
1/1
Verifying :
screen-4.1.0-0.19.20120314git3c2946.el7.x86_64
1/1
Installed:
screen.x86_64
0:4.1.0-0.19.20120314git3c2946.el7
Complete!
"
]
}
state属性
present #安装
latest #更新到最新
absent #卸载
User and
Group
http://docs.ansible.com/ansible/faq.html#how-do-i-generate-crypted-passwords-for-the-user-module
pip install passlib
python -c "from passlib.hash import sha512_crypt; import getpass;
print sha512_crypt.encrypt(getpass.getpass())"
root@jlive:~#ansible
linux -m
user -a "name=hello password=$(echo
hello|md5sum|awk '{print $1}')"
192.168.8.150 | success >> {
"changed":
true,
"comment":
"",
"createhome":
true,
"group":
1002,
"home":
"/home/hello",
"name":
"hello",
"password":
"NOT_LOGGING_PASSWORD",
"shell":
"",
"state":
"present",
"system":
false,
"uid": 1002
}
192.168.8.100 | success >> {
"changed":
true,
"comment":
"",
"createhome":
true,
"group":
1001,
"home":
"/home/hello",
"name":
"hello",
"password":
"NOT_LOGGING_PASSWORD",
"shell":
"/bin/bash",
"state":
"present",
"system":
false,
"uid": 1001
}
root@jlive:~#ansible
linux -m user -a
"name=hello state=absent"
192.168.8.150 | success >> {
"changed":
true,
"force":
false,
"name":
"hello",
"remove":
false,
"state": "absent"
}
192.168.8.100 | success >> {
"changed":
true,
"force":
false,
"name":
"hello",
"remove":
false,
"state": "absent"
}
Deploying From
Source Control
ansible linux -m git -a "repo=git://foo.example.org/repo.git
dest=/srv/myapp version=HEAD"
root@jlive:~#ansible
centos -m git -a
"repo=https://gitcafe.com/liujun_live/ucloud-bash-sdk.git
dest=/srv/myapp"
192.168.8.100 | success >> {
"after":
"82af63bd8a5eeac30e0d1f0587d76ca716261b88",
"before":
null,
"changed": true
}
root@jlive:~#ansible
centos -m shell -a "ls /srv/myapp"
192.168.8.100 | success | rc=0 >>
README
README.md
common.conf
example
sdk_UCloud.sh
Managing
Services
root@jlive:~#ansible
centos -m service -a "name=sshd state=restarted"
192.168.8.100 | success >> {
"changed":
true,
"name":
"sshd",
"state": "started"
}
stage属性
started #启动
stopped #停止
restarted #重启
Background
Operations
root@jlive:~#ansible
linux -B 3600 -P
0 -a "dd if=/dev/zero of=/dev/null"
background launch...
192.168.8.100 | success >> {
"ansible_job_id": "299528871122.11109",
"results_file":
"/root/.ansible_async/299528871122.11109",
"started": 1
}
192.168.8.150 | success >> {
"ansible_job_id":
"299528871122.4896",
"results_file":
"/root/.ansible_async/299528871122.4896",
"started": 1
}
root@jlive:~#ansible
centos -m
async_status -a
"jid=299528871122.11109"
192.168.8.100 | success >> {
"ansible_job_id":
"299528871122.11109",
"changed":
false,
"results_file":
"/root/.ansible_async/299528871122.11109",
"started": 1
}
Gathering
Facts
root@jlive:~#ansible
linux -m setup
192.168.8.150 | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.8.150"
],
"ansible_all_ipv6_addresses": [
"fe80::5054:ff:fe0b:b1"
],
"ansible_architecture":
"x86_64",
"ansible_bios_date":
"01/01/2011",
"ansible_bios_version":
"0.5.1",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.13.0-32-generic",
"console":
"ttyS0",
"quiet":
true,
"ro":
true,
"root":
"/dev/mapper/localhost--vg-root",
"splash":
true,
"vga":
"791",
"vt.handoff": "7"
},
"ansible_date_time": {
"date":
"2016-01-22",
"day":
"22",
"epoch":
"1453393941",
"hour":
"00",
"iso8601":
"2016-01-21T16:32:21Z",
"iso8601_micro":
"2016-01-21T16:32:21.779349Z",
"minute":
"32",
"month":
"01",
"second":
"21",
"time":
"00:32:21",
"tz":
"CST",
"tz_offset": "+0800",
"weekday":
"Friday",
"year":
"2016"
},
"ansible_default_ipv4": {
"address":
"192.168.8.150",
"alias":
"eth0",
"gateway":
"192.168.8.254",
"interface": "eth0",
"macaddress": "52:54:00:0b:00:b1",
"mtu":
1500,
"netmask":
"255.255.255.0",
"network":
"192.168.8.0",
"type":
"ether"
},
"ansible_default_ipv6":
{},
"ansible_devices": {
"vda":
{
"holders":
[],
"host": "SCSI storage
controller: Red Hat, Inc Virtio block
device",
"model":
null,
"partitions": {
"vda1": {
"sectors":
"497664",
"sectorsize": 512,
"size":
"243.00 MB",
"start":
"2048"
},
"vda2": {
"sectors":
"2",
"sectorsize": 512,
"size":
"1.00 KB",
"start":
"501758"
},
"vda5": {
"sectors":
"41439232",
"sectorsize": 512,
"size":
"19.76 GB",
"start":
"501760"
}
},
"removable":
"0",
"rotational":
"1",
"scheduler_mode":
"",
"sectors":
"41943040",
"sectorsize":
"512",
"size": "20.00
GB",
"support_discard":
"0",
"vendor": "0x1af4"
}
},
"ansible_distribution":
"Ubuntu",
"ansible_distribution_major_version":
"14",
"ansible_distribution_release":
"trusty",
"ansible_distribution_version":
"14.04",
"ansible_domain":
"localdomain",
"ansible_env": {
"HOME":
"/root",
"LANG":
"C",
"LANGUAGE": "en_HK:en",
"LC_CTYPE": "C",
"LOGNAME":
"root",
"MAIL":
"/var/mail/root",
"PATH":
"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
"PWD":
"/root",
"SHELL":
"/bin/bash",
"SHLVL":
"1",
"SSH_CLIENT": "192.168.8.254 53465 2220",
"SSH_CONNECTION": "192.168.8.254 53465 192.168.8.150
2220",
"SSH_TTY":
"/dev/pts/1",
"TERM":
"xterm-256color",
"USER":
"root",
"XDG_RUNTIME_DIR": "/run/user/0",
"XDG_SESSION_ID": "30",
"_":
"/bin/sh"
},
"ansible_eth0": {
"active":
true,
"device":
"eth0",
"ipv4":
{
"address":
"192.168.8.150",
"netmask":
"255.255.255.0",
"network": "192.168.8.0"
},
"ipv6":
[
{
"address":
"fe80::5054:ff:fe0b:b1",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "52:54:00:0b:00:b1",
"mtu":
1500,
"promisc":
false,
"type":
"ether"
},
"ansible_eth1": {
"active":
false,
"device":
"eth1",
"macaddress": "52:54:00:0b:00:02",
"mtu":
1500,
"promisc":
false,
"type":
"ether"
},
"ansible_eth2": {
"active":
false,
"device":
"eth2",
"macaddress": "52:54:00:0b:00:b1",
"mtu":
1500,
"promisc":
false,
"type":
"ether"
},
"ansible_eth3": {
"active":
false,
"device":
"eth3",
"macaddress": "52:54:00:04:00:b1",
"mtu":
1500,
"promisc":
false,
"type":
"ether"
},
"ansible_form_factor":
"Other",
"ansible_fqdn":
"localhost.localdomain",
"ansible_hostname":
"localhost",
"ansible_interfaces": [
"lo",
"eth3",
"eth2",
"eth1",
"eth0"
],
"ansible_kernel":
"3.13.0-32-generic",
"ansible_lo": {
"active":
true,
"device":
"lo",
"ipv4":
{
"address":
"127.0.0.1",
"netmask":
"255.0.0.0",
"network": "127.0.0.0"
},
"ipv6":
[
{
"address": "::1",
"prefix": "128",
"scope": "host"
}
],
"mtu":
65536,
"promisc":
false,
"type":
"loopback"
},
"ansible_lsb": {
"codename": "trusty",
"description": "Ubuntu 14.04.1 LTS",
"id":
"Ubuntu",
"major_release": "14",
"release":
"14.04"
},
"ansible_machine":
"x86_64",
"ansible_memfree_mb": 225,
"ansible_memtotal_mb":
363,
"ansible_mounts": [
{
"device":
"/dev/mapper/localhost--vg-root",
"fstype":
"ext4",
"mount":
"/",
"options":
"rw,errors=remount-ro",
"size_available":
17671827456,
"size_total": 19690643456
},
{
"device":
"/dev/vda1",
"fstype":
"ext2",
"mount":
"/boot",
"options":
"rw",
"size_available":
196618240,
"size_total": 246755328
}
],
"ansible_nodename":
"localhost",
"ansible_os_family":
"Debian",
"ansible_pkg_mgr": "apt",
"ansible_processor": [
"QEMU
Virtual CPU version 1.5.3",
"QEMU
Virtual CPU version 1.5.3"
],
"ansible_processor_cores":
1,
"ansible_processor_count":
2,
"ansible_processor_threads_per_core":
1,
"ansible_processor_vcpus":
2,
"ansible_product_name":
"KVM",
"ansible_product_serial":
"NA",
"ansible_product_uuid":
"5103EA6D-02FE-49B7-A19B-173128A511E0",
"ansible_product_version": "RHEL 7.0.0 PC
(i440FX + PIIX, 1996)",
"ansible_python_version":
"2.7.6",
"ansible_selinux": false,
"ansible_ssh_host_key_dsa_public":
"AAAAB3NzaC1kc3MAAACBAP9HwFLp0isPPUlBJ0cjNnv5h/5IZu4r+uQdFpB/G5LYn3fhTsXvdWJtuwLQXU0oMTN16uyy9GJlnjyw3DKTsfpt6OUH91GGL4rpEvrzHzJsrhYkqRL33O+ZEc+4fH6exYyt/tMgl5IffxX0B6H1SJUbW/FegWbnI5tCdjVUhY09AAAAFQC4NmExzFtXrRr2ehYK4FkegIeMfQAAAIEA/A99YYxeq7LfbBU4cepQcmZqGu4GTaAUKvwq8C/N7RWAksv9bQSUxuvjEVi3fW/4KQVN33qauG89z7bwKUeqTU7GEoPqi9AJXd4Q/0SWaqeh1c4yz+0TpaiCeRJ26XDYymNdayvm8Kh4rkED776sSVdr3BwoyeE2o+VvYO5FYGgAAACAEpcEKA8QogcZ8kt8UxCMishjDAg1iQ+Yl1/YVrpOC8QkK+KLtMbP2UoJTYW+WYvX2o5VFRgXJETPBoHnm8zVploo891fYRWRKLCgCXhbCm9Wql8f6VYplqJ6sYH7G/gNZlYr+G8cdYmKLX+et7T1dZlQspl3Hdr0uWy+xmJY2FI=",
"ansible_ssh_host_key_ecdsa_public":
"AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAX8XegqWBVOgmz5KhYb8b6up8FTiigJEFozZOLiIEXoXMilvhYyboR1yTDTSqd+wSSEfO5dYTybMNNFD/pZgkI=",
"ansible_ssh_host_key_rsa_public":
"AAAAB3NzaC1yc2EAAAADAQABAAABAQDciuPUsbkww8/lqjgEhaOh0o2n8enk1AdHhwoQb6LwTaPMrv60IwvkIlviaAH+zWdzG0SM48UN9/V2b+4H+ZvYFGj1KVDSYDhsnXPP7FaiA/bLzkrAXHScpeCTNGGsfiwWwpOXiscC8lIr/qk/ANusy5xQq4K6EweL7fG0/MoRuerajJCrRvPhdDvM4+AzhJxBzxf8Pbmlhaaes8Y8QEe6glKvSZ48/Joc9v6RdXTSvIbvrJ1xZViVPWGJ2FkTqZAgIkIAyi+n6o8zgu6UFLtseNPjcvQMlceeuKTDInTMUJdPrDYxNJANlppuEPFPG8YWHwbhLEVONDmr3lO3D4+d",
"ansible_swapfree_mb":
1023,
"ansible_swaptotal_mb":
1023,
"ansible_system": "Linux",
"ansible_system_vendor": "Red
Hat",
"ansible_user_id": "root",
"ansible_userspace_architecture":
"x86_64",
"ansible_userspace_bits":
"64",
"ansible_virtualization_role":
"guest",
"ansible_virtualization_type":
"kvm",
"module_setup": true
},
"changed": false
}