$?cat?hosts localhost?ansible_ssh_host=192.168.38.104?key=104client_105?ansible_ssh_host=192.168.38.105?key=105sour_106?ansible_ssh_host=192.168.38.106?key=106
创建:/etc/ansible/host_vars/ 创建:/etc/ansible/group_vars/ 目录下文件名与host?&?group名一致,并未yml格式 例: ????$?cat?/etc/ansible/host_vars/client_105.yml ???? --- client_105_key:?105
-?为全局变量:-?-e?"name=duanyifei"-?--extra-vars=EXTRA_VARS-?-e?"@var.json"?/?"@var.yaml"?(通过文件传入)???-?cat?var.json:????????-?{"key":?"json"}???-?cat?var.yaml:????????-?key:?yaml
--- -?host:?client_105 ??vars: ????key:?ansible
vars_files: ????-?var.yaml
--- -?hosts:?client_105 ??gather_facts:?False ??vars_prompt: ????-?name:?‘client_105_key‘ ??????prompt:?‘Input?key‘??#交互时提示信息 ??????private:?no??????????#输入数据是否显示 ??tasks: ????-?name:?print?105_key ??????debug:?msg="{{?client_105_key?}}"
两个tasks之前传递变量:
????tasks: ??-?name:?test1 ????shell:?hostname ????register:?info ??-?name:?test2 ????debug:?msg="the?varibale?is?{{?info?}}"
#?获取remote_ip变量,并传给对端 $?cat?variable.yml --- -?hosts:?client_105 ??tasks: ????-?name:?print?105_key ??????template:?src=1.txt?dest=/tmp $?cat?1.txt {{?ansible_all_ipv4_addresses?}} 结果: [root@Client_105?~]#?cat?/tmp/1.txt [u‘192.168.38.105‘]
原文地址:http://blog.51cto.com/duanyifei/2049315