ansible之判断语句的用法
判断语句:
实践案例1: 根据不同操作系统,安装相同的软件包
Centos: httpd
Ubuntu: httpd2
[root@centos7 project1]# cat when.yml - hosts: webservers tasks: - name: Install httpd Server yum: name=httpd state=present when: ansible_distribution == "CentOS" - name: Install httpd server apt: name=httpd2 state=present when: ansible_distribution == "Ubuntu"
2. 所有web主机名的添加到阿里云仓库, 其余的都跳过添加: 可以用and 和or 做多次条件判断
# is match 是做匹配的意思
[root@centos7 project1]# cat when2.yml --- - hosts: all tasks: - name: Add Nginx Repos yum_repository: name: nginx_tet description: EPEL YUM repo baseurl: http://mirrors.cloud.aliyuncs.com/epel/7/$basearch gpgcheck: no when: (ansible_hostname is match "web*") or (ansible_hostname is match "lb*")
3. 循环语句:
实践案例一: 使用循环启动多个服务
#注意: {{ item }} 是变量的意思
with_items: 是上面变量里面的内容! 这里的意思就是启动httpd 和mariadb2个服务,如果还要启动samba就是 - samba
[root@centos7 project1]# cat when3.yml - hosts: webservers tasks: - name: Start httpd systemd: name={{ item }} state=started with_items: - httpd - mariadb
实践二:批量安装软件包的方法: 这里也可以使用上面用到的{{ item }} 这个变量来实现
[root@centos7 project1]# cat 5.yml - hosts: webservers tasks: - name: ensure a list of packages installed yum: name= "{{ packages }}" state=present vars: packages: - httpd - httpd-tools
4.
实践案例三: 使用字典循环方式创建用户和批量拷贝文件 (重要!!!)
#注意!!! name={{ item.name }} 不能有空格,不然就无法成功
#注意, 批量的时候item.name 就等于下面的循环列表中的name
[root@centos7 project1]# cat 6.yml - hosts: webservers tasks: - name: Add Users user: name={{ item.name }} groups={{ item.groups }} state=present with_items: - { name: 'testuser1', groups: 'bin' } - { name: 'testuser2', groups: 'root' } [root@centos7 project1]# cat with4.yml - hosts: webservers tasks: - name: Copy Rsync configure and Rsync passwd copy: src={{ item.src }} dest={{ item.dest }} mode={{ item.mode }} with_items: - { src: "./rsyncd.conf", dest: "/etc/rsyncd.conf", mode: "0644" } - { src: "./rsync.passwd", dest: "/tmp/etc/rsync.passwd", mode: "0600" }
5.handlers注意事项
#无论多少个task通知了相同的handlers handlers仅会在所有tasks结束后运行一次
# 只有task发生改变了才会通知handlers 没有改变则不会出发handlers
# 不能使用handlers替代tasks
[root@centos7 project1]# cat han.yml - hosts: webservers vars: - http_port:8083 tasks: - name: Install Http Server yum: name=httpd state=present - name: Configure httpd server template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf notify: - Restart Httpd Server - Restart PHP Server - name: start httpd server service: name=httpd state=started enabled=yes handlers: - name: Restart Httpd Server systemd: name=httpd start=restarted - name: Restart PHP Server systemd: name=php-fpm start=restarted
6. tags标签(用于调试的场景下)
[root@centos7 project1]# cat tag.yml - hosts: webservers vars: - http_port:8083 tasks: - name: Install Http Server yum: name=httpd state=present tags: - install_httpd - httpd_server - name: Configure httpd server template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf notify: - Restart Httpd Server - Restart PHP Server tags: - confiure_httpd - httpd_server - name: start httpd server service: name=httpd state=started enabled= tags: service_httpd handlers: - name: Restart Httpd Server systemd: name=httpd start=restarted - name: Restart PHP Server systemd: name=php-fpm start=restarted [root@centos7 project1]# ansible-playbook tag.yml --list-tags #查看tag.yml内有几个标签 [root@centos7 project1]# ansible-playbook tag.yml -t httpd_server #运行一个标签内的任务 [root@centos7 project1]# ansible-playbook tag.yml -t httpd_server,service_httpd #运行多个标签内的任务,注意用逗号隔开就行了
7. include的用法,就是吧每个yml写成单独的一个功能,方便调用和规范化
include_tasks
[root@centos7 project1]# cat task.yml - hosts: webservers vars: - http_port: 8081 tasks: - include_tasks: task_install.yml - include_tasks: task_start.yml [root@centos7 project1]# cat task_install.yml - name: Install http server yum: name=httpd state=present [root@centos7 project1]# cat task_start.ym - name: start httpd servers service: name=httpd state=started enabled=yes
ansible 之jinja2模板的使用
jinja2的模板可以用变量和for的循环语句, 他的语法和ajax的语法一样
1. 案例: 使用变量 + for循环的方式实现功能
[root@gukai project1]# cat jinja_nginx.yml - hosts: webservers vars: - http_port: 80 - server_name: www.oldboy.com tasks: - name: copy nginx configure template: src=./oldboy.conf.j2 dest=/etc/nginx/conf.d/oldboyedu_proxy.conf [root@gukai project1]# cat oldboy.conf.j2 upstream {{ server_name }} { {% for i in range(1,10) %} server 172.16.1.{{i}}:{{http_port}}; {% endfor %} server { listen {{ http_port }}; server_name {{ server_name }}; location / { proxy_pass http://{{ server_name }}; proxy_set_header Host $http_host; } } [root@gukai project1]# ansible-playbook jinja_nginx.yml #最后执行任务推送过去
2.案例:############jinja2版本,通过if else 判断语句生成不同的模板keeplive方法
[root@gukai project1]# cat kee.conf.j2 global_defs { route_id {{ ansible_hostname }} } vrrp_instance VI_1 { {% if ansible_hostname =="web01" %} state MASTER priority 150 {% elif ansible_hostname == "web02" %} state BACKUP priority 100 {% endif %}
3. 案例:
###########使用ansible jinja IF 生成不同的mysql配置文件(自定义变量)
#注意,使用jinja模板 发送配置文件到目标主机上需要使用template 命令
[root@gukai project1]# cat jinja_mysql.yml - hosts: webservers gather_facts: no vars: PORT: 13306 #PORT: false tasks: - name: Coyp mysql confiure template: src=./my.cnf.j2 dest=/tmp/my.cnf [root@gukai project1]# cat my.cnf.j2 # This is mysql co configure {% if PORT %} bind-address=0.0.0.0:{{ PORT }} {% else %} bind-address=0.0.0.0:3306 {% endif %} [root@gukai project1]# ansible-playbook jinja_mysql.yml
ansible 之role的使用方法
[root@gukai ~]# cd /etc/ansible/roles/ [root@gukai roles]# mkdir nfs/{tasks,handlers,templates} -pv mkdir: 已创建目录 "nfs" mkdir: 已创建目录 "nfs/tasks" mkdir: 已创建目录 "nfs/handlers" mkdir: 已创建目录 "nfs/templates"
#这个就是创建角色 [root@gukai roles]# cat site.yml #对那个目录下的功能起作用 - hosts: webservers roles: - nfs
[root@gukai tasks]# cat config.yml #配置服务 - name: configure nfs-utils server template: src=./exports.j2 dest=/etc/exports owner=root group=root mode=0644 notify: Restart NFS Server [root@gukai tasks]# cat install.yml #安装服务 - name: Install NFS-utils Server yum: name=nfs-utils state=present [root@gukai tasks]# cat start.yml #启动服务 - name: Start NFS server systemd: name=nfs state=started enabled=yes
[root@gukai tasks]# cat main.yml #main.yml是主函数的意思下面包含了各个功能的 - include_tasks: install.yml #- include_tasks: config.yml - include_tasks: start.yml #include_tasks 是包含使用那些yml文件,目的是吧每个功能都单独区分开来 [root@gukai tasks]# ansible-playbook site.yml #运行playbook
#######################安装memcache
[root@gukai roles]# mkdir memcached/{tasks,handlers,templates} -pv mkdir: 已创建目录 "memcached" mkdir: 已创建目录 "memcached/tasks" mkdir: 已创建目录 "memcached/handlers" mkdir: 已创建目录 "memcached/templates"
[root@gukai roles]# tree memcached/ #目录结构 memcached/ ├── handlers │ └── main.yml ├── tasks │ ├── config.yml │ ├── install.yml │ ├── main.yml │ └── start.yml └── templates └── redis.j2
[root@gukai roles]# cat memcached/handlers/main.yml - name: restart redis server systemd: name=redis state=restarted
[root@gukai roles]# cat memcached/tasks/config.yml - name: copy redis server configure template: src=redis.j2 dest=/tmp/redis.conf notify: restart redis server [root@gukai roles]# cat memcached/tasks/start.yml - name: start redis server systemd: name=redis state=started enabled=yes [root@gukai roles]# cat memcached/tasks/install.yml - name: Install memcached server yum: name=redis state=present #############以上是功能的模块 [root@gukai roles]# cat memcached/tasks/main.yml - include_tasks: install.yml - include_tasks: config.yml - include_tasks: start.yml #主函数,包含以上功能代码
[root@gukai roles]# cat memcached/templates/redis.j2 PORT="12333" USER="redis" CACHESIZE="{{ ansible_memtotal_mb //2 }}" #套用jinja2的模板语法和变量的语法实现特定的信息 [root@gukai roles]# cat site.yml - hosts: webservers roles: - nfs - hosts: webservers roles: - memcached #加载进去memcached模块 [root@gukai roles]#ansible-playbook site.yml
##############将功能整合到一起的写法
[root@gukai roles]# tree rsync/ rsync/ ├── handlers │ └── main.yml ├── tasks │ └── main.yml └── templates ├── rsyncd.conf.j2 └── rsyncd.password.j2
[root@gukai roles]# cat rsync/handlers/main.yml - name: restart rsync server systemd: name=rsyncd state=restarted [root@gukai roles]# cat rsync/tasks/main.yml - name: install rsync server yum: name=rsync state=present - name: configure rsync server template: src={{ item.src }} dest={{ item.dest }} mode={{ item.mode }} with_items: - { src: 'rsyncd.conf.j2', dest: '/tmp/rsync.conf', mode: '0644' } - { src: 'rsyncd.password.j2', dest: '/tmp/rsync.conf', mode: '777' } notify: restart rsync server - name: systemd rsync server systemd: name=rsyncd state=started enabled=yes [root@gukai roles]# cat rsync/templates/rsyncd.conf.j2 uid=www gid=www port=873 [root@gukai roles]# cat site.yml #- hosts: webservers # roles: # - nfs #- hosts: webservers # roles: # - memcached - hosts: webservers roles: - rsync