ansible变量
  TEZNKK3IfmPf 2023年11月14日 70 0

 

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 vars
play vars_prompt
play vars_files
role vars (defined in role/vars/main.yml)
block vars (only for tasks in block)
task vars (only for the task)
include_vars
set_facts / registered vars
role (and include_role) params
include params
extra 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=etyfhzmweadf
openstack-compute2

[control-node]
openstack-control1 filename=control1.txt    # 主机变量
openstack-control2 filename=control2.txt

[openstack:children]
load-node
compute-node
control-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.yaml
pkg: 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.yaml
PLAY [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 clients
country: china
client:
- 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
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月14日   27   0   0 go变量
  TEZNKK3IfmPf   2023年11月14日   24   0   0 cronansible
  TEZNKK3IfmPf   2023年11月14日   20   0   0 nginxtomcatansible
  TEZNKK3IfmPf   2023年11月14日   71   0   0 变量ansible
  TEZNKK3IfmPf   2023年11月14日   84   0   0 scriptansible
  TEZNKK3IfmPf   2024年05月17日   30   0   0 ansible安装
  TEZNKK3IfmPf   2023年11月14日   156   0   0 django模板变量
  TEZNKK3IfmPf   2023年11月14日   60   0   0 命名变量
  TEZNKK3IfmPf   2023年11月14日   25   0   0 变量golang
  TEZNKK3IfmPf   2023年11月14日   61   0   0 命名变量
  TEZNKK3IfmPf   2023年11月14日   25   0   0 java变量
TEZNKK3IfmPf