ansible-playbook的簡單傳參方式


// -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/^[ \t]*//
    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文件里面了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM