首页 > 其他分享 >playbook(9)

playbook(9)

时间:2022-11-22 14:33:38浏览次数:32  
标签:httpd name nginx state user mysql playbook

基本格式

ansible-playbook <filename.yml> ... [options]

常见选项

 -C --check          #只检测可能会发生的改变,但不真正执行操作
 --list-hosts        #列出运行任务的主机 
--list-tags         #列出tag 
--list-tasks        #列出task
 --limit 主机列表      #只针对主机列表中的主机执行
 -v -vv  -vvv        #显示过程

# Playbook 初步
## 利用 playbook 创建 mysql 用户
范例:mysql_user.yml
```yaml
---
- hosts: dbsrvs
  remote_user: root

  tasks:
    - {name: create group, group: name=mysql system=yes gid=306}
    - name: create user
      user: name=mysql shell=/sbin/nologin system=yes group=mysql uid=306 home=/data/mysql create_home=no      

利用 playbook 安装 nginx

范例:install_nginx.yml

---
# install nginx 
- hosts: websrvs
  remote_user: root  
  tasks:
    - name: add group nginx
      user: name=nginx state=present
    - name: add user nginx
      user: name=nginx state=present group=nginx
    - name: Install Nginx
      yum: name=nginx state=present
    -name: html page
      copy: src=files/index.html dest=/usr/share/nginx/html/index.html
    - name: Start Nginx
      service: name=nginx state=started enabled=yes

这里的copy语句需要自己在主机上建立文件夹并编辑一个文件,也可以省略copy语句
[root@ansible data]# mkdir files
[root@ansible data]# vim /files/index.html

利用 playbook 安装和卸载 httpd

范例:install_httpd.yml

---
#install httpd 
- hosts: websrvs
  remote_user: root
  gather_facts: no

  tasks:
    - name: Install httpd
      yum: name=httpd state=present
    - name: Install configure file
      copy: src=files/httpd.conf dest=/etc/httpd/conf/
    - name: web html
      copy: src=files/index.html  dest=/var/www/html/
    - name: start service
      service: name=httpd state=started enabled=yes

ansible-playbook   install_httpd.yml --limit 10.0.0.8

范例:remove_httpd.yml

#remove_httpd.yml
---
- hosts: websrvs
  remote_user: root

  tasks:
    - name: remove httpd package
      yum: name=httpd state=absent
    - name: remove apache user 
      user: name=apache state=absent
    - name: remove config file
      file: name=/etc/httpd  state=absent
    - name: remove web html
      file: name=/var/www/html/index.html state=absent

利用 playbook 安装mysql

范例:安装mysql-5.6.46-linux-glibc2.12

[root@ansible ~]#ls -l /data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz 
-rw-r--r-- 1 root root 403177622 Dec  4 13:05 /data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz

[root@ansible ~]#cat /data/ansible/files/my.cnf 
[mysqld]
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
log-bin
pid-file=/data/mysql/mysqld.pid

[client]
port=3306
socket=/tmp/mysql.sock

[mysqld_safe]
log-error=/var/log/mysqld.log

[root@ansible ~]#cat /data/ansible/files/secure_mysql.sh 
#!/bin/bash
/usr/local/mysql/bin/mysql_secure_installation <<EOF

y
magedu
magedu
y
y
y
y
EOF

[root@ansible ~]#tree /data/ansible/files/
/data/ansible/files/
├── my.cnf
├── mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
└── secure_mysql.sh

0 directories, 3 files

[root@ansible ~]#cat /data/ansible/install_mysql.yml
---
# install mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
- hosts: dbsrvs
  remote_user: root
  gather_facts: no

  tasks:
    - name: install packages
      yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
    - name: create mysql group
      group: name=mysql gid=306 
    - name: create mysql user
      user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
    - name: copy tar to remote host and file mode 
      unarchive: src=/data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=root group=root 
    - name: create linkfile  /usr/local/mysql 
      file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
    - name: data dir
      shell: chdir=/usr/local/mysql/  ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
      tags: data
    - name: config my.cnf
      copy: src=/data/ansible/files/my.cnf  dest=/etc/my.cnf 
    - name: service script
      shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    - name: enable service
      shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on  
      tags: service
    - name: PATH variable
      copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
    - name: secure script
      script: /data/ansible/files/secure_mysql.sh
      tags: script

范例:install_mariadb.yml

---
#Installing MariaDB Binary Tarballs
- hosts: dbsrvs
  remote_user: root
  gather_facts: no

  tasks:
    - name: create group
      group: name=mysql gid=27 system=yes
    - name: create user
      user: name=mysql uid=27 system=yes group=mysql shell=/sbin/nologin home=/data/mysql create_home=no
    - name: mkdir datadir
      file: path=/data/mysql owner=mysql group=mysql state=directory
    - name: unarchive package
      unarchive: src=/data/ansible/files/mariadb-10.2.27-linux-x86_64.tar.gz dest=/usr/local/ owner=root group=root
    - name: link
      file: src=/usr/local/mariadb-10.2.27-linux-x86_64 path=/usr/local/mysql state=link 
    - name: install database
      shell: chdir=/usr/local/mysql   ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
    - name: config file
      copy: src=/data/ansible/files/my.cnf  dest=/etc/ backup=yes
    - name: service script
      shell: /bin/cp  /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
    - name: start service
      service: name=mysqld state=started enabled=yes
    - name: PATH variable
      copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh

Playbook中使用handlers和notify

Handlers本质是task list,类似于MySQL中的触发器触发的行为,其中的task与前述的task并没有本质上的不同,主要用于当关注的资源发生变化时,才会采取一定的操作。而Notify对应的action可用于在每个play的最后被触发,这样可避免多次有改变发生时每次都执行指定的操作,仅在所有的变化发生完成后一次性地执行指定操作.在notify中列出的操作称为handler,也即notify中调用handler中定义的操作
示范

- hosts: webserver
  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/
      notify: restart httpd
    - name: ensure apache is running
      service: name=httpd state=started enabled=yes
  
  handlers:
    - name: restart httpd
      service: name=httpd state=restarted

这里需要自己在主机上安装一个htppd,使用dnf install httped -y
然后把httpd.conf文件复制到对应的文件夹
cp /etc/httpd/conf/httpd.conf files/

示范

- hosts: webserver
  vars:
    http_port: 80
    max_clients: 256
  remote_user: root
  
  tasks:
    - name: ensure apache is at the latest version
      yum: name=httpd state=latest
    - name: ensure apache is running
      service: name=httpd state=started
    - name: Install configure file
      copy: src=files/httpd.conf dest=/etc/httpd/conf/
      notify: restart httpd
  
  handlers:
      - name: restart httpd 
        service: name=httpd state=restarted

示范

- hosts: webserver
  remote_user: root
  
  tasks:
    - name: add group nginx
      tags: user
      user: name=nginx state=present
    - name: add user nginx
      user: name=nginx state=present group=nginx
    - name: Install Nginx
      yum: name=nginx state=present
    - name: config
      copy: src=/root/config.txt dest=/etc/nginx/nginx.conf
      notify:
        - Restart Nginx
        - Check Nginx Process
  
  handlers:
    - name: Restart Nginx
      service: name=nginx state=restarted enabled=yes
    - name: Check Nginx process
      shell: killall -0 nginx > /tmp/nginx.log

Playbook中tags使用

在playbook文件中,可以利用tags组件,为特定task 指定标签,当在执行playbook时,可以只执行特定tags的task,而非整个playbook文件

- hosts: testsrv
  remote_user: root
  tags: inshttpd   针对整个playbook添加tage
  tasks:
    - name: Install httpd
      yum: name=httpd state=present
    - name: Install configure file
      copy: src=files/httpd.conf dest=/etc/httpd/conf/
      tags: rshttpd
      notify: restart httpd
  handlers:
    - name: restart httpd
      service: name=httpd status=restarted
     
ansible-playbook –t rshttpd httpd2.yml

标签:httpd,name,nginx,state,user,mysql,playbook
From: https://www.cnblogs.com/yutoujun/p/16915023.html

相关文章

  • AnsiblePlaybook变量默认值设置
    ----hosts:localremote_user:roottasks:-name:debugdebug:msg:"vis{{item}}"with_items:-"{{v|default(10)}}......
  • Ansible-Playbook
    playbooks是一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一......
  • 【干货】2021新消费品牌STEP增长方法论:品牌营销与生意增长Playbook.pdf(附下载链接)...
    大家好,我是文文,今天给大家分享巨量引擎和凯度中国联合发布的《2021新消费品牌STEP增长方法论.pdf》,本报告将带你一起解码新品牌各个阶段的核心经营逻辑,帮助新消费品牌成功穿......
  • 来至舒大佬的一键部署redis集群playbook
    ----name:installredishosts:allserial:2vars:-version:"redis-7.0.5"-user:"redis"-id:"88"-INSTALL_DIR:"/apps/redis"-CPUS:"......
  • ansible-playbook 用法
     catinstall_zabbix_3.yaml----name:#名称hosts:new#hosts为文件名,new为hosts文件里得[new]tasks:#任务-name:shell:|rpm-ivhhtt......
  • ansible使用playbook部署LNMP
    ansible使用playbook部署LNMP目录ansible使用playbook部署LNMP安装ansible基于ansible进行基础准备使用playbook进行编写环境介绍:系统ip主机名服务centos8......
  • ansible--playbook剧本
    一、初步说明以一个简单的playbook为例,说明yaml的基本语法yaml⽂件以---开头,以表明这是⼀个yaml⽂件,就像xml⽂件在开头使⽤宣称它是xml⽂件⼀样。但即使没有使⽤--......
  • playbook
    playbook目录1.实施playbook1.1Ansibleplaybook与临时命令1.2格式化Ansibleplaybook1.3运行playbook1.4提高输出的详细程度1.5语法验证1.6执行空运行2.实施多个......
  • 关于ansible-通过Ad-hoc和playbook-对linux主机的连接性测试
    环境:被控主机都是linux主机,不过好几种发行版本,但是笔者都还是设计了统一的标准1、主控和被控端都是Linux操作系统,都是有ansible用户的2、主控通过ssh-key的私钥登录到被......
  • 关于ansible-通过playbook-对aws上windows-server的连接性测试
    因笔者这ansible对windows的管理没有使用常规的连接方式而是采用的ansible主控端,通过发送ssm命令管理远端的aws上的windows主机因此一台新的机器接入进来后,及各个相关的......