运维开发网

ansible模块register/vars

运维开发网 https://www.qedev.com 2020-09-06 12:37 出处:51CTO 作者:菜机运维
register注册变量可以将输出信息作为变量值示例:[[email protected]]#catregister.ymlhosts:PCremote_user:roottasks:name:testshellshell:"echotest>/root/test"register:testvar==================>将shell输出的结果赋值给tes

register注册变量

可以将输出信息作为变量值

示例:

[[email protected] work]# cat register.yml


  • hosts: PC

    remote_user: root

    tasks:

    • name: test shell

      shell: "echo test > /root/test"

      register: testvar ==================>将shell输出的结果赋值给testvar变量

    • name: shell return

      debug:

      var: testvar ======>debug调用testvar

      msg: "{{testvar.end}}" ==============>debug调用testvar的内部的参数

vars_prompt 提示用户输入信息,并写入变量

name变量的名称

prompt 提示用户输入信息

encrypt: "sha512_crypt"使用sha512哈希算法,对字符串哈希,主要用于创建密码

confirm 提示用户再次提交

private是否隐藏用户输入的信息,默认yes,no不隐藏

示例(创建用户):

[[email protected] work]# cat c_user_pass.yml


  • hosts: PA

    remote_user: root

    vars_prompt:

    • name: "user"

      prompt: "Please input your create user name"

      private: no

    • name: "pass"

      prompt: "Please input your password"

      encrypt: "sha512_crypt"

      confirm: yes

      tasks:

    • name: add user

      user:

      name: "{{user}}"

      password: "{{pass}}"

命令行传入变量

--extra-vars 简写 -e 适用与ansible ansible-playbook

传入单个变量值:ansible-playbook var.yml -e 'var1="jack"'

传入多个变量值:ansible-paybook var.yml -e 'var1="jack" var2="ross"' (空格隔开,否则会报错)

传入变量列表:ansible-playbook var.yml -e '{"var_list":["var1","var2","var3",]}'

传入变量文件:ansible-playbook var.yml -e '@/etc/ansible/vars/hero.yml'

示例:

[[email protected] work]# vim var.yml


  • hosts: PC

    remote_user: root

    vars:

    cmd_var1: This is a default var1

    cmd_var2: This is a default var2

    name_list: ["jack","ross","wolf","captain"]

    tasks:

    • name: "var on command line"

      debug:

      msg: "{{cmd_var1}},{{cmd_var2}},{{name_list[2]}}"

    • name: "var in file"

      debug:

      msg: I am {{hero_list[2]}}

变量有默认值的情况下,命令行传入的变量值优先级更高

变量文件:

[[email protected] work]# cat ../vars/hero.yml

#This is a hero list

hero_list:

  • spander-man
  • iron-man
  • captain-Amarican
  • doctor
  • worlf
  • sour

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号