a=10 echo '$a' 結果$a echo "$a" 結果10
ymal規範(必須嚴格遵照): 字典: key: value(冒號後邊必須有空格) 列表: - (以短橫槓開頭) 如: - attila: shuai olsen: mei movie: - name: 復聯 salary: 10000 結果: [ { attila: 'shuai', olsen: 'mei', movie: [ { name: '復聯', salary: 10000 } ] } ]
Usage: ansible-playbook [options] playbook.yml [playbook2 ...] -C, --check #白跑,執行可是不會有結果
--list-hosts #列出符合的主機
-f FORKS, --forks=FORKS #作併發
--syntax-check #檢查語法
-k, --ask-pass #輸入密碼
!!!必須遵照ymal規範
- hosts: web # 指定運行命令的主機
remote_user: root #能夠不寫,默認是root
tasks: # 任務列表
- name: createuser # 任務1,必須是name
user: name=olsen1 home=/opt/olsen1 uid=666 # 指定模塊
- name: copyfile # 任務2
copy: src=/opt/xxx.txt dest=/opt/xxx1.txt # 執行任務的順序是,當web組裏的全部服務器執行完第一個任務以後在執行第二個
ansible-playbook --syntax-check playbook1.yml # 檢查語法
ansible-playbook playbook1.yml # 真正的執行
# 多個playbook,就是在一個.yml文件中有複製了多個像單個playbook那樣的,瞭解便可
# playbook3.yml文件
- hosts: web tasks: - name: create{{user}} user: name={{user}} ansible-playbook -e user=curry1 playbook3.yml
# 修改host文件 vim /etc/ansible/hosts
[web] 192.168.111.129 user=curry2 192.168.111.130 user=kobe2 ansible-playbook playbook3.yml # 129有了curry2,130有了kobe2
# 修改host文件 vim /etc/ansible/hosts
[web] 192.168.111.129
192.168.111.130 [web:vars] user=curry3 ansible-playbook playbook3.yml # 129和130都有了curry3
# playbook4.yml文件
- hosts: web vars: - user: curry4 tasks: - name: create{{user}} user: name={{user}} ansible-playbook playbook4.yml # 129和130都有了curry4
查看收集信息的命令: ansible 192.168.111.129 -m setup|more
ansible_all_ipv4_addresses #全部的ipv4地址
ansible_all_ipv6_addresses #全部的ipv6地址
ansible_architecture #系統的架構
ansible_date_time #系統時間
ansible_default_ipv4 #默認的ipv4地址
address ip地址 alias 網卡名稱 broadcast 廣播地址 gateway 網關 netmask 子網掩碼 network 網段 ansible_default_ipv6 #默認的ipv6地址
ansible_device_links #系統的磁盤信息
ansible_distribution #系統名稱
ansible_distribution_file_variety #系統的基於公司
ansible_distribution_major_version #系統的主版本
ansible_distribution_version #系統的所有版本
ansible_dns #系統的dns 默認udp 端口53
ansible_domain #系統的域 ldap
ipv4 #ipv4地址
ansible_env #系統的環境
ansible_fqdn #系統的完整主機名
ansible_hostname #系統的簡寫主機名
ansible_kernel #系統的內核版本
ansible_machine #系統的架構
ansible_memtotal_mb #系統的內存
ansible_memory_mb #系統的內存使用狀況
ansible_mounts #系統的掛載信息
ansible_os_family #系統家族
ansible_pkg_mgr #系統的包管理工具
ansible_processor #系統的cpu
ansible_processor_cores #每顆cpu的核數
ansible_processor_count #cpu的顆數
ansible_processor_vcpus #cpu的個數=cpu的顆數*每顆cpu的核數
ansible_python #系統python信息
ansible_python_version #系統python的版本
ansible_system #系統名字
# playbook6.yml文件
- hosts: web tasks: - name: install yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/etc/redis.conf tags: copyfile - name: start service: name=redis state=started ansible-playbook playbook6.yml # 全都執行一遍
ansible-playbook -t copyfile playbook6.yml # 只執行copy那個命令(把bind的ip修改以後執行這步就能夠)
# playbook7.yml文件
- hosts: web tasks: - name: install yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/etc/redis.conf tags: copyfile notify: restart # value必須是要觸發的模塊的value
- name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted ansible-playbook -t copyfile playbook7.yml # 不只執行copy還重啓
vim /etc/redis/conf bind {{ansible_default_ipv4.address}] # 上邊setup中的參數
- hosts: web tasks: - name: install yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=/etc/redis.conf tags: copyfile notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted ansible-playbook -t copyfile playbook8.yml
mkdir templates cp /etc/redis/conf templates/ mv redis.conf{,.j2} = mv redis.conf redis.conf.j2 # 更名爲.j2格式
- hosts: web tasks: - name: install yum: name=redis - name: copyfile # 建立名爲templates文件夾(和playbook9.yml在同一文件夾下)直接寫文件名就能找到
template: dest=/etc/redis.conf src=redis.conf.j2 tags: copy notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted ansible-playbook -t copyfile playbook9.yml
# playbook10.yml文件
- hosts: web tasks: - name: copyfile copy: content="十步殺一人" dest=/tmp/a.txt when: ansible_distribution_major_version=="7" # setup的參數
- name: copyfile copy: content="千里不流行" dest=/tmp/a.txt when: ansible_distribution_major_version=="6" ansible-playbook -t copyfile playbook10.yml
# playbook11.yml文件
- hosts: web tasks: - name: createuser user: name={{item}} # 必須是item
with_items: - attila50 - curry50 - kobe50 ansible-playbook -t copyfile playbook11.yml # 建立了上述三個用戶
# playbook12.yml文件
- hosts: web tasks: - name: createuser user: name={{item}} with_items: - attila60 - curry60 - kobe60 - name: creategroup group: name={{item}} # cat /etc/group 能查看組信息
with_items: - attila60 - curry60 - kobe60 ansible-playbook -t copyfile playbook12.yml # 建立了上述三個用戶和三個組
# playbook12.yml文件
- hosts: web tasks: - name: crateuser user: name={{item.name}} group={{item.group}} with_items: - {"name":attila80,"group":attila80} - {"name":curry80,"group":curry80} - {"name":kobe80,"group":kobe80} ansible-playbook -t copyfile playbook12.yml
爲何要使用roles: - 目錄結構清晰 - 能夠相互調用 在調用文件中寫須要引入的文件 - import_tasks: roles/nginx/tasks/install.yml - 備份方便