5645re 发表于 2017-9-13 09:30:01

ansibel安装以及语法分析


1
2
3
4
yum install epel-release -y
yum install python-setuptools python-setuptools-devel python-devel sshpass -y
easy_install pip
pip install ansible


使用shell模块案例

1
2
3
ansible -i /etc/ansible/test/hosts mqservers -m shell -a 'echo "export http_proxy=http://192.168.2.11:3128" >> /etc/bashrc'
ansible -i /etc/ansible/test/hosts mqservers -m shell -a 'echo "export https_proxy=http://192.168.2.11:3128" >> /etc/bashrc'
ansible -i /etc/ansible/test/hosts mqservers -m shell -a 'cat /etc/bashrc'




一个硬盘挂载案例


1
2
3
4
ansible tools-backup -m parted -a "device=/dev/xvdb number=1 state=present" -k
ansible tools-backup -m filesystem -a "fstype=ext4 dev=/dev/xvdb1" -k
ansible tools-backup -m file -a "path=/data state=directory mode=0755" -k
ansible tools-backup -m mount -a "path=/data src=/dev/xvdb1 fstype=ext4 state=mounted" -k





1
2
3
cat /etc/ansible/hosts

192.168.x.x





shell模块案例分析:
ansible 是命令
-i INVENTOR中文翻译n. 存货,存货清单;详细目录;财产清册,意思是记录主机财产表
/etc/ansible/test/hosts 是主机财产表
mqservers 是在/etc/ansible/test/hosts表中的名称
-mMODULE_NAME 模块名
shell shell模块

-aMODULE_ARGS 模块参数
'echo "export http_proxy=http://192.168.2.11:3128" >> /etc/bashrc' 具体的模块参数命令

一个硬盘挂载案例:
和上面的shell案例差不多,如果没有-i默认是使用default=/etc/ansible/hosts主机财产表
-m 后面接了不同的参数parted,filesystem,mount等。
-k -ask-pass要求输入对面主机密码

ansible简明的使用格式

1
2
3
4
5
6
7
ansible <host-pattern> [-f forks] [-m module_name] [-a args]

    host-pattern # 可以是all,或者配置文件中的主机组名
    -f forks# 指定并行处理的进程数
    -m module # 指定使用的模块,默认模块为command
    -a args   # 指定模块的参数
如果你有多台服务器的话,想并发运行,可以使用-f参数,默认是并发5





查看各模块的使用方法

1
2
3
ansible-doc :Show Ansible module documentation
-l 列出所有的ansible模块
-s 列出该模块的相关指令





连接与验证测试

1
ansible -i /etc/ansible/hosts all -m ping





常用的模块:copy、command、service、yum、apt、file、raw、shell、script、cron、user、state、template、

YAML分析

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
- hosts: hanodes      #指定要执行任务的主机,可由冒号分隔主机组
remote_user: root   #指定远程主机上执行任务的用户
vars:#定义如下2个变量
    crmsh: crmsh-1.2.6.4.el6.x86_64.rpm
    pssh: pssh-2.3.1-2.el6.x86_64.rpm
tasks:    #指定需执行的任务列表,每个task都有其name和使用的模块及参数
    - name: test connection
      ping:      #ping模块无需执行参数
      remote_user: jason#在task中指定远程主机上执行任务的用户
      sudo: yes   #使用sudo在远程主机上执行任务
    - name: corosync installing
      yum: name=corosync state=present
    - name: pacemaker installing          #定义一个软件安装任务
      yum: name=pacemaker state=present   #使用yum安装,并配置需安装的软件名(name),及状态(state)
    - 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 reinstall /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:   #定义一个通知,当此任务执行时,可以激发响应的handler
      - 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



页: [1]
查看完整版本: ansibel安装以及语法分析