标签:class mysql playbook start 缩进 install and state ice
变量类别
获取内置变量
ansible all -m setup
执行a.yaml,同时传递变量name=tom,age=20
# ansible-playbook a.yaml --extra-vars "name=tom, age=20"
案例:安装httpd,要求设置apache的端口为808,网站跟目录/myweb
- host: all # 指定要操作的主机组 remoute_user: root # 指定在远程主机上以哪个用户身份执行tasks vars: # 定义变量 - port: 808 # 变量名、变量值 - root: /myweb # 变量名、变量值 tasks: # 指定要执行的操作列表 - name: install httpd # 设置一个名称,用于给用户提示正在执行操作 yum: name=httpd state=latest # yum是模块名,后面是参数 - name: start httpd service: name=httpd state=started # service是模块名,后面的参数
注意:playbook对格式缩进要求十分严格。
yaml 文件中的主要构成
案例:在所有被管理节点安装httpd,然后启动httpd,要求httpd启动端口是8080
- hosts: all remote_user: root tasks: - name: install httpd yum: name=httpd state=present - name: start httpd service: name=httpd state=started enabled=true - name: send httpd.conf copy: src=/root/httpd.conf.template dest=/etc/httpd/conf/httpd.conf notify: - restart httpd handlers: - name: restart httpd service: name=httpd state=restarted
(1)自定义变量
- hosts: all remote_user: root vars: - package_name: mysql-server # mariadb-server - service_name: mysqld # mariadb tasks: - name: install mysql server yum: name={{ package_name }} state=latest - name: start mysql server service: name={{ service_name }} state=started
(2)使用ansible的内置变
在每个被管理主机上创建一个用户,用户名和主机名相同
查询内置变量 [root@centos6-1 ~]# ansible all -m setup | grep fqdn "ansible_fqdn": "centos6-2", [root@centos6-1 ~]# cat c.yaml - hosts: all tasks: - name: create user user: name={{ ansible_fqdn }} state=present - name: create file file: name=/tmp/{{ ansible_fqdn }} state=touch
(3)主机清单变量
# 定义主机变量 [webservers] 192.168.31.64 userid=1050 192.168.31.66 userid=1060 # 定义主机组变量 [webservers:vars] username=jerry
- hosts: all tasks: - name: create user user: name={{ username }} uid={{ userid }} state=present
template称为模板
模板的用处是用来动态生成服务的配置文件
- hosts: all tasks: - name: delete old epel repo file shell: rm -rf /etc/yum.repos.d/epel* - name: create new epel repo file shell: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo - name: install nginx yum: name=nginx state=latest - name: send nginx.conf template: src=/root/template.j2 dest=/etc/nginx/nginx.conf notify: - restart nginx - name: start nginx service: name=nginx state=started enabled=true handlers: - name: restart nginx service: name=nginx state=restarted
案例:用playbook实现如下功能
1:在所有被管理主机上安装zsh
2:在主机名为centos7-2的主机上创建用户tom3,其他主机不创建
- hosts: all tasks: - name: install zsh yum: name=zsh state=present - name: create user tom3 user: name=tom3 state=present when: ansible_fqdn == "centos7-5"
- hosts: all tasks: - name: create user user: name={{ item }} state=present with_items: - u1 - u2 - u3 - u4 - u5
作用:给某个task设置一个表情,用于仅仅执行某个task
- hosts: all tasks: - name: isntall shell: yum install httpd -y - name: send file copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: - restart httpd tags: - senfile - name: start httpd shell: systemctl start httpd handlers: - name: restart httpd shell: systemctl restart httpd
[root@centos7-1 ~]# ansible-playbook 4.yaml --tags="senfile"
标签:class mysql playbook start 缩进 install and state ice
原文地址:https://www.cnblogs.com/wendyluo/p/13178890.html