ansible基础
ansible 介绍:官网,百科之类的
ansible的部署
centos 6.5上的部署.
2.2.2.11 node1.king.com ansible 2.2.2.15 node3.king.com 2.2.2.12 node2.king.com
tar xf ansible-1.5.4.tar.gz cd ansible-1.5.4 python setup.py build python setup.py install mkdir /etc/ansible cp -r examples/* /etc/ansible
ansible的用法:
ansible <host-pattern> [options]
实例:配置匹配的主机
ansible 基于SSH协议,因此需要添加ssh互信,如果不添加则需要手动输入密码.
ssh互信.针对主机名互信.并且在/etc/hosts文件中添加对应的地址
ssh-keygen -t rsa -P ‘‘ -f .ssh/id_rsa ssh-copy-id -i .ssh/id_rsa.pub 2.2.2.12
[root@node1 ~]# cat /etc/ansible/hosts [hanodes] node1.king.com node2.king.com node3.king.com
ansible命令的用用法:
配置好主机后,我们先执行下联通性
[root@node1 ~]# ansible all -m ping
node1.king.com | success >> {
"changed": false,
"ping": "pong"
}
node3.king.com | success >> {
"changed": false,
"ping": "pong"
}
node2.king.com | success >> {
"changed": false,
"ping": "pong"
}默认试用的模块为command
[root@node1 ~]# ansible all -a ‘date‘ node1.king.com | success | rc=0 >> Sat May 3 18:08:53 CST 2014 node2.king.com | success | rc=0 >> Sat May 3 18:08:54 CST 2014 node3.king.com | success | rc=0 >> Sat May 3 18:08:55 CST 2014
[root@node1 ~]# ansible all -m command -a ‘date‘ node1.king.com | success | rc=0 >> Sat May 3 18:09:25 CST 2014 node3.king.com | success | rc=0 >> Sat May 3 18:09:26 CST 2014 node2.king.com | success | rc=0 >> Sat May 3 18:09:25 CST 2014
ansible-doc xx 显示指定的模块的文档
ansible-doc -l 显示用那些模块
[root@node1 ~]# ansible all -m yum -a "name=httpd state=present"
node1.king.com | success >> {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"httpd-2.2.15-29.el6.centos.x86_64 providing httpd is already installed"
]
}
node3.king.com | success >> {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"httpd-2.2.15-29.el6.centos.x86_64 providing httpd is already installed"
]
}
node2.king.com | success >> {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"httpd-2.2.15-29.el6.centos.x86_64 providing httpd is already installed"
]
}上面就是我们安装httpd的用法
ansible这个命令是不是太低端,我们有高级的用法ansible-playbook,需要配置yaml语法
yaml
AML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言,另外Ingy dt Net与Oren Ben-Kiki也是这语言的共同设计者。
YAML Ain‘t Markup Language,即YAML不是XML。不过,在开发的这种语言时,YAML的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。其特性:
YAML的可读性好
YAML和脚本语言的交互性好
YAML使用实现语言的数据类型
YAML有一个一致的信息模型
YAML易于实现
YAML可以基于流来处理
YAML表达能力强,扩展性好5.1 playbook基础组件
5.1.1 Hosts和Users
playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组;remote_user则用于指定远程主机上的执行任务的用户。如上面示例中的
-hosts: webnodes
remote_user: root
不过,remote_user也可用于各task中。也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务;此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户。
- hosts: webnodes
remote_user: mageedu
tasks:
- name: test connection
ping:
remote_user: mageedu
sudo: yes
5.1.2 任务列表和action
play的主体部分是task list。task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。在运行自下而下某playbook时,如果中途发生错误,所有已执行任务都将回滚,因此,在更正playbook后重新执行一次即可。
task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致。
每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出。
定义task的可以使用“action: module options”或“module: options”的格式,推荐使用后者以实现向后兼容。如果action一行的内容过多,也中使用在行首使用几个空白字符进行换行。
tasks:
- name: make sure apache is running
service: name=httpd state=running
在众多模块中,只有command和shell模块仅需要给定一个列表而无需使用“key=value”格式,例如:
tasks:
- name: disable selinux
command: /sbin/setenforce 0
如果命令或脚本的退出码不为零,可以使用如下方式替代:
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
或者使用ignore_errors来忽略错误信息:
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
5.1.3 handlers
用于当关注的资源发生变化时采取一定的操作。
“notify”这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。
- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
handler是task列表,这些task与前述的task并没有本质上的不同。
handlers:
- name: restart memcached
service: name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted
案例:
heartbeat.yaml
- hosts: hbhosts
remote_user: root
tasks:
- name: ensure heartbeat latest version
yum: name=heartbeat state=present
- name: authkeys configure file
copy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys
- name: authkeys mode 600
file: path=/etc/ha.d/authkeys mode=600
notify:
- restart heartbeat
- name: ha.cf configure file
copy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cf
notify:
- restart heartbeat
handlers:
- name: restart heartbeat
service: name=heartbeat state=restarted案例:
heartbeat.yaml 这个是yaml文件
- hosts: hbhosts 这个是主机组
remote_user: root 这个是远程执行的用户
tasks: 任务.
- name: ensure heartbeat latest version 这个是干活的名称
yum: name=heartbeat state=present 具体干活
- name: authkeys configure file
copy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys
- name: authkeys mode 600
file: path=/etc/ha.d/authkeys mode=600
notify:
- restart heartbeat
- name: ha.cf configure file
copy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cf
notify: 通知handlers干活
- restart heartbeat
handlers:
- name: restart heartbeat
service: name=heartbeat state=restarted我们来测试这个ansible干活不
- hosts: hanodes
remote_user: root
vars:
crmsh: crmsh-1.2.6-4.el6.x86_64.rpm
pssh: pssh-2.3.1-2.el6.x86_64.rpm
tasks:
- name: corosync installing
yum: name=corosync state=present
- name: pacemaker installing
yum: name=pacemaker state=present
- name: crmsh rpm packages
copy: src=/ansible/corosync/packages/{{ crmsh }} dest=/tmp/{{ crmsh }}
- name: pssh rpm packages
copy: src=/ansible/corosync/packages/{{ pssh }} dest=/tmp/{{ pssh }}
- name: crmsh installing
command: yum -y install /tmp/{{ crmsh }} /tmp/{{ pssh }}
- name: authkey configure file
copy: src=/ansible/corosync/conf/authkey dest=/etc/corosync/authkey
- name: authkey mode 400
file: path=/etc/corosync/authkey mode=400
notify:
- restart corosync
- name: corosync.conf configure file
copy: src=/ansible/corosync/conf/corosync.conf dest=/etc/corosync/corosync.conf
tags:
- conf
notify:
- restart corosync
- name: ensure the corosync service startup on boot
service: name=corosync state=started enabled=yes
handlers:
- name: restart corosync
service: name=corosync state=restarted下面是显示:
[root@node1 ~]# time ansible-playbook /ansible/corosync/corosync.yaml
PLAY [hanodes] ****************************************************************
GATHERING FACTS ***************************************************************
ok: [node1.king.com]
ok: [node2.king.com]
ok: [node3.king.com]
TASK: [corosync installing] ***************************************************
ok: [node1.king.com]
failed: [node3.king.com] => {"changed": false, "failed": true, "rc": 0, "results": []}
msg: The following packages have pending transactions: corosync-x86_64
failed: [node2.king.com] => {"changed": false, "failed": true, "rc": 0, "results": []}
msg: The following packages have pending transactions: corosync-x86_64上面的绿色是表示干活成功,红色是表示干活失败
文章来自:http://limeizhi.blog.51cto.com/2267772/1405806