zoukankan      html  css  js  c++  java
  • ansible-playbook的简单传参方式

    基本语法:
        ansible-playbook -v -i myhost  test.yml -e "name=xiaoming" 
     
    // -v  是看运行细节。要更细节的信息,把-v换成 -vvv
    // myhost 是我们自己写的host文件。也就是说,我们不一定要用/etc/ansible/hosts那个文件。
    // -e 是传参到yml文件里面
    ansible的渲染是依赖于jinja2的。
    所以yml文件里面都是用花括号,表示待渲染的变量:{{  }}
     
    vim  test.yml
    - hosts: web
      gather_facts: no
     
     
      tasks:
      - ping:
      - name: get S/N
        shell: show version | grep S/N | cut -f2 -d":" $2'|sed  's/^[ 	]*//
        register: the_SN
        when: ("CPE") != (node.type)
        run_once: true
      - name: check S/N
        command: /bin/false
        register: result
        when: the_SN["stdout"] is defined and the_SN["stdout"] != (node.serialNum)
        ignore_errors: yes
        run_once: true
      - name: record the wrong S/N
        copy:
          content: '{"failed": true,"changed": false,"msg": "the sn given is not equal to system sn"}'
          dest: __result.json
        when: result is failed
        connection: local
        run_once: true
      - name: is end
        fail: msg='the sn given:{{node.serialNum}} is not equal to system sn:{{the_SN["stdout"]}}'
        when: result is failed
      - name: should goon
        debug: msg="go on..............."
     
    vim  myhost
    [host1]
    192.168.1.100
     
    [host1:vars]
    ansible_ssh_user="user"
    ansible_ssh_port=22
    ansible_ssh_pass="Password"
    

      

    有了以上的数据,我们就可以对host1里面的机子用yml文件做配置了。但是参数-e,后面如果需要很多的参数,那就不方便了。比如这样:
    ansible-playbook -i myhost test.yml -e '{"name":"xiaoming","age":18......}'
    这样写起来就笼长。所以,其实还有一下写法:
    先写一个json文件:test.json
     
    vim test.json
    {"name":"xiaoming","age":18}
    

      

     
    然后这么去使用:
    ansible-playbook -i myhost test.yml -e '@test.json'
    是的,没错。就像微信聊天,扣扣聊天一样,艾特一下它,就可以了。这样,test.json里面的参数,就可以传到yml文件里面了。
     
     
  • 相关阅读:
    CentOS ping: unknown host 解决方法
    chmod 777 修改权限
    Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器[摘抄]
    PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
    unix:/tmp/php-cgi.sock
    Nginx虚拟主机配置教程
    nginx 浏览php的时候会变成下载
    sphinx的配置和管理.No2
    Sphinx以及coreseek的安装及使用 .No1
    76、android:supportsRtl 和 android:layout_marginEnd
  • 原文地址:https://www.cnblogs.com/haiguixiansheng/p/11507279.html
Copyright © 2011-2022 走看看