---
區分多個配置信息。另外選擇性的連續三個點號 ...
用來表示配置文件的結尾#
號註釋代碼tab
混用key/value
的值均須要大小寫敏感key/value
的值可同行寫也可換行寫。同行使用 :
分隔value
但是個字符串,也但是另外一個列表name: task
name
只能包括一個 task
yml
或 yaml
-
大頭# A list of tasty fruits - Apple - Orange - Strawberry - Mango
key
與 value
構成--- # An employee record name: Example Developer job: Developer skill; Elite
key:value
放置於 {} 中進行表示,用 ,
分隔多個 key:value
--- # An employee record {name: Example Developer, job: Developer, skill: Elite}
-
來表明,Map 裏的鍵值對用 :
分隔name: John Smith age: 41 gender: Male spouse: name: Jane Smith age: 37 gender: Female children: - name: Jimmy Smith age: 17 gender: Male - name: Jenny Smith age: 13 gender: Female
#!/bin/bash # 安裝 Apache yum install --quiet -y httpd # 複製配置文件 cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf cp /tmp/vhosts.conf /etc/httpd/conf.d/vhosts.conf # 啓動 Apache,並設置開機自啓 service httpd start chkconfig httpd on
--- - hosts: all tasks: - name: "安裝Apache" yum: name=httpd - name: "複製配置文件 httpd.conf" copy: scr=/tmp/httpd.conf dest=/etc/httpd/conf/ - name: "複製配置文件 vhosts.conf" copy: scr=/tmp/vhosts.conf dest=/etc/httpd/conf.d/ - name: "啓動Apache,並設置開機啓動" service: name=httpd state=started enabled=true
one.example.com one.example.com:two.example.com 192.168.1.120 192.168.1.*
Websrvs:dbsrvs
兩個組的並集Websrvs:&dbsrvs
兩個組的交集webservers:!phoenix
在 websrvs 組,但不在 dbsrvs 組- hosts: websrvs:dbsrvs
- hosts: websrvs remote_user: root tasks: - name: tast connection ping: remote_user: test sudo: yes # 默認sudo 爲 root sudo_suer: wang # sudo 爲 wang
action: module arguments
module: arguments
建議使用key=value
notify
通知給相應的 handlerstags
打標籤,然後可在 ansible-playbook
命令上使用 -t
指定進行調用tasks: - name: disable selinux command: /sbin/setenforce 0
tasks: - name: run this command and ignore the result shell: /usr/bin/somecommand || /bin/true
tasks: - name: run this command and ignore the result shell: /usr/bin/somecommand ignore_errors: True
ansible-playbook <filename.yml> ... [options]
--check
只檢測可能會發生的改變,但不真正執行操做--list-hosts
列出運行任務的主機--limit
主機列表 只針對主機列表中的主機執行-v
顯示過程 -vv -vvvv
更詳細ansible-playbook file.yml ansible-playbook file.yml --check # 只檢測 ansible-playbook file.yml --limit websrvs
--- - hosts:all remote_user: root tasks: - name: create mysql user user: name=mysql system=yes uid=36 - name: create a group group: name=httpd system=yes
--- -hosts: websrvs remote_user: root tasks: - name: Install httpd yum: name=httpd state=present - name: copy configure file copy: src=files/httpd.conf dest=/etc/httpd/conf/ - name: start service service: name=httpd state=started enabled=yes
--- -hosts: websrvs remote_user: root tasks: - name: Install httpd yum: name=httpd state=present - name: copy configure file copy: src=files/httpd.conf dest=/etc/httpd/conf/ notify: restart httpd - name: start service service: name=httpd state=started enabled=yes handlers: - name: restart httpd service: name=httpd status=restarted
--- - hosts: websrvs remote_user: root task: - name: add group nginx tags: user user: name=nginx state=present - name: add user nginx user: name=nginx statepresent group=nginx - name: Install Nginx yum: name=nginx state=present - name: config copy: src=/root/config.txt dest=/etc/nginx/nginx.conf notify: - Restart Nginx - Check Nginx Process handlers: - name: Restart Nginx service: name=nginx state=restarted enabled=yes - name: Check Nginx Process shell: killall -0 nginx > /tmp/nginx.log
--- - hosts: websrvs remote_user: root tasks: - name: Install httpd yum: name=httpd state=present tags: install,always - name: Install configure file copy: src=file/httpd.conf dest=/etc/httpd/conf/ tags: conf,always - name: start httpd service tags: service service: name=httpd state=started enabled=yes
ansible-playbook --tags install,service httpd.yml
--list-tags
ansible-playbook --list-tags httpd.yml
--skip-tags
跳過指定的標籤ansible-playbook --skip-tags always httpd.yml
ansible setup facts
遠程主機的全部變量均可直接調用/etc/ansible/hosts
中定義
ansible-playbook -e varname=value file.yml
key=value
http_port=80
{{ variable_name }}
調用變量,且變量名先後必須有空格,有時用 "{{ variable_name }}"
才生效ansible-playbook test.yml -e "hosts=www user=test"
按照不一樣的方式優先級爲:命令行,playbook定義變量文件,playbook定義變量,hosts定義私有變量,hosts定義公共變量mysql
testvars.ymllinux
--- - hosts: websrvs remote_user: root tasks: - name: create file copy: content={{ var }} dest=/tmp/file.txt
# ansible-playbook -e "var=command" testvars.yml # ansible websrvs -m shell -a 'cat /tmp/file.txt' 192.168.2.132 | CHANGED | rc=0 >> command 192.168.2.131 | CHANGED | rc=0 >> command
--- - hosts: websrvs remote_user: root vars_files: - vars.yml tasks: - name: create file copy: content={{ var.content }} dest=/tmp/file.txt
var: content: vars.yml
# ansible-playbook testvars.yml # ansible websrvs -m shell -a 'cat /tmp/file.txt'192.168.2.131 | CHANGED | rc=0 >> vars.yml 192.168.2.132 | CHANGED | rc=0 >> vars.yml
--- - hosts: websrvs remote_user: root vars: var: {content: playbook} tasks: - name: create file copy: content={{ var.content }} dest=/tmp/file.txt
# ansible-playbook testvars.yml # ansible websrvs -m shell -a 'cat /tmp/file.txt'192.168.2.132 | CHANGED | rc=0 >> playbook 192.168.2.131 | CHANGED | rc=0 >> playbook
/etc/ansible/hosts
中定義--- - hosts: websrvs remote_user: root tasks: - name: create file copy: content={{ var }} dest=/tmp/file.txt
[websrvs] 192.168.2.131 var=hosts_websrvs1 192.168.2.132 var=hosts_websrvs2
# ansible-playbook testvars.yml # ansible websrvs -m shell -a 'cat /tmp/file.txt' 192.168.2.131 | CHANGED | rc=0 >> hosts_websrvs1 192.168.2.132 | CHANGED | rc=0 >> hosts_websrvs2
[websrvs] 192.168.2.131 192.168.2.132 [websrvs:vars] var=hosts_websrvs_vars
# ansible-playbook testvars.yml # ansible websrvs -m shell -a 'cat /tmp/file.txt' 192.168.2.131 | CHANGED | rc=0 >> hosts_websrvs_vars 192.168.2.132 | CHANGED | rc=0 >> hosts_websrvs_vars
# children 底下爲父羣組 zabbix-agent 的子羣組 # vars底下爲羣組共同便變量,包括已定義變量和自定義變量 [zabbix-agent:children] # 父羣組 test1 # 子羣組1 test2 # 子羣組2 [test1] # 子羣組1 192.168.2.13[0:2] # 遠端服務器 IP 列表 [test1:vars] # 子羣組1 參數 ansible_ssh_user=root # 遠端 ssh 服務器用戶 ansible_ssh_pass="test1123" # 遠端 ssh 服務器密碼 ansible_ssh_port=22 # 遠端 ssh 服務器端口 [test2] # 子羣組2 192.168.2.10[1:3] # 遠端服務器 IP 列表 192.168.2.11{1:3] # 遠端服務器 IP 列表 [test2:vars] # 子羣組2 參數 ansible_ssh_user=root # 遠端 ssh 服務器用戶 ansible_ssh_pass="test2123" # 遠端 ssh 服務器密碼 ansible_ssh_port=22 # 遠端 ssh 服務器端口
--- - hosts: zabbix-agent # /etc/ansible/hosts 羣組名 gather_facts: no # 跳過檢查 remote_user: root # 遠端服務器用戶 # tasks: # 任務 # - name: judge a file or dir is exits # 判斷該文件是否存在 # shell: /etc/zabbix/zabbix_agentd.conf # ignore_errors: True # 忽略報錯 # register: result # 定義變量 - name: ssh-copy # 複製 ssh 公鑰到遠端主機 authorized_key: user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub')}}" tags: # 標籤 - sshkey - name: CentOS6 install zabbix-agent rpm # 安裝 zabbix-agent 客戶端 rpm 包 yum: name=http://repo.zabbix.com/zabbix/3.0/rhel/6/x86_64/zabbix-agent-3.0.0-2.el6.x86_64.rpm state=present when: # 判斷系統及版本號 - ansible_distribution == "CentOS" - ansible_distribution_major_version == "6" # - result|failed # 判斷該文件不存在 - name: CentOS7 install zabbix-agent rpm yum: name=http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.0-1.el7.x86_64.rpm state=present when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "7" # - result|failed # 判斷該文件不存在 - name: configure Server IP # 配置自動註冊 zabbix-server 端IP shell: sed -i 's/Server=.*/Server=192.168.2.160/' /etc/zabbix/zabbix_agentd.conf - name: configure ServerActive IP # 配置自動註冊 zabbix-server 端IP shell: sed -i 's/ServerActive=.*/ServerActive=192.168.2.160/' /etc/zabbix/zabbix_agentd.conf - name: configure HostMetadata # 配置自動註冊 key/value 值 shell: sed -i 's/# HostMetadata=/HostMetadata=zabbixs/' /etc/zabbix/zabbix_agentd.conf - name: system configure Hostname # 配置當前服務器的主機名 shell: host=`hostname`;sed -i 's/Hostname=Zabbix server/Hostname='$host'/' /etc/zabbix/zabbix_agentd.conf - name: start service # 啓動 zabbix-agent 服務 service: name=zabbix-agent state=started enabled=true
[defaults] host_key_checking = False
export ANSIBLE_HOST_KEY_CHECKING=False