ansible默认的主机清单是/etc/ansible/hosts文件,主机清单能够手动设置,也能够经过Dynamic Inventory动态生成,通常主机名使用FQDN
vim /etc/ansible/hosts [webserver] #方括号设置组名 www1.example.org #定义被监控主机,这边能够是主机名也能够是IP地址,主机名须要修改/etc/hosts文件 www2.example.org:2222 #冒号后定义远程链接端口,默认是ssh的22端口
若是是名称相似的主机,可使用列表的方式标识各个主机
[webserver] www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123456 [dbbservers] db-[a:f].example.org
[webserver] www1.magedu.com http_port=80 maxRequestsChild=808 www2.magedu.com http_port=8080 maxRequestsChild=909
[servers:vars] ntp_server=ntp.example.org nfs_server=nfs.example.org
[apache] http1.example.org http2.example.org [nginx] ngx1.example.org ngx2.example.org [webservers:children] apache nginx
参数 | 说明 |
---|---|
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 |
ansible_*_interpreter | 这里的"*"能够是ruby 或perl 或其余语言的解释器,做用和ansible_python_interpreter 相似 |
ansible_shell_executable | 这将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的配置,默认为/bin/sh |
YAML:另外一种标记语言。是用来写配置文件的语言,很是简洁和强大。 YAML语法和其余语言相似,也能够表达散列表、标量等数据结构。 结构经过空格来展现;序列里配置项经过-来表明;Map里键值用:来分隔;YAML的扩展名为yaml
基本语法规则php
1.大小写敏感 2.使用缩进表示层级关系 3.缩进时不容许使用Tab键,只容许使用空格。 4.缩进的空格数目不重要,只要相同层级的元素左侧对齐便可
1.对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary) 例如:name:Example Developer 键 值 2.数组:一组按次序排列的值,又称为序列(sequence) / 列表(list) 例如:-Apple -Orange 3.纯量:单个的、不可再分的值 例如:number:12.30 sure:true
name:zhangsan age:20 name:lisi age:22 people:
-name:zhangsan age:20 -name:lisi age:22
Playbook是一种彻底不一样的运用ansible的方式,相似与saltstack的state状态文件。ad-hoc没法持久使用,playbook能够持久使用。 playbook是由一个或多个play组成的列表,play的主要功能在于将事先归并为一组的主机装扮成事先经过ansible中的task定义好的角色。从根本上来说,所谓的task无非是调用ansible的一个module。将多个play组织在一个playbook中,便可以让它们联合起来按事先编排的机制完成某一任务。经过task调用ansible的模板将多个play组织在一个playbook中运行。
名称 | 含义 |
---|---|
Hosts | 执行的远程主机列表 |
Tasks | 任务集,即调用模块完成的某操做 |
Varniables | 变量,内置变量或自定义变量在playbook中调用 |
Templates | 模板,即便用模板语法的文件,好比配置文件等 |
Handlers | 处理器,和notity结合使用,由特定条件触发的操做,知足条件方才执行,不然不执行 |
tags | 标签,指定某条任务执行,用于选择运行playbook中的部分代码 |
Roles | 角色 |
一、playbook使用yaml语法格式,后缀能够是yaml,也能够是yml。 在单一的一个playbook文件中,能够连续三个连子号(---)区分多个play。还有选择性的连续三个点好(...)用来表示play的结尾,也可省略。 二、当行开始正常写playbook的内容,通常都会写上描述该playbook的功能。 三、使用#号注释代码。 四、缩进必须统一,不能空格和tab混用。缩进的级别也必须是一致的,一样的缩进表明一样的级别,程序判别配置的级别是经过缩进结合换行实现的。 五、YAML文件内容和Linux系统大小写判断方式保持一致,是区分大小写的,k/v的值均需大小写敏感 六、k/v的值可同行写也能够换行写。同行使用:分隔。v能够是个字符串,也能够是一个列表 七、一个完整的代码块功能须要最少元素包括 name: task
vim a.yaml - hosts: webserver #定义的主机组,即应用的主机 vars: #定义变量 http_port: 80 max_clients: 200 user: root tasks: #执行的任务 - name: ensure apache is at the latest version yum: pkg=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
ansible-playbook [yaml文件名]
实例演示:python
#执行剧本 ansible-playbook ping.yaml
参数:mysql
-k(–ask-pass) 用来交互输入ssh密码 -K(-ask-become-pass) 用来交互输入sudo密码 -u 指定用户
补充命令:linux
#检查yaml文件的语法是否正确 ansible-playbook nginx.yaml --syntax-check #检查tasks任务 ansible-playbook nginx.yaml --list-task #检查生效的主机 ansible-playbook nginx.yaml --list-hosts #指定从某个task开始运行 ansible-playbook nginx.yaml --start-at-task='Copy Nginx.conf'
实例演示:nginx
vim a.yaml - hosts: webserver #指定主机组,能够是一个或多个组。 remote_user: root #指定远程主机执行的用户名 #执行剧本 ansible-playbook a.yaml
!vim - hosts: mysql remote_user: root tasks: - name: test connection ping: remote_user: mysql #指定远程主机执行tasks的运行用户为mysql #执行playbook剧本 ansible-playbook ping.yml -k #验证语法 ansible-playbook a.yml --syntax-check #执行剧本 ansible-playbook a.yml
!vim - hosts: mysql remote_user: root become: yes #2.6版本之后的参数,以前是sudo,意思为切换用户运行 become_user: mysql #指定sudo用户为mysql #执行playbook剧本 ansible-playbook ping.yml -K #执行剧本 ansible-playbook a.yml
1.Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在全部主机上完成第一个任务后再开始第二个任务。 在运行playbook时(从上到下执行),若是一个host执行task失败,整个tasks都会回滚,请修正playbook 中的错误,而后从新执行便可。 Task的目的是使用指定的参数执行模块,而在模块参数中可使用变量,模块执行时幂等的,这意味着屡次执行是安全的,由于其结果一致。 2.每个task必须有一个名称name,这样在运行playbook时,从其输出的任务执行信息中能够很好的辨别出是属于哪个task的。若是没有定义name,‘action’的值将会用做输出信息中标记特定的task。 3.定义一个task,常见的格式:”module: options” 例如:yum: name=httpd 4.ansible的自带模块中,command模块和shell模块无需使用key=value格式
实例演示:web
vim a.yml - hosts: webserver 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中止 #检查语法 ansible-playbook a.yml --syntax-check #执行剧本 ansible-playbook a.yml #检查结果 rpm -q httpd
修改以下内容:正则表达式
!vim - hosts: webserver 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 #检查语法 ansible-playbook a.yml --syntax-check #执行剧本 ansible-playbook a.yml
vim b.yml - hosts: webserver 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: mysql remote_user: root tasks: - name: copy file to mysql copy: src=/etc/inittab dest=/opt/inittab.back #检查语法 ansible-playbook b.yml --syntax-check #执行剧本 ansible-playbook b.yml
Handlers也是一些task的列表,和通常的task并无什么区别。 是由通知者进行的notify,若是没有被notify,则Handlers不会执行,假如被notify了,则Handlers被执行 无论有多少个通知者进行了notify,等到play中的全部task执行完成以后,handlers也只会被执行一次
实例演示:sql
vim c.yml - hosts: webserver 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 #检查语法 ansible-playbook c.yml --syntax-check #执行剧本 ansible-playbook c.yml
亦可引入一些变量:shell
!vim - hosts: webserver 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 #检查语法 ansible-playbook c.yml --syntax-check #执行剧本 ansible-playbook c.yml
1.经过ansible命令传递apache
实例演示:
#编辑以下yml vim a.yml - hosts: mysql remote_user: root vars: - user: tasks: - name: add new user user: name={{user}} #执行剧本 ansible-playbook a.yml -e "user=testvar" #执行命令查看文件 ansible mysql -m command -a 'tail /etc/passwd'
2.直接在yaml中定义变量---如上handlers示例
3.直接引用一些变量
实例演示:
vim test.yml - hosts: mysql remote_user: root tasks: - name: copy file copy: content="{{ansible_all_ipv4_addresses}}," dest=/opt/vars.txt #执行剧本 ansible-playbook test.yml #在122主机上查看vars.txt文件内容 cat /opt/vars.txt
vim /etc/ansible/hosts #在mysql组的主机后面添加以下内容 [mysql] 192.168.142.122 testvar="42.122" #定义testvar变量的值为42.122 vim test.yml #添加{{testvar}}主机变量 - hosts: mysql remote_user: root tasks: - name: copy file copy: content="{{ansible_all_ipv4_addresses}},{{testvar}}" dest=/opt/vars.txt #执行剧本 ansible-playbook test.yml #在122主机上查看vars.txt文件内容 cat /opt/vars.txt
若是须要根据变量、facts(setup)或此前任务的执行结果来做为某task执行与否的前提时要用到条件测试,在Playbook中条件测试使用when子句。 在task后添加when子句便可使用条件测试:when子句支持正则表达式或语法。
实例演示:
vim when.yml - hosts: mysql remote_user: root tasks: - name: "shutdown CentOS" command: /sbin/shutdown -h now when: ansible_distribution == "CentOS" #执行剧本,执行后会发现192.168.142.122主机已经被关机 ansible-playbook when.yml
vim when.yml - hosts: mysql remote_user: root tasks: - name: "shut down CentOS 6 systems" command: /sbin/shutdown -r now when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "6" #执行剧本 ansible-playbook when.yml
vim when.yml - hosts: mysql remote_user: root tasks: - name: "shut down 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") #执行剧本 ansible-playbook when.yml
vim when.yml - hosts: all vars: exist: "True" tasks: - name: creaet file command: touch /tmp/test.txt when: exist | match("True") - name: delete file command: rm -rf /tmp/test.txt when: exist | match("False") #执行剧本 ansible-playbook when.yml
当有须要重复性执行的任务时,可使用迭代机制。其使用格式为将须要迭代的内容定义为item变量引用,并经过with_items语句指明迭代的元素列表便可。
实例演示:
vim d.yml - hosts: webserver remote_user: root tasks: - name: "Install Packages" yum: name={{ item }} state=latest with_items: - httpd - mysql-server - php #检查语法 ansible-playbook d.yml --syntax-check #执行剧本 ansible-playbook d.yml #在全部主机上查看是否有指定添加的用户 tail -5 /etc/passwd
亦可自行定义:
vim e.yml - hosts: webserver remote_user: root tasks: - name: "Add users" user: name={{ item.name }} state=present groups={{ item.groups }} with_items: - { name:'test1', groups:'wheel'} - { name:'test2', groups:'root'} #检查语法 ansible-playbook e.yml --syntax-check #执行剧本 ansible-playbook e.yml #在全部主机上查看是否有指定添加的用户 tail -5 /etc/passwd
#建立工做目录 mkdir templates #复制配置文件 cp /etc/httpd/conf/httpd.conf templates/httpd.conf.02 #修改httpd.conf.02文件 vim templates/httpd.conf.02 Listen {{http_port}} ServerName {{ansible_fqdn}} MaxClients {{maxClients}} #修改ansible/hosts文件 vim /etc/ansible/hosts [webserver] 192.168.142.121 http_port=80 maxClients=100 192.168.142.122 http_port=8080 maxClients=200 #编写剧本 vim apache.yml - hosts: webserver remote_user: root vars: - package: httpd - service: httpd tasks: - name: install httpd package yum: name={{package}} state=latest - name: install configuration file for httpd template: src=/root/templates/httpd.conf.2 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 #检查语法 ansible-playbook e.yml --syntax-check #执行剧本 ansible-playbook apache.yml
#在两台被管理服务器上查看 grep -i listen /etc/httpd/conf/httpd.conf grep -i maxClient /etc/httpd/conf/httpd.conf grep -i servername /etc/httpd/conf/httpd.conf
在一个playbook中,咱们通常会定义不少个task,若是咱们只想执行其中的某一个task或多个task时就可使用tags标签功能了。 格式以下:
vim hosts.yml - hosts: webserver remote_user: root tasks: - name: Copy hosts file copy: src=/etc/hosts dest=/etc/hosts tags: - only - name: touch file file: path=/opt/hosts state=touch #检查语法 ansible-playbook e.yml --syntax-check #执行剧本 ansible-playbook hosts.yml --tags="only" ansible-playbook hosts.ym
事实上,不光能够为单个或多个task指定同一个tags。playbook还提供了一个特殊的tags为always。做用就是当使用always当tags的task时,不管执行哪个tags时,定义有always的tags都会执行。
实例演示:
vim hosts.yml - hosts: webserver remote_user: root tasks: - name: Copy hosts file copy: src=/etc/hosts dest=/etc/hosts tags: - only - name: touch file file: path=/opt/hosts state=touch tags: - always #检查语法 ansible-playbook e.yml --syntax-check #为了避免影响这次操做删除远程主机中/opt/hosts文件 rm -rf /opt/hosts #执行剧本 ansible-playbook hosts.yml --tags="only"
#分别在两台被管理服务器上去查看文件建立状况 ls /opt/