[关闭]
@contribute 2016-06-03T01:23:33.000000Z 字数 1255 阅读 1402

ansible

tinkerpop


  1. ansible linshi -m copy -a "src=/root/github/mg-playbooks dest=~"

安装

  1. pip install ansible
  2. pip install https://github.com/pallets/jinja/zipball/master
  3. pip install paramiko

基本命令使用

  1. # 测试命令
  2. ansible all -m ping
  3. # 返回管理机器的一些基本信息
  4. ansible all -m setup
  1. ansible all -a 'date'

这是一条ad-hoc命令——临时执行命令,ad-hoc是ansible里的一个概念, 在上面命令中就是 -a ,具体稍后再说。命令中的all是值hosts中的所有服务器,当然也可以通过 ansible -i ~/hosts -a'who' 这样根据组名指定服务器。
ad-hoc——临时的,在ansible中是指需要快速执行,并且不需要保存的命令。说白了就是执行简单的命令——一条命令。对于复杂的命令后面会说playbook。

  1. # 文件操作
  2. ansible all -m file -a 'path=~/hello.txt'
  3. # 修改文件权限
  4. ansible all -m file -a 'path=~/hello.txt mode=700'
  5. #拷贝文件
  6. ansible all -m copy -a "src=/tmp/hello.txt dest=~"
  7. # 删除文件或目录
  8. ansible machinename -m file -a 'path=/tmp/testing state=absent'

playbook使用

  1. # playbook.yml
  2. ---
  3. - hosts: local # hosts中指定
  4. remote_user: the5fire # 如果和当前用户一样,则无需指定
  5. tasks:
  6. - name: whoami
  7. copy: src=~/hosts dest=~/hosts.dest # 本地拷贝到远端
  8. notify: # 如果copy执行完之后~/hosts.dest文件发送了变化,则执行
  9. - clear copy # 调用handler
  10. handlers:
  11. - name: clear copy
  12. shell: 'mv ~/hosts.dest hosts.del' # 假装删除

心得

变量优先级:

  1. roles/xxx/vars > host_vars > group_vars/${host} > group_vars/all

inventory

配置的参数有:

  1. ansible_ssh_host
  2. ansible_ssh_port
  3. ansible_ssh_pass
  4. ansible_ssh_user
  5. ansible_connection
  6. ansible_shell_tpye
  7. ansible_ssh_private_key_file
  8. ansible_python_interpreter

相关文章

自动化运维工具Ansible详细部署
Ansible中文权威指南
Cassandra Ansible Playbook
ansible简单学习笔记
自动化工具 ansible中文指南
ansible常用模块用法

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注