五郎. 发表于 2018-7-30 11:34:26

ansible安装和简单使用

  一、安装
  1、安装第三方epel源
  centos 5的epel
  rpm -ivh http://mirrors.sohu.com/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpm
  centos 6的epel
  rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
  由于是6版本所以安装6的epel
  2、安装ansible
  yum install ansible(ansible主机安装ip:192.168.1.100,下面的hosts里面机器不需要安装,所有执行操作都是在ansible主机完成)
cd /etc/ansible/root@ip-10-10-10-10:/etc/ansible # lltotal 12-rw-r--r-- 1 root root 5113 Dec 29 03:00 ansible.cfg-rw-r--r-- 1 root root965 Dec 29 03:00 hosts其中ansible.cfg是配置文件,hosts是管理主机信息  cat /etc/ansible/hosts
  
  #ipport用户名密码
  192.168.1.101 ansible_ssh_port=22 ansible_ssh_user=rootansible_ssh_pass=123456
  3、简单测试使用
  (1)ping:
  ansible主机执行:ansible cgc -m ping
  结果:
  192.168.1.101 | success >> {
  "changed": false,
  "ping": "pong"
  }
  证明测试没有问题;
  (2)copy文件:
  ansible主机执行:ansiblecgc-s -mcopy-a   'src=/root/mkdir.shdest=/home/mode=755    owner=www group=www'
  192.168.1.101 | success >> {
  "changed": true,
  "checksum": "ec8295b3b33f8a74b7b7acd98be4870ab6ac1726",
  "dest": "/home/mkdir.sh",
  "gid": 500,
  "group": "www",
  "md5sum": "0ed7ccc108ef06e3a95a8006b5d74e54",
  "mode": "0755",
  "owner": "www",
  "size": 29,
  "src": "/root/.ansible/tmp/ansible-tmp-1441617257.44-136368933988342/source",
  "state": "file",
  "uid": 500
  }
  在192.168.1.101的home目录下检查有mkdir.sh文件。
  三、playbook配置管理
  8、playbook
  A.进行一下shell模块操作,测试删除文件
  先查看一下客户端的select.py是否存在
  ansible cgc -mshell -a "ls-l/home/"
  192.168.1.101 | success | rc=0 >>
  total 12
  -rwxr-xr-x 1 www www   29 Sep7 17:14 mkdir.sh
  -rwxr-xr-x 1 www www646 Sep7 18:29 select.py
  drwx------ 2 www www 4096 Sep7 17:13 www
  可以看到是存在的
  然后写一个删除的playbook
  创建test.yml
  cattest.yml
  ---
  - hosts: cgc
  remote_user: root
  tasks:
  - name: delete /home/select.py
  shell: rm -rf /home/select.py
  运行:
  ansible-playbook test.yml
  PLAY ********************************************************************
  GATHERING FACTS ***************************************************************
  ok:
  TASK: ************************************************
  changed:
  PLAY RECAP ********************************************************************
  192.168.1.101            : ok=2    changed=1    unreachable=0    failed=0
  查询:
  ansible cgc -mshell -a "ls-l/home/select.py"
  192.168.1.101 | FAILED | rc=2 >>
  ls: cannot access /home/select.py: No such file or directory
  yum安装:
  ansible cgc-m yum -a 'name=httpd*state=installed'
  重启服务:
  ansible cgc-m service -a "name=httpdstate=stopped"
  http://liuzhengwei521.blog.51cto.com/4855442/1773802
  http://506554897.blog.51cto.com/2823970/1975245 git的web搭建
页: [1]
查看完整版本: ansible安装和简单使用