如何使用 ansible 变量
作者:ghostwritten
- 2022 年 5 月 10 日
本文字数:2217 字
阅读完需:约 7 分钟

1. 变量优先级
变量优先级由小到大排列(优先级大的变量可以覆盖优先级小的变量):
command line values (eg “-u user”)role defaults [1]inventory file or script group vars [2]inventory group_vars/all [3]playbook group_vars/all [3]inventory group_vars/* [3]playbook group_vars/* [3]inventory file or script host vars [2]inventory host_vars/* [3]playbook host_vars/* [3]host facts / cached set_facts [4]play varsplay vars_promptplay vars_filesrole vars (defined in role/vars/main.yml)block vars (only for tasks in block)task vars (only for the task)include_varsset_facts / registered varsrole (and include_role) paramsinclude paramsextra vars (always win precedence)
复制代码
2. 命令行参数 extra_vars 配置变量
--extra_vars = -e
ansible test70 -e "pkg=httpd" -m yum -a "name=httpd state=present"
复制代码
$ vim install.yml ---- hosts: node remote_user: root tasks: - name: install httpd service yum: name={{ pkg }} state=present
复制代码
ansible-playbook install.yml --extra-vars " pkg = httpd"ansible-playbook install.yml -i <指定ip或组> -e " pkg = httpd"
复制代码
3. inventory 主机清单中定义的变量
$ cat /etc/ansible/hosts[load-node]openstack-load1 openstack-load2
[compute-node]openstack-compute1 ansible_ssh_host=10.0.1.10 ansible_ssh_port=2002 ansible_ssh_user=stanley ansible_ssh_pass=etyfhzmweadfopenstack-compute2
[control-node]openstack-control1 filename=control1.txt # 主机变量openstack-control2 filename=control2.txt
[openstack:children]load-nodecompute-nodecontrol-node
[openstack:vars]issue="Hello, World" # 组变量
复制代码
4. ansible-playbook 中 vars、vars_files 定义的变量
4.1 vars
$ vim yaml/vars.yaml- hosts: compute-node remote_user: root vars: pkg: httpd # 定义变量 tasks: - name: install httpd service yum: name={{ pkg }} state=present
复制代码
4.2 vars_files
$ vim /tmp/var.yamlpkg: httpd
复制代码
$ vim install.yml---- hosts: all gather_facts: False vars_files: - /tmp/var.yaml tasks: - name: install httpd service yum: name={{ pkg }} state=present
复制代码
多变量
$ vim hello.yaml ---- name: hosts ip hosts: 192.168.1.190 remote_user: root vars: client: liming country: china tasks: - name: "echo a word" debug: msg: "Hello World, {{client}}, my {{country}} friend!"
复制代码
$ ansible-playbook hello.yaml --syntax-check$ ansible-playbook hello.yamlPLAY [hosts ip] ****************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************ok: [192.168.1.190]
TASK [echo a word] *************************************************************************************************************************ok: [192.168.1.190] => { "msg": "Hello World, liming, my china friend!"}
PLAY RECAP *********************************************************************************************************************************192.168.1.190 : ok=2 changed=0 unreachable=0 failed=0
复制代码
$ ansible-playbook hello.yaml -e "client=xiaogang, country=japan"$ ansible-playbook hello.yaml -e '{"client": "xiaogang", "country": "japan"}'
复制代码
变量值为列表
$ vim hello.yaml---- name: hosts ip hosts: localhost remote_user: root vars: client: liming country: china tasks: - name: "echo a word" debug: msg: "Hello World, {{client[0]}} {{client[1]}} and {{client[0]}},my {{country}} friends!"
复制代码
$ ansible-playbook hello.yaml -e '{"client": ["xiaohua""john","hugo"], "country": "japan"}'
复制代码
$ vim clientscountry: chinaclient:- liming- xiaogang- xuanzi
复制代码
ansible-playbook hello.yaml -e "@clients"
复制代码
4.3 vars_prompt(可实现人机交互)
$ vim print.yml---- hosts: test70 remote_user: root vars_prompt: - name: "your_name" prompt: "What is your name" private: no #当该值为yes,则用户的输入不会被打印,否则反之。 - name: "your_age" prompt: "How old are you" private: no tasks: - name: output vars debug: msg: Your name is {{your_name}},You are {{your_age}} years old.
复制代码
5. 注册变量
$ vim date.yaml---- hosts: localhost remote_user: root tasks: - name: show date shell: "/bin/date" register: date # 注册一个变量 - name: Record time log shell: "echo {{ date.stdout }} > /tmp/date.log"
复制代码
$ ansible-playbook date.yaml $ cat /tmp/date.log Mon Feb 17 00:23:47 CST 2020
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 2
版权声明: 本文为 InfoQ 作者【ghostwritten】的原创文章。
原文链接:【http://xie.infoq.cn/article/752fe33ae9e92a4d63bd83d4b】。文章转载请联系作者。
ghostwritten
关注
还未添加个人签名 2018.11.14 加入
还未添加个人简介










评论