首页 > 其他分享 >9.ansible之定义变量

9.ansible之定义变量

时间:2023-05-17 22:55:36浏览次数:32  
标签:变量 定义 ansible root yml db04 name

Ansible支持十几种定义变量的方式,其中常用的有如下:

  • Inventory变量
  • Host Facts变量
  • Register变量
  • Playbook变量
  • Playbook提示变量
  • 变量文件
  • 命令行变量

1)Inventory(在主机清单配置文件中定义变量)

[root@db04 ansible]# cat hosts
[test]
db01 myvar1="hello the world" myvar2="content"
db02

这里写一个yml文件

[root@db04 ansible]# cat inventory_var.yml 
---
- hosts: db01
  tasks:
    - name: create a file with var.
      shell: echo {{myvar1}}>/tmp/{{myvar2}}

从yml文件中可以看出,myvar1在主机清单文件中被赋值为"hello the world", myvar2变量被赋值为"content"

然后再执行这个yml文件:ansible-playbook  inventory_var.yml

2) Host Facts变量(可以直接调用ansible收集系统信息)

[root@db04 ansible]# cat facts_var.yml 
---
- hosts: db01
  tasks:
    - name: Use facts info.
      copy:
        content: "{{ansible_hostname}}:{{ansible_bios_version}}"
        dest: /tmp/facts.txt
[root@db04 ansible]# 

3)register语句可以将某个命令的执行结果保存在变量中

[root@db04 ansible]# cat register.yml 
---
- hosts: db01
  tasks:
    - name: save shell result to a variable.
      shell:  hostname
      register: myvar
    - name: print the varialbe's value through debug.
      debug:
        msg:  "{{myvar}}"

4)Playbook变量(使用vars关键词可以在playbook内定义变量)

[root@db04 ansible]# cat playbook_var.yml 
---
- hosts: db01
  vars:
    iname:  heal
    ipss: '123'
  tasks:
    - name: Use variables create user.
      user:
        name: "{{iname}}"
        password: "{{ipss|password_hash('sha512')}}"
[root@db04 ansible]# 

5)剧本提示变量,这个变量类似于shell中的read命令

[root@db04 ansible]# cat prompt_var.yml 
---
- hosts: db01
  vars_prompt:
    - name: iname
      prompt: "请输入用户名"
      private:  no
    - name: ipasswd
      prompt: "请输入密码"
      private: yes
  tasks:
    - name: create users
      user:
        name: "{{iname}}"
        password:  "{{ipasswd|password_hash('sha512')}}"

6)单独定义变量文件(variables.yml),在playbook中用vars_files调用该文件

[root@db04 ansible]# cat variables.yml 
---
iname:  cloud
ipass:  '123'
[root@db04 ansible]# cat playbook_var.yml 
---
- hosts: db01
  vars:
    iname:  heal
    ipss: '123'
  tasks:
    - name: Use variables create user.
      user:
        name: "{{iname}}"
        password: "{{ipss|password_hash('sha512')}}"

7) 执行ansible-playbook命令时使用-e参数定义变量

[root@db04 ansible]# cat command_var.yml 
---
- hosts: db01
  tasks:
    - name: create a user
      user:
        name: "{{iname}}"
        password: "{{ipass|password_hash('sha512')}}"
[root@db04 ansible]# 
[root@db04 ansible]# ansible-playbook command_var.yml  -e iname=tiechui -e ipass='123'

 

标签:变量,定义,ansible,root,yml,db04,name
From: https://www.cnblogs.com/zmc60/p/17410587.html

相关文章

  • 27、在 Java 中,为什么不允许从静态方法中访问非静态变量?
    静态变量属于类本身,在类加载的时候就会分配内存,可以通过类名直接访问;非静态变量属于类的对象,只有在类的对象产生时,才会分配内存,通过类的实例去访问;静态方法也属于类本身,但是此时没有类的实例,内存中没有非静态变量,所以无法调用。......
  • linux中通过PS1变量控制提示符的颜色
    1.何谓提示符的颜色.如下图用户名的颜色,@符号的颜色,主机名的颜色,当前目录的颜色都可以通过变量PS1来控制2.在/home/sgj/.bashrc中设置PS1的值PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$'3.使用source命令......
  • JVM(四)虚拟机栈(一)栈帧结构:局部变量表与操作数栈
    JVM(四)虚拟机栈(一)栈帧结构:局部变量表与操作数栈1虚拟机栈1.1简介虚拟机栈出现的背景:由于跨平台性的设计,Java的指令都是根据栈来设计的,不同平台的CPU架构不同,所以不能基于寄存器。这样做的优点是跨平台,指令集更小,编译器容易实现,缺点是性能下降,实现同样的功能需要更多的指令。......
  • VSCode上的代码变量命名工具插件,让你的开发效率倍增!
    本篇文章主要讲解VSCode上的代码变量命名工具插件chtml代码命名工具的使用。日期:2023年5月15日vscode版本1.78及以上转载地址:https://blog.csdn.net/weixin_46078894,已获作者同意!插件说明CHTML是一款在线的代码命名工具,提供变量命名规则库,可以帮助开发者快速选择合适的变......
  • 在ubuntu中为path环境变量添加一个新路径
    1.查看原来的path环境变量的内容echo$PATH2.将路径 /home/sgj/.local/bin添加到path环境变量中#打开/etc/profile问价sudo/etc/profile3.在最后一行输入以下内容路径了。加上之后就是不要忘记原来的路径,最后的:$PATH不要忘记写,要不然你的path环境变量只有你添加的......
  • 软件测试01:软件及分类和缺陷的定义
    软件测试:软件及分类和缺陷的定义软件程序数据文档软件分类层次分类系统软件应用软件组织分类商业软件开源软件结构分类单机软件分布式软件软件缺陷软件缺陷的由来起源于上世纪70年代中期《测试数据选择的原理》《软件测试的艺术》20世纪80......
  • Linux下创建线程报错‘pthread_create’未定义的引用
    报错如下:我查找了网页上的解决方案,发现多数是因为编译链接时没有加-lpthread可是我加了,一直都在用;最终找出问题所在:函数名写错了pthread_create()而不是pthread_creat()细心是一种美好品质,希望我能尽快拥有它。......
  • 【React】自定义水印方法
    创建水印方法:constsetWaterwark=({//使用ES6的函数默认值方式设置参数的默认取值container=document.body,width='250px',height='160px',textAlign='left',textBaseline='bottom',font='15pxM......
  • vue自定义组件——search-box
    github地址:https://github.com/lxmghct/my-vue-components组件介绍props:value/v-model:检索框的值,default:''boxStyle:检索框的样式,default:'position:fixed;top:0px;right:100px;'highlightColor:高亮颜色,default:'rgb(246,186,130)'......
  • 【Echarts】tooltip自定义提示框
    1组件可直接使用。2tooltip:{3show:true,4trigger:'axis',5confine:true,6padding:0,7borderWidth:0,8backgroundColor:'rgba(1,1,1,0)',9//axisPointer:{10......