ansible 模块 facts

简介:

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 %}


wKioL1N3p5KAA_rMAAJeeiqIkag935.jpg


对与变量,注意的地方

参考 

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系列的版本中调整变量优先级的顺序为如下。优先级由低到高

  1. role defaults 

  2. inventory vars 

  3. inventory group_vars

  4. inventory host_vars

  5. playbook group_vars

  6. playbook host_vars

  7. host facts

  8. registered vars

  9. set_facts

  10. play vars

  11. play vars_prompt

  12. play vars_files

  13. role and include vars

  14. block vars (only for tasks in block)

  15. task vars (only for the task)

  16. extra vars

变量实例

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
ansible在playbook中变量传入到执行脚本的方法
## 简介
 
这个方式用于ansible调用playbook方法实现远程服务器执行某个脚本,脚本中的某些参数需要由管理服务器传入的情况。
 
下面的例子是ansible实际调用script模块实现远程服务器执行一个shell脚本
 
## 脚本内容
 
### ansible 执行脚本
```python
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# edit: mesopodamia@gmail.com
 
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



本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1905182,如需转载请自行联系原作者

相关文章
|
6月前
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
|
8月前
|
网络协议 网络安全
Ansible模块介绍——防火墙模块
Ansible模块介绍——防火墙模块
139 0
|
6月前
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
|
9月前
|
Shell
ansible模块大全上【建议收藏】
ansible模块大全上【建议收藏】
103 0
ansible模块大全上【建议收藏】
|
5月前
|
网络安全 数据安全/隐私保护
ansible的get_url模块
ansible的get_url模块
|
5月前
|
存储 Linux Python
ansible手动添加模块
ansible手动添加模块
47 0
|
8月前
|
缓存 运维 监控
【运维知识进阶篇】Ansible变量详解(变量定义+变量优先级+变量注册+层级定义变量+facts缓存变量)
【运维知识进阶篇】Ansible变量详解(变量定义+变量优先级+变量注册+层级定义变量+facts缓存变量)
115 0
|
8月前
Ansible模块管理——磁盘管理模块、mount模块
Ansible模块管理——磁盘管理模块、mount模块
241 0
|
8月前
|
网络协议 网络安全 数据安全/隐私保护
Ansible模块介绍——配置网络模块、上传下载文件模块
Ansible模块介绍——配置网络模块、上传下载文件模块
256 0
|
8月前
|
应用服务中间件 nginx
Ansible模块——软件包管理模块
Ansible模块——软件包管理模块