##使用指定用户hbase运行脚本
- name: "start hbase region server cluster"
shell: "{{software_root_dir}}/{{hbase_foldername}}/bin/start-hbase.sh &"
become: yes
become_method: su
become_user: "{{hbase_cluster_config['hbase_username']}}"
##计算时间差
- set_fact: kafka_deploy_start_timestamp="{{lookup('pipe','date \"+%Y-%m-%d %H:%M:%S\"')}}"
- set_fact: kafka_deploy_finished_timestamp="{{lookup('pipe','date \"+%Y-%m-%d %H:%M:%S\"')}}"
- name: "kafka_deploy 开始时间: {{kafka_deploy_start_timestamp}} 结束时间: {{kafka_deploy_finished_timestamp}} 共耗时{{( (kafka_deploy_finished_timestamp | to_datetime) - (kafka_deploy_start_timestamp | to_datetime)).total_seconds()}}秒."
debug: msg=" "
- name: "添加用户{{hbase_username}}到指定组hadoop "
shell: "usermod -a -G hadoop {{hbase_username}}"
##动态添加一个资源池到vmware vCenter
- name: "动态添加一个资源池到vmware vCenter"
vmware_resource_pool:
hostname: "{{vcenterconfig['vcenterhostname']}}"
username: "{{vcenterconfig['vcenterusername']}}"
password: "{{vcenterconfig['vcenterpassword']}}"
datacenter: "{{vcenterconfig['datacenter']}}"
validate_certs: no
cluster: “test-cluster” #这个参数是要实现在vcenter 里点击右键创建的
resource_pool: "{{vcenterconfig['resource_pool']}}"
mem_shares: normal
mem_limit: -1
mem_reservation: 0
mem_expandable_reservations: True
cpu_shares: normal
cpu_limit: -1
cpu_reservation: 0
cpu_expandable_reservations: True
state: present
##浮点数 取整 //
- debug: var="{{ (nificonfig['zookeeper_cluster_uri'].split(",") | length | int + 1) // 2 | int }}"
##批量合并列表合并对象
- name: "批量合并列表合并对象"
set_fact: all_host_list="{{ all_host_list }} + {{ hostdict[item] }}"
with_items: "{{hostdict.keys()}}"
用+,列表合并
# use case I: appending to LIST variable:
- name: my appender
set_fact:
my_list_var: '{{my_list_myvar + new_items_list}}'
循环修改外层变量
{% set vars = {'foo': False} %}
{% for item in items %}
{% if vars.update({'foo': True}) %} {% endif %}
{% if vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if vars.foo %} Ok(2)! {% endif %}
jinja2 字典可以用 keys(), values() 来直接访问里面的集合
{% for item in ip_hostname_dict.values() %}
vagrant up {{item}} &
{% endfor %}
{% for item in ip_hostname_dict.keys() %}
ping -c 3 {{item}}
{% endfor %}
##ansible 遍历字典的方法 多层for循环, 修改外层变量, dictionary map 使用方法 开心实战
{% set ip_hostname_dict={} %}
{% for key,dict_item in hostdict.items() %}
{% for temphost in hostdict[key] %}
{% if ip_hostname_dict[temphost.ip] is undefined %}
#{{temphost.name.split('.')[0]}}
config.vm.define "{{temphost.name.split('.')[0]}}" do |web|
web.vm.hostname = "{{temphost.name.split('.')[0]}}"
web.vm.network :public_network, ip: "{{temphost.ip}}", bridge: "{{vm_bridge_nic_name}}", bootproto: "static", gateway: "{{gateway}}"
web.vm.provider :virtualbox do |vb|
vb.linked_clone = true
vb.name = "{{temphost.name.split('.')[0]}}"
vb.cpus = {{temphost.cpu}}
vb.memory = {{temphost.memory}}
vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
end
end
{% endif %}
{% do ip_hostname_dict.update({temphost.ip : temphost.name}) %}
{% endfor %}
{% endfor %}
#find 模块必须使用绝对路径,这里的 playbook_dir是当前role的main.yml的所在位置 这样用户不管拷贝这个文件夹到哪里部署都是正确的
- name: "查找{{ playbook_dir }}/roles/marathon-app-deploy/templates/marathon-apps files end with .json.j2,.json 文件"
find:
paths: "{{ playbook_dir }}/roles/marathon-app-deploy/templates/marathon-apps/"
recurse: yes
file_type: "file"
patterns: '*.json.j2,*.JSON.J2,*.json'
register: json_file_matched
delegate_to: 127.0.0.1