1
2
3
|
ftp
.testansible.com
samba.testansible.com
mail.testansible.com
|
1
2
3
4
5
6
7
8
9
|
名称 默认值 描述
ansible_ssh_host 主机的名字 SSH目的主机名或IP
ansible_ssh_port 22 SSH目的端口
ansible_ssh_user root SSH登录使用的用户名
ansible_ssh_pass none SSH认证所使用的密码
ansible_connection smart ansible使用何种连接模式连接到主机
ansible_ssh_private_key_file none SSH认证所使用的私钥
ansible_shell_type sh 命令所使用的shell
ansible_python_interpreter
/usr/bin/python
主机上的python解释器
|
1
|
#ansible --inventory-file=/root/iptables/iptableshosts iptables -m script -a '/root/iptables/check_iptables.sh'
|
1
|
#ansible --inventory-file=/root/iptables/iptableshosts iptables -f 6 -m script -a '/root/iptables/check_iptables.sh'
|
1
2
3
|
#vim /etc/ansible/hosts
192.168.100.10
192.168.100.11
|
1
2
3
4
5
6
|
[web]
192.168.100.10
192.168.100.11
[httpd]
192.168.100.12
192.168.100.13
|
1
2
3
4
5
6
7
8
9
|
[apache]
http1.
test
.com
http2.
test
.com
[nginx]
nginx1.
test
.com
nginx2.
test
.com
[webservers:children]
apache
nginx
|
1
2
|
[webservers]
web1.
test
.com http_port=80 ansible_ssh_port=12345
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[all:vars]
ntp_server=ntp.centos.com
[production]
test1
test2
test3
[production:vars]
db_primary_port=22
[groupservers]
web1.
test
.com
web2.
test
.com
[groupservers:vars]
ntp_server=ntp.
test
.com
admin_user=tom
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[apache]
http1.
test
.com
http2.
test
.com
[nginx]
nginx1.
test
.com
nginx2.
test
.com
[webservers:children]
apache
nginx
[webservers:vars]
ntp_server=ntp.
test
.com
|
1
2
|
File:
/etc/ansible/group_vars/group
admin_user: tom
|
1
2
|
File:
/etc/ansible/host_vars/host1
admin_user: john
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
---
- hosts: all
user: root
vars:
GETURL:
"http://192.168.24.14/sa"
TARFILE:
"sa"
TMPPATCH:
"/tmp"
SHFILE:
"os.sh"
tasks:
- name: Download {{TARFILE}}.
tar
.gz package
get_url: url=
"{{GETURL}}/{{TARFILE}}.tar.gz"
dest={{TMPPATCH}} sha256sum=b6f482b3c26422299f06524086d1f087e1d93f2748be18542945bca4c2df1569
tags:
-downsa
- name: tarzxvf {{TARFILE}}.
tar
.gz
file
shell:
tar
zxvf
"{{TMPPATCH}}/{{TARFILE}}.tar.gz"
-C {{TMPPATCH}}
tags:
-tarxsa
- name: Run{{SHFILE}} script
shell:
"{{TMPPATCH}}/{{TARFILE}}/{{SHFILE}}"
tags:
-runsa
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#ansible-doc copy
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
> COPY
The [copy] module copies a
file
on the
local
box to remote locations. Use the [fetch] module to copy files from remote locations to the
local
box. If you need
variable interpolation
in
copied files, use the [template] module.
Options (= is mandatory):
- backup
……………………还有很多可以显示,此处省略
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#ansible-doc -l
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
a10_server Manage A10 Networks AX
/SoftAX/Thunder/vThunder
devices
a10_service_group Manage A10 Networks AX
/SoftAX/Thunder/vThunder
devices
a10_virtual_server Manage A10 Networks AX
/SoftAX/Thunder/vThunder
devices
acl Sets and retrieves
file
ACL information.
add_host add a host (and alternatively a group) to the ansible-playbook
in
-memory inventory
airbrake_deployment Notify airbrake about app deployments
alternatives Manages alternative programs
for
common commands
apache2_module enables
/disables
a module of the Apache2 webserver
apt Manages apt-packages
……………………还有很多模块可以显示,这里省略
|
1
2
3
|
#ansible host1 -m command -a "free -m" #可以简写为ansible host1 -a "free -m"
#ansible host1 -m script -a "/home/test.sh 12 34"
#ansible host1 -m shell -a "/home/test.sh"
|
1
2
3
4
5
6
7
8
|
#ansible web -m shell -a "/root/test.sh 3 4 "
10.0.90.25 | success | rc=0 >>
7
20151119-171933
10.0.90.24 | success | rc=0 >>
7
20151119-171933
注:
test
.sh 在客户端服务器
/root
目录,内容是前两个位置变量相加,并打印出当前时间。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#cat test1.sh
#!/bin/bash
if
[ -z $1 ] || [ -z $2 ];
then
echo
"Wrong,Please input two args"
echo
"Usage `basename $0` arguments arguments"
exit
6
fi
mkdir
-pv
/usr/local/src/
$1/$2
#cat createdir.yml
---
- hosts:
"{{ host }}"
user:
"{{ user }}"
gather_facts: True
tasks:
- name: Create Dir
in
client server
script:
/etc/ansible/test1
.sh data log
执行:
#ansible-playbook createdir.yml -e "host=web user=root"
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
shell脚本
#cat alter.sh
#!/bin/bash
##modify passwd##
echo
'root:1234567890'
|chpasswd
if
[ $? -
eq
0 ]
then
echo
"Change password for root OK!!!"
else
echo
"Change password for root failure!!!"
fi
ansible的yml文件
#cat modify_all_password.yml
---
- hosts: all
user: root
gather_facts: True
tasks:
- name: Modify root
passwd
in
all client
script:
/etc/ansible/roles/alter
.sh
然后执行:
#ansible-playbook modify_all_password.yml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#ansible-playbook --help
Usage: ansible-playbook playbook.yml
Options:
--ask-become-pass ask
for
privilege escalation password
-k, --ask-pass ask
for
connection password
--ask-
su
-pass ask
for
su
password (deprecated, use become)
-K, --ask-
sudo
-pass ask
for
sudo
password (deprecated, use become)
--ask-vault-pass ask
for
vault password
-b, --become run operations with become (nopasswd implied)
--become-method=BECOME_METHOD
privilege escalation method to use (default=
sudo
),
valid choices: [
sudo
|
su
| pbrun | pfexec | runas |
doas ]
…………………… 还有很多,此处省略。
这里介绍一些常用的
--inventory=PATH (-i PATH): 指定inventory文件,默认文件是
/etc/ansible/hosts
--verbose(-
v
): 显示详细输出,也可以使用-vvvv显示精确到每分钟的输出
--extra-vars=VARS(-e VARS): 定义在playbook使用的变量,格式为:
"key=value,key=value"
--forks=NUM ( -f NUM): 指定并发执行的任务数,默认为5,根据服务器性能,调大这个值可提高ansible执行效率
--connection=TYPE ( -c TYPE):指定连接远程主机的方式,默认为
ssh
,设为
local
时,刚只在本地执行playbook,建议不做修改
--check: 检测模式,playbook中定义的所有任务将在每台远程主机上进行检测,但并不直正执行
|
1
2
3
4
5
6
|
#ansible host1 -m copy -a "src=/root/php-5.5.24-1.ele.el6.x86_64.rpm dest=/usr/local/src owner=root group=root mode=0755"
查看客户端文件是否存在
#ansible host1 -m shell -a "ls -l /usr/local/src"
172.16.29.193 | success | rc=0 >>
total 10264
-rw-r--r--. 1 root root 10507544 May 30 02:40 php-5.5.24-1.ele.el6.x86_64.rpm
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# ansible 10.0.90.25 -m stat -a "path=/etc/sysctl.conf"
10.0.90.25 | SUCCESS => {
"changed"
:
false
,
"stat"
: {
"atime"
: 1459270210.5650001,
"checksum"
:
"a27c7ce2e6002c37f3cb537ad997c6da7fd76480"
,
"ctime"
: 1441217442.5749998,
"dev"
: 2051,
"exists"
:
true
,
"gid"
: 0,
"gr_name"
:
"root"
,
"inode"
: 1181554,
"isblk"
:
false
,
"ischr"
:
false
,
"isdir"
:
false
,
"isfifo"
:
false
,
"isgid"
:
false
,
"islnk"
:
false
,
"isreg"
:
true
,
"issock"
:
false
,
"isuid"
:
false
,
"md5"
:
"c97839af771c8447b9fc23090b4e8d0f"
,
"mode"
:
"0644"
,
"mtime"
: 1413471211.0,
"nlink"
: 1,
"path"
:
"/etc/sysctl.conf"
,
"pw_name"
:
"root"
,
"rgrp"
:
true
,
"roth"
:
true
,
"rusr"
:
true
,
"size"
: 1150,
"uid"
: 0,
"wgrp"
:
false
,
"woth"
:
false
,
"wusr"
:
true
,
"xgrp"
:
false
,
"xoth"
:
false
,
"xusr"
:
false
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#ansible host1 -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"
172.16.29.193 | success >> {
"changed"
:
true
,
"checksum"
:
"8bc43056c39fbb882cf5d7b0391d70b6e84096c6"
,
"dest"
:
"/tmp/index.html"
,
"gid"
: 0,
"group"
:
"root"
,
"md5sum"
:
"324aa881293b385d2c0b355cf752cff9"
,
"mode"
:
"0440"
,
"msg"
:
"OK (unknown bytes)"
,
"owner"
:
"root"
,
"secontext"
:
"unconfined_u:object_r:user_tmp_t:s0"
,
"sha256sum"
:
""
,
"size"
: 93299,
"src"
:
"/tmp/tmp3WI5fE"
,
"state"
:
"file"
,
"uid"
: 0,
"url"
:
"http://www.baidu.com"
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#ansible host1 -m yum -a "name=vsftpd state=latest"
Ubuntu系列:
#ansible host1 -m apt -a "pkg=vsftpd state=latest"
yum 模块的一些用法:
- 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
- name:
install
the
'Gnome desktop'
environment group
yum: name=
"@^gnome-desktop-environment"
state=present
|
1
2
3
4
5
6
7
8
9
10
|
#ansible host1 -m cron -a "name='crontab test' minute=0 hour=5,2 job='ls -alh > /dev/null'"
172.16.29.193 | success >> {
"changed"
:
true
,
"jobs"
: [
"crontab test"
]
}
效果如下:
#Ansible: crontab test
0 5,2 * * *
ls
-alh >
/dev/null
|
1
2
3
4
5
6
7
8
9
10
|
#cat add_cron.yml
---
- hosts: web_crontab
remote_user: root
gather_facts: True
tasks:
- name: add ntp server
cron
job
cron
: name=
"local network ntpserver"
minute=
"*/12"
hour=
"*"
job=
"/usr/sbin/ntpdate 10.0.18.1 > /root/ntp.log"
执行:
#ansible-playbook add_cron.yml
|
1
2
3
4
5
6
7
8
9
10
|
#cat del_cron.yml
---
- hosts: web
remote_user: root
gather_facts:
false
tasks:
- name: del an old
crontab
job
cron
: name=
"local network ntpserver"
state=absent
执行:
|