Skip to main content

ansible脚本案例

1.建名为mysql的系统用户

create_user.yaml

---
- hosts: web2
remote_user: root
gather_facts: no
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

2.安装nginx

install_nginx.yaml

---
- hosts: web2
remote_user: root
gather_facts: no
tasks:
- name: add group nginx
group:
name: nginx
state: present
- name: add user nginx
user:
name: nginx
state: present
group: nginx
- name: install nginx
yum:
name: nginx
state: present
- name: web page
copy:
src: ./files/index.html
dest: /usr/share/nginx/html/index.html
- name: start nginx
service:
name: nginx
state: started
enabled: yes

3.安装和删除httpd

install_httpd.yaml

---                                                                                                         
- hosts: web2
remote_user: root
gather_facts: no

tasks:
- name: Install httpd
yum: name=httpd
- name: Modify config list port
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen 8080' #改成监听8080
- name: Modify config data1
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: 'DocumentRoot "/var/www/html"'
line: 'DocumentRoot "/data/html"' #html目录改位置
- name: Modify config permition
lineinfile:
path: /etc/httpd/conf/httpd.conf
insertafter: 'DocumentRoot "/data/html"' #给新的html目录添加权限
line: |
<Directory "/data/html">
AllowOverride None
Require all granted
</Directory>
- name: mkdir website dir
file:
path: /data/html
state: directory
group: apache
owner: apache
recurse: true
- name: copy web html
copy:
src: files/index.html
dest: /data/html
- name: start service
service:
name: httpd
state: started
enabled: true

remove_httpd.yaml

---
- hosts: web2
remote_user: root
gather_facts: no
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=/data/html/ state=absent

4.安装和删除mysql5.7

mysql: http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

.
├── ansible.cfg
├── files
│ ├── my.cnf
│ ├── mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
│ └── mysqld.service
├── hosts
└── install_mysql5.7or8.0.yaml

install_mysql.yaml

---                                                                                                         
- hosts: web2
remote_user: root
gather_facts: yes
vars:
mysql_version: 5.7.37
mysql_file: mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.gz
mysql_root_password: 123456
tasks:
- name: install packages centos7
yum:
name:
- libaio
- perl-Data-Dumper
when: ansible_facts['distribution_major_version'] == '7'
- name: install package centos8
yum:
name:
- libaio
- perl-Data-Dumper
- ncurses-compat-libs
when: ansible_facts['distribution_major_version'] == '8'
- name: create mysql group
group:
name: mysql
gid: 306
- name: create mysql user
user:
name: mysql
uid: 306
group: mysql
shell: /sbin/nologin
system: true
create_home: false
home: /data/mysql
- name: copy tar to host
unarchive:
src: ./files/{{mysql_file}}
dest: /usr/local/
owner: root
group: root
- name: create linkfile
file:
src: /usr/local/mysql-{{mysql_version}}-linux-glibc2.12-x86_64
dest: /usr/local/mysql
state: link
- name: init data dir
shell: /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql
tags: data
- name: config my.cnf
copy:
src: ./files/my.cnf
dest: /etc/my.cnf
tags: conf
- name: service script
copy:
src: ./files/mysqld.service
dest: /etc/systemd/system
- name: reload
service:
daemon_reload: true
- name: PATH variable
copy:
content: 'PATH=/usr/local/mysql/bin:$PATH'
dest: /etc/profile.d/mysql.sh
- name: get password
shell: awk '/A temporary password/{print $NF}' /data/mysql/mysql.log
register: password
- name: start service
service:
name: mysqld
state: started
enabled: yes
- name: change password
# debug:
# msg: "{{ password.stdout}}"
shell: /usr/local/mysql/bin/mysqladmin -uroot -p'{{password.stdout}}' password {{mysql_root_password}}

files/mysqld.service

[Unit]                                                                                                      
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

files/my.cnf

[mysqld]                                                                                                    
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock

log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock

5.修改主机名为ip最后一位或者随机字符

hostname.yaml

- hosts: web1
tasks:
- name: generate random
shell:
#cmd: openssl rand -base64 12 | tr -dc '[:alnum:]'
#cmd: bash -c 'echo $((RANDOM%255))'
#cmd: echo $RANDOM | md5sum | cut -c 1-8
cmd: hostname -I | awk '{print $1}' #取ip
register: num
- name: show random
debug:
msg: "{{ num.stdout.split('.')[3] }}" #取ip最后一个数字
# - name: change hostname
# hostname:
# name: web-{{ num.stdout }}


6.传递参数给ansible 安装httpd

var2.yaml

---
- hosts: web2
remote_user: root
tasks:
- name: install package
yum:
name: "{{ pkgname }}"
state: present
#state: removed

ansible-playbook -e pkgname=httpd var2.yaml

7.yaml中直接定义变量,创建用户和组

var3.yaml

---
- hosts: web2
remote_user: root
vars:
user_name: user1
group_name: group1
tasks:
- name: create group {{ group_name }}
group:
name: "{{ group_name }}"
state: present
- name: create user {{ user_name }}
user:
name: "{{ user_name }}"
group: "{{ group_name }}"
state: present

8.获取ip并且创建ip为名字的目录 或者文件

var4.yaml

---
- hosts: web2
vars:
#collect_info: "/data/test/{{ansible_default_ipv4['address']}}"
collect_info: "/data/test/{{ansible_facts.default_ipv4.address}}"
tasks:
- name: print
debug:
msg: "{{ansible_facts.default_ipv4.address}}"
- name: create ip directory
file:
name: "{{collect_info}}"
state: directory

- hosts: web2
vars:
suffix: "txt"
file: "{{ ansible_facts.nodename }}.{{suffix}}"

tasks:
- name: test var
file: path="/data/{{file}}" state=touch