ansible -i hosts ip1 -m synchronize -a 'mode=pull src=file.tar.gz dest=/tmp/fenku/'
ansible -i hosts ip2 -m synchronize -a 'src=/tmp/fenku/ dest=/xx/xx/'
command,shell,script
Copy
copy: src=/opt/src/profile dest=/etc/profile force=yes owner=root group=root mode=0644
file
file: dest=` nginx_web_dir `/` item ` state=directory
file: path=` item ` owner=` nginx_user ` group=` nginx_user ` mode=0755
service service模块说白了,就是Linux下的service命令
template
template: src=index.html dest=` nginx_web_dir `/vhost/index.html owner=` nginx_user ` group=` nginx_user ` mode=0644
setup
Cron
User
user: name=` nginx_user ` state=present createhome=no shell=/sbin/nologin
yum
action
action: user name=tomcat password=xx update_password=always shell=/bin/bash home=/opt/ea
- name: copy test
copy: src=/etc/ansible/playbook/roles/http/meta/test dest=/opt/apps/`pro_dir`/
tags:
- updateconf
notify:
- stop tomcat service
- start tomcat service
ansible-playbook -i hosts site.yml --extra-vars "tomcat_dir=test pro_dir=test
并发执行
ansible-playbook user.yml -f 10
ansible web -m setup -a "filter=ansible_mounts"
变量 案例
client
/etc/ansible/facts.d/nm.fact
ansible -i hosts ot11 -m setup -a "filter=ansible_local"
shell: chdir=/opt/ea tar xvzf tomcat.tar.gz && rm -rf `ansible_local`.`nm`.`general`.`tomcat_dir` && mv tomcat `ansible_local`.`nm`.`general`.`tomcat_dir` && chown -R tomcat.tomcat `ansible_local`.`nm`.`general`.`tomcat_dir`
这边需要开启facts变量功能, gather_facts: no 或者是false是关闭,gather_facts:yes 或者是true都是开启。 当时没注意,找到了官方的实例,直接就干,结果sx了。咋都不行,总是提示define为定义。。。 原来facts没有开。。
{% for v in hostvars.iteritems() %}
{{ v['ansible_hostname'] }}
{% endfor %}
对与变量,注意的地方
参考
http://www.tuicool.com/articles/MfMjyeN
http://www.178linux.com/9431?utm_source=tuicool&utm_medium=referral
重点参考 http://blog.csdn.net/minxihou/article/details/53667797 有实例。
Ansible中的tags使用 http://unixman.blog.51cto.com/10163040/1674198
变量的优先级。处理同一个变量名多处定义的问题。
3.1、在1.x版本里面,越靠下优先级越高,可以覆盖上面的
1、role defaults 中定义的变量,优先级最低
2、从被管理系统中收集的facts信息
3、inventory中定义的变量
4、在playbook中的vars: 或者 roles 中的vars
5、一些连接变量,例如ansible_user 。
6、命令行的变量 ansible-playbook -e 选项的变量, 优先级别最高。
3.2 ,在2.x系列的版本中调整变量优先级的顺序为如下。优先级由低到高
role defaults
inventory vars
inventory group_vars
inventory host_vars
playbook group_vars
playbook host_vars
host facts
registered vars
set_facts
play vars
play vars_prompt
play vars_files
role and include vars
block vars (only for tasks in block)
task vars (only for the task)
extra vars
变量实例
ansible在playbook中变量传入到执行脚本的方法 ## 简介 这个方式用于ansible调用playbook方法实现远程服务器执行某个脚本,脚本中的某些参数需要由管理服务器传入的情况。 下面的例子是ansible实际调用script模块实现远程服务器执行一个shell脚本 ## 脚本内容 ### ansible 执行脚本 ```python #!/usr/bin/env python # -*- coding:utf-8 -*- # edit: [email protected] import ansible from ansible.playbook import PlayBook from ansible.inventory import Inventory from ansible import callbacks from ansible import utils import sys reload(sys) sys.setdefaultencoding('utf-8') vars = "hello world" ansible_command = "/usr/local/bin/ansible-playbook" playbook = "/home/eric/playbook/deploy.yml" hosts = ['192.168.0.99'] # Boilerplace callbacks for stdout/stderr and log output utils.VERBOSITY = 0 playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY) stats = callbacks.AggregateStats() runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) # pro输入数据是列表 def ansible_playbook(playbook,pro,vars): pb = PlayBook( playbook=playbook, inventory = Inventory(pro), callbacks = playbook_cb, runner_callbacks = runner_cb, stats = stats, extra_vars = {'var':vars} ) return pb.run() if __name__ == "__main__": ansible_playbook(playbook,hosts,vars) ``` ### 远程调用脚本(eric.sh) ```shell #!/usr/bin/env bash echo `date` > /tmp/date.txt echo $1 > /tmp/date.txt ``` ### ansible中playbook的设置 playbook的定义(deploy.yml) ```yaml - hosts: all roles: - role: init ``` 具体的角色init中task的main.yml定义 ```yaml - script: ~/bin/eric.sh "{{ var }}" ```
还有一种变量实现方式
roles:
- { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }
关于ansible的playbook模板和facts的后续文档
有兴趣的朋友可以看看tornado的模板渲染nginx配置
ansible 1.7.2 api 获取有某些应用的ip
http://szgb2016.blog.51cto.com/340201/1812500