vim /etc/ansible/hosts
[webserver] # 设置组名
www1.xxxxx.com # 定义被监控的主机,可以是主机名也可以是IP地址
www2.xxxxx.com:22222 # 冒号后定义远程连接端口,默认是ssh的22端口
[webserver]
www[01:50].xxxxxxx.com ansible_ssh_user=root ansible_ssh_pass=123123
# 这句话表示01到50的主机,可以使用免交互的方式进行管理
[dbbservers]
db-[a:f].xxxxxxx.com # 支持匹配a b c d e f
1)主机变量
[webserver]
www1.xxxxxxx.com http_port=80 maxRequestsChild=808
www2.xxxxxxx.com http_port=8080 maxRequestsChild=909
2)组变量
[servers:vars]
ntp_server=ntp.xxxxxx.com
nfs_server=nfs.xxxxxx.com
3)组嵌套
[apache]
http1.xxxxxxx.com
http2.xxxxxxx.com
[nginx]
ngx1.xxxxxxx.com
ngx2.xxxxxxx.com
[webservers:children]
apache
nginx
4)inventory变量参数
参数 说明
ansible_ssh_host 将要连接的远程主机名,与你想要设定的主机的别名不同的话,可通过此变量设置
ansible_ssh_port ssh端口号,如果不是默认的端口号,通过此变量设置
ansible_ssh_user 默认的ssh用户名
ansible_ssh_pass ssh密码(这种方式并不安全,强烈建议使用--ask-pass或SSH密钥)
ansible_ssh_private_key_file ssh使用的私钥文件,适用于有多个密钥,而你不想用SSH代理的情况
ansible_ssh_common_args 此设置附加到sftp,scp和ssh默认命令行
ansible_sftp_extra_args 此设置附加到默认sftp命令行
ansible_scp_extra_args 此设置附加到默认scp命令行
ansible_ssh_extra_args 此设置附加到默认ssh命令行
ansible_ssh_pipelining 确定是否使用SSH管道,可以覆盖ansible.cfg中的设置
ansible_shell_type 目标系统的shell类型,默认情况下,命令的执行使用”sh“语法,可设置为”csh“或”fish“
ansible_python_interpreter 目标主机的python路径,适用于的情况系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如 \*BSD, 或者 /usr/bin/python。不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26)
ansible_*_interpreter 这里的*可以是ruby或Perl或者其他语言的解释器
ansible_shell_executable 将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的
例如:name:example Developer
键 值
例如: - football
- basketball
例如:number:33.33
sure:true
YAML例子
name:zhangsan
age:21
name:lisi
age:22
people:
- name:zhangsan
age:21
-name:lisi
age:22
playbook配置文件使用了YAML语法,具有简介明了、结构清晰等特点,playbook配置文件类似于shell脚本,是一个YAML格式的文件,用于保存针对特定需求的任务列表,之前介绍的ansible命令虽然可以完成各种任务,但是当配置一些复杂的任务时,逐条输入就显得效率非常地下了,更有效的方案是在playbook配置文件中放置所有的任务代码,利用ansible-playbook命令执行该文件,可实现自动化运维,YAML文件的扩展名通常为.yaml或者.yml
- hosts: websever 定义的主机组 即应用的主机
vars: 定义变量
http_port: 80
max_clients: 200
user: root
tasks: 执行的任务
- name: ensure apache is at the latest version
yum: name=httpd state=latest
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
notify: 调用
- restart apache
- name: ensure apache is running
service: name=httpd state=started
handlers: 被调用的
- name: restart apache
service: name=httpd state=restarted
执行一个playbook
ansible-playbook [yaml文件名]
例如:
ansible-playbook ping.yml
-k (-ask-pass)用来交互输入ssh密码
-K(-ask-become-pass)用来交互输入sudo密码;-u:指定用户
补充命令:
ansible-playbook nginx.yaml --syntax-check # 检查yaml文件的语法
ansible-playbook nginx.yaml --list-task # 检查tasks任务
ansible-playbook nginx.yaml --list-hosts # 检查生效的主机
ansible-playbook nginx.yaml --start-at-task='Copy Nginx.conf' # 从某一个task任务开始
1)hosts和users介绍
- hosts: mysql # 指定主机组,可以是一个或多个组
remote_user:root # 指定远程主机执行的用户名
2)为每个任务定义远程执行用户
- hosts: mysql
remote_user: root
tasks:
- name: test connection
ping:
remote_user: root
- hosts:mysql
remote_user: root
become: yes
become_user: mysql
4)tasks列表和action
- hosts: 192.168.126.20
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
- name: make sure apache is running
service: name=httpd state=started
play中只要执行命令的返回值不为0,就会报错,tasks停止
- hosts: mysql
remote_user: root
tasks:
- name: disable selinux
command: '/sbin/setenforce 0'
ignore_errors: True # 忽略错误,强制返回成功
- name: make sure apache is running
service: name=httpd state=started
另一个示例:
- hosts: mysql
remote_user: root
tasks:
- name: create nginx group
group: name=nginx system=yes gid=208
- name: create nginx user
user: name=nginx uid=208 group=nginx system=yes
- hosts: test01
remote_user: root
tasks:
- name: copy file to mysql
copy: src=/etc/inittab dest=/opt/inittab.bak
5)Handlers
- hosts: mysql
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd state=latest
- name: install configuration file for httpd
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
这里也可以引入变量
- hosts: mysql
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{package}} state=latest
- name: install configuration file for httpd
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name={{service}} state=started
handlers:
- name: restart httpd
service: name={{service}} state=restarted
playbook使用变量的方法
1、通过ansible命令传递
例如:编译如下yaml
- hosts: mysql
remote_user: root
vars:
- user:
tasks:
- name: add new user
user: name={{user}}
执行命令:
ansible-playbook a.yml -e “user=testvar”
查看:
ansible mysql -a ‘tail /etc/passwd’
2、直接在yaml中定义变量—如上handlers示例
3、直接引用一些变量
如:引用ansible的固定变量
vim test.yml
- hosts: test01
remote_user: root
tasks:
-name: copy file
copy: content={{ansible_all_ipv4_addresses}} dest=/opt/1.txt
4、引用主机变量
vim /etc/ansible/hosts
在mysql组的主机后面添加如下
[mysql]
192.168.126.40 testvar="8080" # 定义testvar变量的值为8080
vim test.yml
- hosts: mysql
remote_user: root
tasks:
-name: copy file
copy: content={{ansible_all_ipv4_addresses}} {{testvar}} dest=/opt/1.txt
5、条件测试
单条件判断:
vim when.yml
- hosts: test01
remote_user: root
tasks:
- name: shutdown centos
command: /sbin/shutdown -r now # h:关机,r:重启
when: ansible_distribution == "CentOS"
多条件判断:
vim when.yml
- hosts: test01
remote_user: root
tasks:
- name: shutdown centos7 systems
command: /sbin/shutdown -r now
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
组条件判断:
vim when.yml
- hosts: test01
remote_user: root
tasks:
- name: shutdown CentOS 6 and Debian 7 systems
command: /sbin/shutdown -t now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
自定义变量进行条件测试
vim when.yml
- hosts: all
vars:
exist: "True"
tasks:
- name: create file
command: touch /tmp/test.txt
when: exist | match("True")
- name: delete file
command: rm -rf /tmp/test.txt
when: exist | match("False")
6、迭代
- hosts: test
remote_user: root
tasks:
- name: add user
user: name={{item}}
with_items:
- tw
- gcc
- yy
也可以自己定义
- hosts: test01
remote_user: root
tasks:
- name: add user
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name: 'test1', groups: 'wheel'}
- { name: 'test2', groups: 'root'}