本文共 9920 字,大约阅读时间需要 33 分钟。
ansible默认的主机清单是/etc/ansible/hosts文件
主机清单可以手动设置,也可以通过Dynamic Inventory动态生成vi /etc/ansible/hosts[webserver] #使用方括号设置组名www1.example.org #定义被监控主机,这边可以是主机名也可以是IP地址www2.example.org:2222 #冒号后定义远程连接端口,默认是ssh的22端口
如果是名称类似的主机,可以使用列表的方式标识各个主机
[webserver]//[01:50]表示匹配从01到50,后面跟着内置变量,这里定义了ssh的访问的用户名和密码,用于免交互登录www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=abc123[dbbservers]//[a:f]表示支持匹配a到fdb-[a:f].example.org
Inventory中的变量
[webserver]//定义变量http_port(开放的端口信息)和maxRequestsChild(最大进程数)www1.magedu.com http_port=80 maxRequestsChild=808www2.magedu.com http_port=8080 maxRequestsChild=909
[servers:vars]ntp_server=ntp.example.orgnfs_server=nfs.example.org
[apache]http1.example.orghttp2.example.org[nginx]ngx1.example.orgngx2.example.org
//定义一个组名,将刚才定义的两个组名放入,即webservers组包含apache组和nginx组的主机[webservers]apachenginx
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/pythonansible_*_interpreter #这里的" * "可以是 ruby 或 perl 或其他语言的解释器,作用和ansible_python_interpreter 类似。ansible_shell_executable #这将设置 ansibie 控制器将在目标机器上使用的 shell ,覆盖 ansible.cfg 中的配置,默认为 /bin/sh。
YAML:另一种标记语言。是用来写配置文件的语言,非常简洁和强大。
YAML语法和其他语言类似,也可以表达散列表、标量等数据结构。 结构通过 空格 来展示;序列里配置项通过-来代表;Map里键值用:来分隔;YAML的扩展名为yaml大小写敏感
使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格。 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可对象
键值对的集合,又称为映射(mapping)/哈希(hashes)/字典(dictionary)例如: name (键): Example(值)类class:(物品) 对象1:(桌子) 属性(名称,长、宽、高等) 方法(动词,放东西) ... 对象2 对象3
数组
一组按次序排列的值,又称为序列(sequence)/列表(list)例如:-Apple -Orange
纯量
单个的、不可再分的值例如:number: 12.30 sure: true
通过task调用ansible的模板将多个play组织在一个playbook中运行。
Tasks:任务,即调用模块完成的某操作;原理和事务一样,要么一起执行,要么一起不执行。
Variables:变量;声明变量的三个场景:hosts文件中定义、剧本中定义、在命令中加上-e定义。 Templates:模板;定义模板同一格式,解决每个服务可能因格式不一样而不兼容的问题。 Handlers:处理器,当某条件满足时,触发执行的操作。 Roles:角色;将任务分类执行,彼此之间互不干扰。 我们先看一个playbook的示例简单了解一下vim httpd.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=/root/test/httpd.j2 dest=/etc/httpd/conf/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解释------------------------- 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 //yum模块:指定软件包和版本参数;即使用yum安装最新版的httpd - name: write the apache config file template: src=/srv/httpd.j2 dest=/etc/httpd.conf //定义一个httpd的模板(在管理端自己创建) notify: //调用下面的操作 - restart apache //操作的名字,在handlers下定义 - name: ensure apache is running service: name=httpd state=started //输出httpd状态 handlers: //处理器 - name: restart apache //被调用的操作名字 service: name=httpd state=restarted //重启httpd服务
格式:ansible-playbook [yaml文件名]例如:ansible-playbook ping.yml参数:-k(–ask-pass) 用来交互输入ssh密码 -K(-ask-become-pass) 用来交互输入sudo密码 -u 指定用户补充命令:ansible-playbook XXXX.yaml --syntax-check #检查yaml文件的语法是否正确ansible-playbook XXXX.yaml --list-task #检查tasks任务ansible-playbook XXXX.yaml --list-hosts #检查生效的主机ansible-playbook XXXX.yaml --start-at-task='ensure apache is at the latest version' #指定从某个task开始运行
- hosts: webserver #指定主机组,可以是一个或多个组 remote_user: root #指定远程主机执行的用户名
还可以为每个任务定义远程执行用户:
- hosts: mysql remote_user: root tasks: - name: test connection ping: remote_user: mysql #指定远程主机执行tasks的运行用户为mysql执行playbook时: ansible-playbook ping.yml -k
- hosts: mysql remote_user: root become: yes #2.6版本以后的参数,之前是sudo,意思为切换用户运行 become_user: mysql #指定sudo用户为mysql tasks: - name: copy text copy: src=/etc/fstab dest=/home/mysql/fstab.bak执行playbook时: ansible-playbook ping.yml -K
在mysql主机上查看复制的文件
ls /home/mysql
一个yaml文件中可以写多个hosts,定义多个主机组
vim hosts.yaml- 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
1、Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一个任务后再开始。在运行playbook时 (从上到下执行),如果一个host执行task失败, 整个tasks都会回滚,请修正playbook中的错误,然后重新执行,即Task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量,模块执行时幂等的,这意味着多次执行是安全的,因为其结果一定的。
2、每一个task必须有一个名称 name,这样在运行playbook时,从其输出的任务执行信息中可以很好的辨别出是属于哪一个task的。 3、定义一个task,常见的格式: ”module: options" 例如: yum: name =httpd 4、ansible的自带模块中,command模块和shell模块无需使用key=value格式 示例:vim error.yaml- hosts: webserver remote_user: root tasks: - name: disable selinux command: '/sbin/setenforce 0' - name: make sure apache is running service: name=httpd state=started
只要执行命令的返回值不为0,就会报错,tasks停止
修改如下:- 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
Handlers也是一些task的列表, 和一般的task并没有什么区别。
是由通知者进行的notify,如果没有被notify,则Handlers不会执行,假如被notify了 ,则Handlers被执行不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次示例
- 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
也可以引入变量
- 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
例如:编辑如下yamI
vi a.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 -m command -a 'tail /etc/passwd'
vi b.yaml - hosts: mysql remote_user: root vars: - user: gcc01 tasks: - name: add new user user: name={ { user}}
然后执行命令: ansible-playbook b.yml
如: 引用ansible 的固定变量
vi test.yaml - hosts: mysql remote_user: root tasks: - name: copy file copy: content="{ {ansible_all_ipv4_addresses}}" dest=/opt/vars.txt
执行命令: ansible-playbook test.yaml
vi /etc/ansible/hosts#在mysq|组的主机后面添加如下[mysql]192.168.2.5 testvar="2.15"#定义testvar变量的值为2.15
vi test.yaml #添加{ {testvar}}主机变量 - hosts: mysql remote_user: root tasks: - name: copy file copy: content="{ {ansible_all_ipv4_addresses}},{ {testvar}}" dest=/opt/vars.txt
执行命令: ansible-playbook test.yaml
如果需要根据变量、facts (setup) 或此前任务的执行结果来作为某task执行与否的前提时要用到条件测试,在Playbook中条件测试使用。在task后添加when子句即可使用条件测试: when子句支持 jinjia2 表达式或语法,例如:
vi when.yml - hosts: mysql remote_user: root tasks: - name: "shutdown CentOS" command: /sbin/shutdown -h now when: ansible_distribution == "CentOS"
vi when.yml - hosts: mysql remote_user: root tasks: - name: "shut down CentOS 7 systems" command: /sbin/shutdown -r now when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "7"
vi 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")
vi 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")
当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句指明迭代。
- hosts: webserver remote_user: root tasks: - name: "Install Packages" yum: name={ { item }} state=latest with_items: - httpd - mysql-server - php
也可以自己定义
- 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'}
其中:
name表示要操作的软件包的名字; state标识要做什么操作; present:默认的,表示为安装; lastest:安装为最新的版本; absent:表示删除。转载地址:http://srtxn.baihongyu.com/