任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行
变量
模板
处理器,当changed状态条件满足时,(notify)触发执行的操作
角色
vim test1.yaml --- #yaml文件以---开头,以表明这是一个yaml文件,可省略 - name: first play #定义一个play的名称,可省略 gather_facts: false #设置不进行facts信息收集,这可以加快执行速度,可省略 hosts: webservers #指定要执行任务的被管理主机组,如多个主机组用冒号分隔 remote_user: root #指定被管理主机上执行任务的用户 tasks: #定义任务列表,任务列表中的各任务按次序逐个在hosts中指定的主机上执行 - name: test connection #自定义任务名称 ping: #使用 module: [options] 格式来定义一个任务 - name: disable selinux command: '/sbin/setenforce 0' #command模块和shell模块无需使用key=value格式 ignore_errors: True #如执行命令的返回值不为0,就会报错,tasks停止,可使用ignore_errors忽略失败的任务 - name: disable firewalld service: name=firewalld state=stopped #使用 module: options 格式来定义任务,option使用key=value格式 - name: install httpd yum: name=httpd state=latest - name: install configuration file for httpd copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf #这里需要一个事先准备好的/opt/httpd.conf文件 notify: "restart httpd" #如以上操作后为changed的状态时,会通过notify指定的名称触发对应名称的handlers操作 - name: start httpd service service: enabled=true name=httpd state=started handlers: #handlers中定义的就是任务,此处handlers中的任务使用的是service模块 - name: restart httpd #notify和handlers中任务的名称必须一致 service: name=httpd state=restarted
参数 | 说明 |
---|---|
-k(–ask-pass) | 用来交互输入ssh密码 |
-K(-ask-become-pass) | 用来交互输入sudo密码 |
-u | 指定用户 |
--syntax-check | 检查yaml文件的语法是否正确 |
--list-task | 检查tasks任务 |
--list-hosts | 检查生效的主机 |
--start-at-task='install httpd' | 指定从某个task开始运行 |
vim XXX.yaml XXX.yml ---- - name: 设置play的名称 hosts: 指定执行此play的远程主机组 remote_user: 指定执行此play的用户 gather_facts: 设置是否收集facts信息 yes|no|true|flase 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:任务名 模块名:模块参数
横向格式: 模块名:参数1=值 参数2={{变量名}} .... 纵向格式: 模块名: 参数1:值 参数2:"{{变量名}}" ....
横向格式: 值为纯量类型时: with_items:[值1,值2,....] vars: 变量名:[值1,值2,....] 值为对象类型时(键值对字段)时: with_items: - {key1: 值1,key2: 值2} - {key1: 值3,key2: 值4} .... 纵向格式 -name: with_items: - 值1 - 值2 .... 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 ....
1)先准备一个 XXX.j2 配置模版文件,在模版文件中使用{{变量名}} 格式引用主机变量、组变量、facts信息字段变量或vars字段自定义的变量的值。 2)编辑 playbook 文件,在文件中的 tasks 任务中定义 template 模块配置(template: src=XXX.j2文件路径 dest=远程主机文件路径)
- name: second play hosts: dbservers remote_user: root vars: #定义变量 - groupname: mysql #格式为 key: value - username: nginx tasks: - name: create group group: name={{groupname}} system=yes gid=306 #使用 {{key}} 引用变量的值 - name: create user user: name={{username}} uid=306 group={{groupname}} - name: copy file copy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt #在setup模块中可以获取facts变量信息
必须是被控主机已存在的用户,且ssh密码要与用户密码一致 - hosts: dbservers remote_user: zhangsan become: yes #2.6版本以后的参数,之前是sudo,意思为切换用户运行 become_user: root #指定sudo用户为root 执行playbook时:ansible-playbook test1.yml -k -K 、 需要在客户端添加权限/etc/sudoers
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
vim test3.yaml --- - name: play1 hosts: dbservers gather_facts: false tasks: - name: create file file: path: "{{item}}" state: touch with_items: [ /opt/a, /opt/b, /opt/c, /opt/d ] - name: play2 hosts: dbservers gather_facts: false vars: test: - /tmp/test1 - /tmp/test2 - /tmp/test3 - /tmp/test4 tasks: - name: create directories file: path: "{{item}}" state: directory with_items: "{{test}}" - name: play3 hosts: dbservers gather_facts: false tasks: - name: add users user: name={{item.name}} state=present groups={{item.groups}} with_items: - name: test1 groups: wheel - name: test2 groups: root 或 with_items: - {name: 'test1', groups: 'wheel'} - {name: 'test2', groups: 'root'}
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 #表示始终要运行的代码
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"
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行,修改 2.修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量 vim /etc/ansible/hosts [webservers] 192.168.80.11 http_port=192.168.80.11:80 server_name=www.accp.com:80 root_dir=/etc/httpd/htdocs [dbservers] 192.168.80.12 http_port=192.168.80.12:80 server_name=www.benet.com:80 root_dir=/etc/httpd/htdocs 3.编写 playbook vim apache.yaml --- - hosts: all remote_user: root vars: - package: httpd - service: httpd tasks: - name: install httpd package yum: name={{package}} state=latest - name: install configure file template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #使用template模板 notify: - restart httpd - name: create root dir file: path=/etc/httpd/htdocs state=directory - name: start httpd server service: name={{service}} enabled=true state=started handlers: - name: restart httpd service: name={{service}} state=restarted ansible-playbook apache.yaml
在本地配置nginx.service vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target 配置playbook文件 vim nginx.yaml - name: third play for install nginx with source gather_facts: false hosts: webservers remote_user: root 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 yum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++,make 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/sbin/nginx - name: create nginx service file copy: src=/lib/systemd/system/nginx.service dest=/lib/systemd/system/nginx.service - name: start nginx service: name=nginx state=started enabled=yes