ansible-console
etcd 动态Inventory
https://gist.github.com/justenwalker/09698cfd6c3a6a49075b
Think! And Think Again
ansible-console
etcd 动态Inventory
https://gist.github.com/justenwalker/09698cfd6c3a6a49075b
任务委派就是 在playbook中指定 某任务只在指定的主机上执行
比如
我要在192.168.1.1 服务器添加一个hosts 记录 “1.1.1.1 www.abc.com” ,同时也要把这个hosts 记录写到192.168.1.2
ansible hosts 192.168.1.1 文件内容
[all]
192.168.1.1
name: add host record
shell: “echo “1.1.1.1 www.abc.com” >> /etc/hosts”
name: add host record
shell: “echo “1.1.1.1 www.abc.com” >> /etc/hosts”
delegate_to: 192.168.1.2
如下结果一样
tasks:
tasks:
#
#
hosts: all
tasks:
hosts: CentOS-6.2
tasks:
hosts: CentOS-6.3
tasks:
具体可参考这里:
https://github.com/ansible/lightbulb/tree/master/workshops/developer/lookup_plugins
这个方法 只在ansible2 中可用
自己可以写本地的模块,在ansible中调用自定义的本地模块,可以把本地的值传给远程
这个模块 其实依赖于 lookup_plugins
---
- name: launch instances
os_server:
name: "{{ prefix }}-{{ item.name }}"
state: present
key_name: "{{ item.key }}"
availability_zone: "{{ item.availability_zone }}"
nics: "{{ item.nics }}"
image: "{{ item.image }}"
flavor: "{{ item.flavor }}"
with_items: "{{ servers }}"
register: "os_hosts"
- name: add hosts to inventory
add_host:
name: "{{ item['openstack']['human_id'] }}"
groups: "{{ item['item']['meta']['group'] }}"
ansible_host: "{{ item.openstack.accessIPv4 }}"
with_items: "{{ os_hosts.results }}"
#ansible-pull 模式
ansible-pull 是ansible提供的一个命令
是为了大规模 可持续部署而设置的简单方式
传统的我们使用ansible 是在push模式,就是 我们把我们的配置推送到远程主机
现在 ansible 支持 pull模式
$ ansible-pull -U https://github.com/training-devops/ansible-pull-example -i
可以设置定时任务,让ansible自动 去git仓库中下载内容,如果git仓库有改变,
自动执行 对应的 inventory 和对应的playbook
$cat inventory
[local]
127.0.0.1
hosts: local
tasks:
yh ➜ cc ansible-pull -U https://github.com/training-devops/ansible-pull-example -i /etc/ansible/hosts
Starting ansible-pull at 2016-12-11 18:02:42
localhost | success >> {
“after”: “70339e8d7f435272e99798de9b004b10ccd7cd10”,
“before”: “70339e8d7f435272e99798de9b004b10ccd7cd10”,
“changed”: false
}
PLAY [local] **
GATHERING FACTS *
ok: [127.0.0.1]
TASK: [install vim] *
ansible 需要客户端 至少要有python-json 这个包依赖,但是如果你想批量安装这个包怎么办
可以使用raw 模块,raw模块和command模块基本一样,只是不需要python依赖,可以给一些交换机 等批量发命令
用法如下:
用法:
加密:
ansible-vault encrypt group_vars/all
查看:
ansible-vault view site.yml
解密:
ansible-vault decrypt site.yml
交互输入:
ansible-playbook -i hosts site.yml –tags=jre_install –ask-vault-pass
从文件中读取
ansible-playbook -i hosts site.yml –tags=jre_install –vault-password-file ~/.vault_pass.txt
参考:
http://ansible-tran.readthedocs.io/en/latest/docs/playbooks_vault.html
In 2.x, we have made the order of precedence more specific (with the last listed variables winning prioritization):
role defaults [1]
inventory vars [2]
inventory group_vars
inventory host_vars
playbook group_vars
playbook host_vars
host facts
play vars
play vars_prompt
play vars_files
registered vars
set_facts
role and include vars
block vars (only for tasks in block)
task vars (only for the task)
extra vars (always win precedence)