ansible playbook基础

1.创建用户和组

[root@ansible ansible]# vim create_user.yaml 
---
#create user and group

- hosts: host1
  remote_user: root
  gather_facts: no

  tasks:
    - name: create group
      group: name=wxit system=yes gid=1000
    - name: create user
      user: name=gqq system=yes group=wxit shell=/sbin/nologin

[root@ansible ansible]# ansible-playbook create_user.yaml 

PLAY RECAP **********************************************************************************************************************************
192.168.100.111            : ok=3    changed=2    unreachable=0    failed=0   

[root@ansible ansible]# ansible host1 -a 'getent passwd gqq'
[root@ansible ansible]# ansible host1 -a 'id gqq'

2.安装nginx

---
# install_nginx.yaml
# 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 : start Nginx
    service: name=nginx state=started enabled=yes


ansible host1 -a 'ss -ntl'
ansible host1 -m yum -a 'name=httpd state=absent' #卸载
ansible host1 -a 'ls -l /etc/passwd'

#浏览器访问验证

rpm -ql nginx
ll /usr/share/nginx/html/

mkdir files
vi index.html
"gqq'

  - name : html page
    copy: src=files/index.html dest=/usr/share/nginx/html/index.html

#再跑,显示绿的,刷新页面,看到新页面


ansible gqqhosts --list

3.安装httpd

dnf install httpd
cp /etc/httpd/conf/httpd.conf files/
vim   /etc/httpd/conf/httpd.conf
Listen 8080

范例: install_httpd.yml
#instal1 httpd- hosts : websrvs
remote_user: 
rootgather_facts: no
tasks:
-name : Instal7 httpd
yum: name=httpd state=present
- name: Instal7 configure file
copy : src=files/httpd.conf dest=/etc/httpd/conf/
- name: Instal7 configure file
copy : src=files/index.html dest=/var/www/html/
- name : start service
service: name=httpd state=started enabled=yes


ansible-playbook --list-hosts install_httpd.yaml
ansible-playbook --list-hosts install_httpd.yaml --limit 10.0.0.8





范例: remove_httpd.yml
#remove_httpd.ym1
- 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



4.安装mysql

范例:安装mysql-5.6.46-linux-glibc2.12
I [root@ansible ~]#1s -1 /data/ansible/files/mysq1-5.6.46-7inux-g7ibc2.12-x86_64.tar. gz-rW-r--r-- 1 root root 403177622 Dec 4 13:05/data/ansible/files/mysq7-5.6.46-7inux-glibc2.12-x86_64.tar. gz

[root@ansib1e ~]#cat /data/ansible/files/my.cnf
[mysq7d]
socket=/tmp/mysq7.sock
user=mysq1
symbolic-7inks=O
datadir=/data/mysq1
innodb_file_per_table=1log-bin
pid-file=/data/mysq1/mysqld.pid

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

[mysq1d_safe]
log-error=/var/1og/mysq1d.1og


[root@ansible ~]#cat /data/ansible/fi1es/secure_mysq1.sh
#!/bin/bash
/usr/loca1/mysq1/bin/mysql_secure_insta71ation <<EOF
y
magedu
mageduy
y
y
y
EOF


hosts: websrvs
remote_user: root
tasks:
- name: insta11 packages
yum: name=1ibaio, per1-Data-Dumper , per1-Getopt-Long
- name : create mysq1 group
group: name=mysql gid=306
- name: create mysq1 user
user: name=mysq1 uid=306 group=mysq1 she17=/sbin/nologin system=yes create_home=nohome=/data/mysql
- name : copy tar to remote host and file mode
unarchive: src=/data/ansible/files/mysq1-5.6.46-7inux-glibc2.12-x86_64.tar. gzdest=/usr/local / owner=root group=root
- name : mkdir /usr/loca1/mysq7
file: src=/usr/loca1/mysq1-5.6.46-7inux-g7ibc2.12-x86_64 dest=/usr/loca1/mysq1state=7ink
name : data dir
she11: chdir=/usr/loca1/mysq1/ ./scripts/mysq1_insta1l_db --datadir=/data/mysq1 --user=mysq1
tags: data
- name: config my.cnf
copy: src=/data/ansible/files/my.cnfdest=/etc/my.cnf
- name: service script
she11:/bin/cp /usr/loca1/mysq1/support-files/mysq1.server /etc/init.d/mysq1d
- name: enable service


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值