---
区分多个配置信息。另外选择性的连续三个点号 ...
用来表示配置文件的结尾#
号注释代码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