目录
实操3:通过playbook完成普通账户权限提权为root用户
实操5:通过with_items循环实现在远程主机上关闭服务、创建文件和目录
实操6:基于templates模块,完成nginx服务的配置模板
(1)Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行
(2)Variables:变量
(3)Templates:模板
(4)Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
(5)Roles:角色
完成playbook剧本的编写,并完成语法检查
- #定义剧本名称为nginx安装
- - name: install nginx
- #表示捕收剂facts信息
- gather_facts: false
- #指定指定此play的远程主机组
- hosts: webservers
- #指定运行此play的用户
- remote_user: root
- #指定当前play的任务列表
- tasks:
- #关闭防火墙和selinux
- - name: disabled firewalld
- service: name=firewalld state=stopped enabled=no
- - name: disable selinux
- command: '/sbin/setenforce 0'
- ignore_errors: yes
- - name: disabled selinux forever
- replace: path=/etc/selinux/config regexp=enforcing replace=disabled after=loaded
- #将nginx的repo文件传输给远程主机
- - name: copy nginx repo
- copy: src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo
- #通过yum安装nginx
- - name: install nginx by yum
- yum: name=nginx state=latest
- #安装nginx
- - name: start nginx service
- systemd: name=nginx state=started enabled=yes
验证
- ---
- - name: second play for install httpd
- gather_facts: false
- hosts: webservers:dbservers
- remote_user: root
- tasks:
- #关闭防火墙
- - name: disabled firewalld
- service: name=firewalld state=stopped enabled=no
-
- #关闭 selinux
- - name: disable selinux
- command: '/sbin/setenforce 0'
- ignore_errors: yes
- - name: disabled selinux forever
- replace: path=/etc/selinux/config regexp=enforcing replace=disabled after=loaded
-
- #关闭和删除yum安装的nginx
- - name: disabled nginx
- service: name=nginx state=stopped enabled=no
- ignore_errors: yes
- - name: remove nginx
- yum: name=nginx state=absent
-
- #准备本地yum仓库
- yum: name=nginx state=absent
-
- #准备本地yum仓库
- - name: archive yum online repo
- archive: path=/etc/yum.repos.d/*.repo dest=/etc/yum.repos.d/yum_bak.repo.tar.gz format=gz remove=yes
- - name: copy local yum repo
- copy: src=nginx/local.repo dest=/etc/yum.repos.d/local.repo
- - name: mount cdrom
- mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
-
- #安装nginx的依赖环境包
- - name: install pkgs
- yum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++,make state=latest
-
- #创建nginx用户
- - name: create user nginx
- user: name=nginx create_home=no shell=/sbin/nologin
-
- #解压软件包并安装
- - name: unarchive nginx package
- unarchive: copy=yes src=nginx/nginx-1.24.0.tar.gz dest=/opt/
- - name: install nginx with source
- shell: chdir=/opt/nginx-1.24.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
-
- #创建软连接
- - name: create nginx link
- file: src=/usr/local/nginx/sbin/nginx path=/usr/sbin/nginx state=link
-
- #创建nginx的service文件
- - name: create nginx service file
- copy: src=nginx/nginx.service dest=/lib/systemd/system/nginx.service
- - name: start nginx
- service: name=nginx state=started enabled=yes
- ---
- - name: stop firewalld nfs crond
- gather_facts: true
- hosts: webservers:dbservers
- remote_user: root
- #定义变量名为servername值为firewalld
- vars:
- - servername: firewalld
- tasks:
- - name: stop firewalld
- #调用vars定义的变量
- systemd: name={{servername}} state=stopped enabled=no
- - name: copy ipv4 info
- #通过调用收集的facts信息做变量值
- copy: content={{ansible_default_ipv4.address}} dest=/opt/ip.txt
纵向写法
[root@localhost playbook]#ansible-playbook demo3.yaml -e "servername=crond"
- ---
- - name: stop firewalld nfs crond
- gather_facts: true
- hosts: dbservers
- remote_user: xueyin
- become: yes
- become_user: root
- #定义变量名为servername值为firewalld
- vars:
- - servername: firewalld
- tasks:
- - name: stop firewalld
- #调用vars定义的变量
- systemd:
- name: "{{servername}}"
- state: stopped
- enabled: no
- - name: copy ipv4 info
- #通过调用收集的facts信息做变量值
- copy:
- content: "{{ansible_default_ipv4.address}}"
- dest: /opt/ip.txt
- [root@localhost ~]#vim /etc/sudoers
- 不仅在playbook文件中设置sudo提权 还需要在对应的主机上实现sudo提权
- [root@localhost playbook]#ansible-playbook demo3.yaml -k -K
- -k 指定ssh的登录密码
- -K 大写需要输入用户的登录密码
when一个比较常见的应用场景是实现跳过某个主机不执行任务或者只有满足条件的主机执行任务
- vim test2.yaml
- ---
- - hosts: all
- remote_user: root
- tasks:
- - name: shutdown host
- command: /sbin/shutdown -r now
- when: ansible_default_ipv4.address == "192.168.80.12" #when指令中的变量名不需要手动加上 {{}}
- 或
- when: inventory_hostname == "<主机名>"
-
- ansible-playbook test2.yaml
no1:关闭firewalld、nfs、crond
- ---
- - name: stop firewalld nfs crond
- gather_facts: true
- hosts: webservers:dbservers
- remote_user: root
- #定义变量名为servername,值分别为firewalld,nfs,crond
- vars:
- - servername: [firewalld, nfs, crond]
- tasks:
- - name: stop firewalld nfs crond
- #通过with_items实现循环关闭服务
- with_items: "{{servername}}"
- systemd: name={{item}} state=stopped enabled=no
no2:批量创建多个用户,并加入指定的组
- ---
- - name: create user
- gather_facts: false
- hosts: dbservers
- remote_user: root
- vars:
- myusers:
- - {myname: xyc, mygroup: ky33}
- - {myname: wwy, mygroup: ky34}
- - {myname: wx, mygroup: ky35}
- tasks:
- - name: create groups
- with_items: [ky33, ky34, ky35]
- group: name={{item}}
-
- - name: create users and join into group
- with_items: "{{myusers}}"
- user: name={{item.myname}} groups={{item.mygroup}} state=present
- Templates 模块
- Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。
-
- 1.先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量
- cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2
-
- vim /opt/httpd.conf.j2
- Listen {{http_port}} #42行,修改
- ServerName {{server_name}} #95行,修改
- DocumentRoot "{{root_dir}}" #119行,修改
可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
- tags 模块
- 可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
- playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
-
- vim webhosts.yaml
- ---
- - hosts: webservers
- remote_user: root
- tasks:
- - name: Copy hosts file
- copy: src=/etc/hosts dest=/opt/hosts
- tags:
- - only #可自定义
- - name: touch file
- file: path=/opt/testhost state=touch
- tags:
- - always #表示始终要运行的代码
-
- ansible-playbook webhosts.yaml --tags="only"
-
- vim dbhosts.yaml
- ---
- - hosts: dbservers
- remote_user: root
- tasks:
- - name: Copy hosts file
- copy: src=/etc/hosts dest=/opt/hosts
- tags:
- - only
- - name: touch file
- file: path=/opt/testhost state=touch
-
-
- ansible-playbook dbhosts.yaml --tags="only"
- //分别去两台被管理主机上去查看文件创建情况
- ---
- - name: serventh play for install nginx with source
- gather_facts: yes
- hosts: webservers
- remote_user: root
- vars:
- - root_dir: /var/www/html
- tasks:
- #关闭防火墙和selinux
- - name: disabled firewalld
- service: name=firewalld state=stopped enabled=no
- - name: disable selinux
- command: '/sbin/setenforce 0'
- ignore_errors: yes
- - name: disabled selinux forever
- replace: path=/etc/selinux/config regexp=enforcing replace=disabled after=loaded
-
- #安装依赖包
- - name: mount cdrom
- mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
- - name: install pkgs
- with_items: [pcre-devel, zlib-devel, openssl-devel, gcc, gcc-c++, make]
- yum: name={{item}} state=latest
-
- #创建运行用户
- - name: create nginx user
- user: name=nginx create_home=no shell=/sbin/nologin
-
- #解压软件包并安装
- - name: unarchive nginx package
- unarchive: copy=yes src=/etc/ansible/playbook/nginx-1.24.0.tar.gz dest=/opt/
- - name: install nginx with source
- shell: chdir=/opt/nginx-1.24.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
-
- #创建软链接并启动服务
- - name: create link file for nginx
- file: state=link src=/usr/local/nginx/sbin/nginx path=/usr/local/sbin/nginx
- - name: create nginx service file
- copy: src=nginx.service dest=/lib/systemd/system/nginx.service
- - name: start nginx
- service: name=nginx state=started enabled=yes
-
- #创建网页根目录
- - name: create root dir
- file: path={{root_dir}} state=directory
- #创建网页文件
- - name: create index.html for accp.com
- copy: content="
this is accp test web page
" dest={{root_dir}}/index.html - when: ansible_default_ipv4.address == "192.168.20.17"
- - name: create index.html for benet.com
- copy: content="
this is benet test web page
" dest={{root_dir}}/index.html - when: ansible_default_ipv4.address == "192.168.20.12"
-
- #生成配置文件
- - name: copy nginx config file with template
- template: src=/etc/ansible/playbook/nginx.conf.j2 dest=/usr/local/nginx/conf/nginx.conf
- notify: "reload nginx"
-
- handlers:
- - name: reload nginx
- service: name=nginx state=reloaded
- inventory主机清单管理
- 主机清单配置文件 /etc/ansible/hosts
-
- 格式:
- [组名]
- 主机IP[:ssh端口] #默认是 22 端口
- 主机名[:ssh端口]
- 主机IP[0:255] #设置连续的主机列表范围
- 主机名[a:z] ansible_port= ansible_user= ansible_password= #设置主机变量
-
- [组名:vars] #设置组变量
- ansible_port=
- ansible_user=
- ansible_password=
-
- [大组名:children] #设置嵌套组
- 组名1
- 组名2
- 组名3
-
-
- playbook剧本
- vim XXX.yaml XXX.yml
- ---
- - name: #设置play的名称
- hosts: #指定执行此play的远程主机组
- remote_user: #指定执行此play的用户
- #become: #设置是否sudo切换
- #become_user: #指定sudo切换的用户
- gather_facts: #设置是否收集facts信息 yes|no|true|false
- vars: #设置自定义变量
- tasks: #指定此play的任务列表
- - name: #定义任务的名称
- 模块名: 模块参数 #定义任务要使用的模块和参数(键值对格式)
- - name:
- 模块名: 模块参数
- ignore_errors: true #忽略此任务的失败
- - name:
- 模块名: 模块参数
- notify: '任务名' #定义此任务执行结果为changed状态时要触发的handlers任务
- - name:
- 模块名: 模块参数
- when: #定义条件表达式(== != > >= < <=),条件成立时执行此task任务,否则不执行此任务
- - name:
- 模块名: 模块参数={{item}}
- with_items: #定义循环列表
- - name:
- 模块名: 模块参数
- tags: #定义任务的标签,ansible-playbook --tags="标签" 可实现仅执行拥有指定标签的任务(拥有always标签的任务总是执行)
- - 标签1
- - 标签2
- ....
-
- handlers:
- - name: 任务名
- 模块名: 模块参数
-
-
- ansible-playbook XXX.yaml -u -k -K
- --syntax-check
- --list-task
- --list-hosts
- --start-at-task="任务名称"
- --tags="标签"
-
-
- tasks任务的模块语法格式:
- 纵向格式:
- 模块名:
- 参数1: 值
- 参数2: "{{变量名}}"
- ....
- 横向格式:
- 模块名: 参数1=值 参数2={{变量名}} ....
-
-
- with_items 和 vars 的语法格式
- 纵向格式:
- - name:
- with_items:
- - 值1
- - 值2
- ....
- 模块名: 参数={{item}}
-
- vars:
- 变量名:
- - 值1
- - 值2
- ....
- tasks:
- - name:
- 模块名: 参数={{item}}
- with_items: "{{变量名}}"
-
- #值为对象类型(键值对字段)时:
- with_items:
- - key1: 值1
- key2: 值2
- - key1: 值3
- key2: 值4
- ....
-
- vars:
- 变量名:
- - key1: 值1
- key2: 值2
- - key1: 值3
- key2: 值4
- ....
-
- 横向格式:
- #值为纯量类型时:
- with_items: [值1, 值2, ....]
-
- vars:
- 变量名: [值1, 值2, ....]
-
- #值为对象类型(键值对字段)时:
- with_items:
- - {key1: 值1, key2: 值2}
- - {key1: 值3, key2: 值4}
- ....
-
-
- template配置模板模块
- 1)先准备一个 XXX.j2 配置模板文件,在模板文件中使用 {{变量名}} 格式引用主机变量、组变量、facts信息字段变量或vars字段自定义的变量的值
- 2)编辑 playbook 文件,在文件中的 tasks 任务中定义 template 模块配置(template: src=XXX.j2文件路径 dest=远程主机文件路径)
-
-
- roles角色的作用?
- 可以把playbook剧本里的各个play看作为一个角色,将各个角色的tasks任务、vars变量、template模板和copy、script模块使用的相关文件等内容放置在指定角色的目录里统一管理,在需要的时候可在playbook中使用roles角色直接调用即可。也就是说roles角色可以在playbook中实现代码的复用。
-
- roles/ #角色总目录,其每个子目录就是一个角色目录
- nginx/ #相当于playbook中的每一个play主题,目录名就是角色名
- files/ #存放copy、script模块调用的文件
- templates/ #存放template模块调用的 XXX.j2 模板文件
- tasks/main.yml #定义此角色的tasks普通任务列表
- handlers/main.yml #定义此角色通过notify触发时执行的handlers处理器任务列表
- vars/main.yml #定义此角色用的自定义变量
- defaults/main.yml #定义此角色用的默认变量(一般不用)
- meta/main.yml #定义此角色的元数据信息和依赖关系
- mysql/
- ....
- php/
- ....
-
-
- vim XXX.yaml
- - name:
- hosts:
- remote_user:
- roles:
- - nginx
- - mysql
- - php
-
- ansible-playbook XXX.yaml