Ansible
文章目录
playbook
tasks:任务列表
两种格式:
(1) action: module arguments
(2) module: arguments 建议使用
注意:shell和command模块后面跟命令,而非key=value
某任务的状态在运行后为changed时,可通过“notify”通知给相应的handlers
任务可以通过"tags“打标签,可在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
或者使用ignore_errors来忽略错误信息
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
运行方式
行playbook的方式
ansible-playbook <filename.yml> … [options]
常见选项
–check -C 只检测可能会发生的改变,但不真正执行操作
–list-hosts 列出运行任务的主机
[175@root playbook]#ansible-playbook --list-hosts httpd.yml
–list-tags 列出tag
–list-tasks 列出task
–limit 主机列表 只针对主机列表中的主机执行
-v -vv -vvv 显示过程
示例
ansible-playbook file.yml --check 只检测
ansible-playbook -C file.yml
ansible-playbook file.yml
ansible-playbook file.yml --limit websrvs
1示例安装http
[175@root ~]#vim yumhttp.yml
- - -
- hosts: all
remote_user: root
tasks:
- name: install
yum : name=httpd
- name: config
copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/
- name: service start
service : name=httpd state=started enabled=yes
2.示例安装mariadb二进制
(tip :msyql与mariadb安装方式有差异)
[175@root ~]#vim install_mysql.yml
---
- hosts: web2
remote_user: root
tasks:
- name: user
user: name=mysql system=yes home=/data/msyql create_home=no shell=/sbin/nologin
- name: unarchive
unarchive: src=/root/mariadb-10.2.25-linux-x86_64.tar.gz dest=/usr/local/ owner=root group=root
- name: mysql link
file: src=/usr/local//mariadb-10.2.25-linux-x86_64 dest=/usr/local/mysql state=link
- name: mysql datadir
file: path=/data/mysql state=directory
- name: mysql data owner group
file: path=/data/mysql owner=mysql group=mysql
- name: mysql database
shell: chdir=/usr/local/mysql/ scripts/mysql_install_db --datadir=/data/mysql --user=mysql
- name: mysql path var
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: config
copy: src=/root/playbook/my.cnf dest=/etc/my.cnf
- name: service file
shell: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: start service
shell: /etc/init.d/mysqld start
handlers和notify结合使用触发条件
相当于创建一个触发器
[root@Centos7 playbook]#vim httpd.yml
---
- hosts: web1
remote_user: root
tasks:
- name: install
yum : name=httpd
- name: config
copy: src=/root/playbook/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 state=restarted
Playbook中tags使用标签
示例:httpd.yml
- hosts: websrvs
remote_user: root
tasks:
- name: Install httpd
yum: name=httpd state=present
- name: Install configure file
copy: src=files/httpd.conf dest=/etc/httpd/conf/
tags: conf
- name: start httpd service
tags: service
service: name=httpd state=started enabled=yes
执行 ansible-playbook -t conf httpd.yml
Playbook中变量使用
变量名:仅能由字母、数字和下划线组成,且只能以字母开头
变量来源:
1 ansible setup facts 远程主机的所有变量都可直接调用
2 在/etc/ansible/hosts中定义
普通变量:主机组中主机单独定义,优先级高于公共变量
公共(组)变量:针对主机组中所有主机定义统一变量
3 通过命令行指定变量,优先级最高
ansible-playbook –e varname=value
4 在playbook中定义
vars:
- var1: value1
- var2: value2
5 在独立的变量YAML文件中定义
6 在role中定义
字母下划线数字组成,开头必须是字母
通过{{ variable_name }} 调用变量,且变量名前后必须有空格,有时用
“{{ variable_name }}”才生效
ansible-playbook –e 选项指定
ansible-playbook test.yml -e “hosts=www user=magedu”
setup变量
查看版本:
[root@Centos7 playbook]#ansible web1 -m setup -a ‘filter=ansible_version’
查看hostname
[root@Centos7 playbook]#ansible web1 -m setup -a ‘filter=ansible_fqdn’
查看内存
[root@Centos7 playbook]#ansible web1 -m setup -a ‘ansible_memtotal_mb’
以下是调用变量举例
示例:变量
示例:var.yml
- hosts: websrvs
remote_user: root
tasks:
- name: install package
yum: name={{ pkname }} state=present
ansible-playbook –e pkname=httpd var.yml
示例:变量
示例:var.yml
- hosts: websrvs
remote_user: root
vars
- username: user1
- groupname: group1
tasks:
- name: create group
group: name={{ groupname }} state=present
- name: create user
user: name={{ username }} state=present
ansible-playbook var.yml
ansible-playbook -e "username=user2 groupname=group2” var2.yml
变量
主机变量
可以在inventory中定义主机时为其添加主机变量以便于在playbook中使用
示例:
[websrvs]
www1.magedu.com http_port=80 maxRequestsPerChild=808
www2.magedu.com http_port=8080 maxRequestsPerChild=909
变量
组变量
组变量是指赋予给指定组内所有主机上的在playbook中可用的变量
示例:
[websrvs]
www1.magedu.com
www2.magedu.com
[websrvs:vars]
ntp_server=ntp.magedu.com
nfs_server=nfs.magedu.com
示例:变量
普通变量
[websrvs]
192.168.99.101 http_port=8080 hname=www1
192.168.99.102 http_port=80 hname=www2
公共(组)变量
[websvrs:vars]
http_port=808
mark=“_”
[websrvs]
192.168.99.101 http_port=8080 hname=www1
192.168.99.102 http_port=80 hname=www2
ansible websvrs –m hostname –a ‘name={{ hname }}{{ mark }}{{ http_port }}’
命令行指定变量:
ansible websvrs –e http_port=8000 –m hostname –a
‘name={{ hname }}{{ mark }}{{ http_port }}’
使用变量文件cat vars.yml
var1: httpd
var2: nginx
cat var.yml
- hosts: web
remote_user: root
vars_files:
- vars.yml
tasks:
- name: create httpd log
file: name=/app/{{ var1 }}.log state=touch
- name: create nginx log
file: name=/app/{{ var2 }}.log state=touch
模板template
jinjia2 语言
创建一个模板。里面内容可以夹杂着变量
类型
字符串:使用单引号或双引号
数字:整数,浮点数
列表:[item1, item2, …]
元组:(item1, item2, …)
字典:{key1:value1, key2:value2, …}
布尔型:true/false
算术运算:+, -, *, /, //, %, **
比较操作:==, !=, >, >=, <, <=
逻辑运算:and,or,not
流表达式:For,If,When
功能: 根据模板块文件生成对应的配置文件
template 必须存放在tempaltes目录下 并且命名为.j2结尾
yaml/yml 文件需和templates目录平级,目录结构如下:
./
├── temnginx.yml
└── templates
└── nginx.conf.j2
示例
- cp /etc/httpd/conf/httpd.conf /playbook/templates/httpd.conf.j2
- vim /playbook/templates/httpd.conf.j2
listen {{ httpd_port }} --修改这一行
3.修改路径
4.[root@Centos7 playbook]#ansible-playbook -e httpd_port=1234 httpd.yml
tips 如果在 要传的文件夹里面用参数尽量在 /etc/ansible/hosts 下增加参数如下
vim httpd.conf.j2
listen {{ httpd_port+100}}
vim /etc/ansible/hosts
[web1]
192.168.41.102 httpd_port=6000
ansible-playbook http.yml
when用法
表示判读
---
- hosts: all
remote_user: root
tasks:
- name: adduser
user: name=centos6 state=present
when: ansible_distribution_major_version == "6"
- name: adduser
user: name=centos7 state=present
when: ansible_distribution_major_version == "7"
迭代 with_items
当有需要重复性执行的任务时,可以使用迭代机制
task 给定元素列表 固定变量item
格式
字符串
字段
模板:vim adduser.yml
---
- hosts: web1
remote_user: root
tasks:
- name: adduser
user: name={{ item }}
#删除用户 user: name={{ item }} state=absent remove=yes
with_items:
- user1
- user2
- user3
嵌套子变量
---
- hosts: web1
remote_user: root
tasks:
- name: addgroup
group: name={{ item }} state=present
with_items:
- group1
- group2
- group3
- name: add user
user: name={{ item.name }} group={{ item.group }} state=present
with_items:
- { name: 'user1',group: 'group1' }
- { name: 'user2',group: 'group2' }
- { name: 'user3',group: 'group3' }
Playbook中template for if
[root@Centos7 playbook]#cat for1.yml
---
- hosts: web1
vars:
ports:
- 81
- 82
- 83
tasks:
- name: config
template: src=templates/server.conf.j2 dest=/data/server.conf
[root@Centos7 playbook]#cat templates/server.conf.j2
{% for port in ports %}
server {
listen {{ port }}
}
{% endfor %}
roles
使用roles只需
要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量、
文件、任务、模板及处理器放置于单独的目录中,并可以便捷地include它们的一
种机制。
角色(roles):角色集合
roles/
mysql/
httpd/
nginx/
memcached/
每个角色,以特定的层级目录结构进行组织
roles目录结构:
playbook.yml
roles/
project/
tasks/
files/
vars/
templates/
handlers/
default/ 不常用
meta/ 不常用
/roles/project/ :项目名称,有以下子目录 以及作用
files/ :存放由copy或script模块等调用的文件
templates/:template模块查找所需要模板文件的目录
tasks/:定义task,role的基本元素,至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
handlers/:至少应该包含一个名为main.yml的文件;其它的文件需要在此 文件中通过include进行包含
vars/:定义变量,至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
meta/:定义当前角色的特殊设定及其依赖关系,至少应该包含一个名为main.yml的文件,其它文件需在此文件中通过include进行包含
default/:设定默认变量时使用此目录中的main.yml文件
示例:
角色实例
1.新建各目录
2.新建各任务
3.新建main.yml 按顺序执行 各文件
4.把所有需要传输的文件放在files 里面
5.最后检验执行,执行文件是放在ansible 文件下面和roles文件夹同级别
安装 nginx
yum install ansible -y
yum install nginx -y
echo www.magedu.com > /etc/nginx/nginx.conf
mkdir /data/ansible/roles/nginx/{tasks,files,templates,vars,handlers} -pv
vim /data/ansible/roles/nginx/tasks/user.yml
- name: user
user: name=nginx system=yes uid=80 group=nginx
vim /data/ansible/roles/nginx/tasks/group.yml
- name: group
group: name=nginx system=yes gid=80
vim /data/ansible/roles/nginx/tasks/install.yml
- name: install
yum: name=nginx
vim /data/ansible/roles/nginx/tasks/config.yml
-name: config
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
vim /data/ansible/roles/nginx/tasks/service.yml
- name: service
service: name=nginx state=started enabled=yes
vim /data/ansible/roles/nginx/tasks/data.yml
- name: copy data file
copy: src=roles/nginx/files/index.html dest=/usr/share/nginx/html/index.html
vim /data/ansible/roles/nginx/tasks/main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: config.yml
- include: data.yml
- include: service.yml
vim /data/ansible/role-nginx.yml
---
#nginx role
- hosts: all
remote_user: root
roles:
- role: nginx
ansible-playbook role-nginx.yml
最后的效果:
curl 192.168.23.128
www.magedu.com
安装memcached
[root@Centos7 roles]#cat memcached/handlers/main.yml
- name: restart service
service: name=memcached state=restarted
[root@Centos7 roles]#cat memcached/tasks/install.yml
- name: install
yum : name=memcached
[root@Centos7 roles]#cat memcached/tasks/config.yml
- name: cinfig file
template: src=memcached.j2 dest=/etc/sysconfig/memcached
notify: restart service
[root@Centos7 roles]#cat memcached/tasks/service.yml
- name: service
service: name=memcached state=started enabled=yes
[root@Centos7 roles]#cat memcached/tasks/main.yml
- include: install.yml
- include: config.yml
- include: service.yml
[root@Centos7 roles]#cat memcached/templates/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb//4 }}"
OPTIONS=""