Ansible中的变量及加密
生活随笔
收集整理的這篇文章主要介紹了
Ansible中的变量及加密
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、變量命名
只能包含數(shù)字,下劃線,字母
只能用下劃線或字母開頭
二、變量級(jí)別
全局:?? ?從命令行或配置文件中設(shè)定的
paly:?? ?在play和相關(guān)結(jié)構(gòu)中設(shè)定的
主機(jī):?? ?由清單,事實(shí)收集或注冊的任務(wù)
變量優(yōu)先級(jí)設(shè)定:
狹窄范圍有限與廣域范圍
三、變量設(shè)定和使用方式
#1.在playbook中直接定義變量 --- - name: test varhosts: allvars:USER: westosusertasks:- name: create useruser:name: "{{USER}}"state: present ... #2.在文件中定義變量# vim user_list.yml --- USER: westosuser ...vim test.yml --- - name: Create Userhosts: allvars_files:./user_list.ymltasks:- name: create useruser:name: "{{USER}}"state: absent ... #3.使用變量#tasks:- name: create useruser:name: "{{ USER }}" #4.設(shè)定主機(jī)變量和清單變量# #在定義主機(jī)變量和清單變量時(shí)使用vim inventory [list1] 172.25.1.10 [list2] 172.25.1.20 [list3] 172.25.1.10 172.25.1.20 [list1:vars] USER=westosuservim test.yml --- - name: test varhosts: list1tasks:- name: create useruser:name: "{{USER}}"state: present ... #5.目錄設(shè)定變量# group_vars ##清單變量,目錄中的文件名稱與主機(jī)清單名稱一致 host_vars ##主機(jī)變量,目錄中的文件名稱與主機(jī)名稱一致 #6.用命令覆蓋變量# ansible-playbook user.yml -e "USER=hello" #7.使用數(shù)組設(shè)定變量# #vim user_var.yml --- USER:lee:age: 18obj: linuxwestos:age: 20obj: java#vim user.yml - name: Create Userhosts: allgather_facts: novars_files:./user_var.ymltasks:- name: create usershell:echo "{{USER['lee']['age']}}"echo "{{USER.westos.obj}}" #8.注冊變量# #register 把模塊輸出注冊到指定字符串中--- - name: test registerhosts: list1tasks:- name: hostname commandshell:hostnameregister: info- name: show messagesdebug:msg: {{info['stdout']}}" #9.事實(shí)變量# 事實(shí)變量是ansible在受控主機(jī)中自動(dòng)檢測出的變量 事實(shí)變量中還有與主機(jī)相關(guān)的信息當(dāng)需要使用主機(jī)相關(guān)信息時(shí)不需要采集賦值,直接調(diào)用即可 因?yàn)樽兞啃畔橄到y(tǒng)信息所以不能隨意設(shè)定僅為采集信息,故被成為事實(shí)變量--- - name: testhosts: list1tasks:- name: showdebug:msg: "{{ansible_facts['fqdn']}}" ...?練習(xí)腳本:
采集主機(jī)的ip 和主機(jī)名并以
hostname:
ip:
形式輸出到/etc/motd
--- - name: testhosts: list1tasks:- name: infocopy:content: "hostname: {{ansible_facts['fqdn']}}\nip: {{ansible_facts['enp1s0']['ipv4']['address']}}\n"dest: /etc/motd ...測試:
#10.魔法便變量# hostvars: ##ansible軟件的內(nèi)部信息group_names: ##當(dāng)前受管主機(jī)所在組 groups: ##列出清單中所有的組和主機(jī) inventory_hostname: ##包含清單中配置的當(dāng)前授管主機(jī)的名稱?四、JINJA2模板
#介紹
Jinja2是Python下一個(gè)被廣泛應(yīng)用的模版引擎
他的設(shè)計(jì)思想來源于Django的模板引擎,
并擴(kuò)展了其語法和一系列強(qiáng)大的功能。
其中最顯著的一個(gè)是增加了沙箱執(zhí)行功能和可選的自動(dòng)轉(zhuǎn)義功能
五、j2模板書寫規(guī)則
{# /etc/hosts line #} 127.0.0.1 localhost {{ ansible_facts['all_ipv4_addresses'] }} {{ansible_facts['fqdn']}}#for循環(huán)# vim users.yml users:- westos- linux- ansiblevim test.j2 {% for NAME in users %} {{ NAME }} {%endfor%}#if 判定# {% for NAME in users if not NAME == "ansible" %} User number {{loop.index}} - {{ NAME }} {%endfor%}loop.index ##循環(huán)迭代記數(shù)從1開始 loop.index0 ##循環(huán)迭代計(jì)數(shù)從0開始{% for user in students %} name: {{user['name']}} {%if user['age'] is defined%} age: {{user['age']}} {%endif%} {% if user['age'] is not defined %} age: null {% endif%} obj: {{user['obj']}} {%endfor%} #j2模板在playbook中的應(yīng)用# #playbook1 --- - name: test registerhosts: xxxxtasks:- name: create hoststemplate:src: ./xxxx.j2dest: /mnt/hosts#playbook2 --- - name: test.j2hosts: 172.25.0.254vars:students:- name: student1obj: linux- name: student2age: 18obj: linuxtasks:- template:src: ./test.j2dest: /mnt/list練習(xí)腳本
create web vhost
www.westos.com? 80??? ------ > /var/www/html
linux.westos.com 80 ------> /var/www/virtual/westos.com/linux
?
六、Ansible的加密控制
#創(chuàng)建建立文件 1. ansible-vault create westos2. vim westos-vault 123456ansible-vault create --vault-password-file=westos-vault westos#加密現(xiàn)有文件 ansible-vault encrypt test#查看加密文件 ansible-vault view westos ansible-vault view --vault-password-file=westos-valut westos#編輯加密文件ansible-vault edit westos1 ansible-vault edit --vault-password-file=westos-valut westos##解密文件 ansible-vault decrypt westos ##文件永久解密 ansible-vault decrypt westos --output=linux ##文件解密保存為linux##更改密碼 ansible-vault rekey westos1 ansible-vault rekey westos1 --new-vault-password-file=key1#playbook# ansible-playbook apache_install.yml --ask-vault-pass 與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Ansible中的变量及加密的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell 中的运算
- 下一篇: Anisble中的任务执行控制