ansible安装jdk并写入环境变量
jdk除了包的安装,还需要写入环境变量
- name: Create taishi dir file: path={{ taishi_dir }} state=directory owner={{ taishi_user }} group={{ taishi_user }} tags: jdk - name: visit old_jdk shell: rpm -qa | grep jdk register: jdk_result ignore_errors: True tags: jdk - name: visit old_java shell: rpm -qa | grep java register: java_result ignore_errors: True tags: jdk - name: uninstall old_jdk shell: rpm -qa | grep jdk | xargs rpm -e --nodeps when: jdk_result is succeeded tags: jdk - name: uninstall old_java shell: rpm -qa | grep java | xargs rpm -e --nodeps when: jdk_result is succeeded - name: Create new jdk dir file: path="{{ taishi_dir }}/jdk" state=directory owner={{ taishi_user }} group={{ taishi_user }} tags: jdk - name: Copy jdkpackage to hosts copy: src=jdk-8u60-linux-x64.tar.gz dest=/tmp tags: jdk - name: Install new jdk1.8 for hosts unarchive: src="/tmp/jdk-8u60-linux-x64.tar.gz" dest="{{ taishi_dir }}/jdk" copy=no mode=0755 tags: jdk #使用lineinfile就不会重复在/etc/profile文件中不会重复写入path内容 - name: set env lineinfile: dest=/etc/profile insertafter="{{item.position}}" line="{{item.value}}" state=present with_items: - {position: EOF, value: "export JAVA_HOME={{ taishi_dir }}/jdk/jdk1.8.0_60/"} - {position: EOF, value: "export PATH=$JAVA_HOME/bin:$PATH"} tags: jdk #使用echo的shell命令的方式多次执行就会多次追加内容 - name: set env2 shell: "echo 'export JAVA_HOME={{ taishi_dir }}/jdk/jdk1.8.0_60/' >> /etc/profile" #执行java命令之前可以手动source最新的环境变量 - name: check java version with env shell: source /etc/profile && java -version tags: jdk #执行java命令的时候可以使用java的全路径来调用 - name: check java version with fullpath shell: "{{ taishi_dir }}/jdk/jdk1.8.0_60/bin/java -version" tags: jdk
ansible生成elastic密码
1.配置hosts
[elastic]
192.168.30.174 默认elastic组的第一个节点为master,其它节点为数据节点
192.168.30.175
192.168.30.176
2.自动生成密码
3.生成指定密码
4.自动生成证书
5.es多次生成密码
1.还原config目录下的elasticsearch.keystore的文件
2.删除es自动创建的索引,es会自动把用户信息存储到默认的索引中
1.查看索引列表
curl -u elastic:JmhE6E6RQrXopYSrQIQg http://127.0.0.1:9200/_cat/indices
2.删除指定索引
curl -XDELETE -u elastic:JmhE6E6RQrXopYSrQIQg http://127.0.0.1:9200/.security-7
1.查看索引列表
curl -u elastic:Transfar@2022 http://127.0.0.1:9200/_cat/indices
curl -u elastic:Transfar@123 --insecure https://192.168.30.102:9200/_cat/indices
curl -u elastic:Transfar@2022 --insecure https://instance:9200/_cat/indices
2.删除指定索引
curl -XDELETE -u elastic:Transfar@2022 http://instance:9200/.security-7
curl -XDELETE --insecure -u elastic:Transfar@2022 https://instance:9200/.security-7
3.es接口信息
curl -u elastic:Transfar@2022 --insecure https://instance:9200/_cat/master
curl -u elastic:Transfar@2022 --insecure https://instance:9200/_cat/nodes
4.更新索引
curl -u elastic:Transfar@123 --insecure -XPUT -H "Content-Type: application/json" https://192.168.30.102:9200/_all/_settings -d '{"index.blocks.read_only_allow_delete": null}'
5.执行单个角色下所有的tasks 不管tasks/main.yml中的tasks定义不同的tags
ansible-playbook -i hosts --tags elastic-master site.yml
tags定义在site.yml中
- hosts: elastic-master
gather_facts: true
roles:
- { role: elastic-master, tags: 'elastic-master' }
6.执行某个tags下的tasks集合,而不管tasks是否属于同一个角色
ansible-playbook -i hosts --tags esca site.yml
tags定义在tasks/main.yml中
- name: "Start elastic cluster"
shell: "nohup su - {{ taishi_user }} -c {{ taishi_dir }}/elastic/elasticsearch-7.8.1/bin/elasticsearch &"
tags: startes
7.在指定的文件中追加行
- name: set esmaster hosts
lineinfile: dest=/etc/hosts insertafter="{{item.position}}" line="{{item.value}}" state=present
with_items:
- {position: EOF, value: "{{ groups['elastic-master'][0] }} instance"}
tags: jdk
ansible安装es
1.启动成功,但设置用户密码的时候出错
2.启动失败(配置文件配置不合要求)
[root@localhost ~]# ulimit -n 65535 && ulimit -u 4096 && su - admin -c /app/taishi/elastic/elasticsearch-7.8.1/bin/elasticsearch
配置文件一定要按照下面的格式,一项配置都不能少。
cluster.name: es-cluster node.name: node-174 cluster.initial_master_nodes: node-174 path.data: /home/admin/es-cluster/data path.logs: /home/admin/es-cluster/logs network.host: 0.0.0.0 discovery.seed_hosts: ['192.168.30.174'] xpack.security.enabled: true xpack.security.http.ssl.enabled: true xpack.security.transport.ssl.enabled: true xpack.security.http.ssl.keystore.path: /app/taishi/elastic/elasticsearch-7.8.1/config/certs/elastic-certificates.p12 xpack.security.transport.ssl.keystore.path: /app/taishi/elastic/elasticsearch-7.8.1/config/certs/elastic-certificates.p12
3.生成密码方式.在生成证书的时候默认用的域名就是instance.
4.启动程序的时候带上环境变量
ulimit -n 65535 && ulimit -u 4096 && su - admin -c /app/taishi/elastic/elasticsearch-7.8.1/bin/elasticsearch
ansible拉取远程主机上的文件
fetch模块从远端获取文件,默认将这些文件按照树型方式按主机名分类进行存储。如果需要按照自定义目录进行存储,需要设置自定义参数
dest设置的路径最后带不带/,存储的结果都会不一样.
ansible安装mysql
- name: Create mysql base dir file: path="{{ taishi_dir }}/mysql" state=directory owner={{ taishi_user }} group={{ taishi_user }} recurse=yes tags: mysql - name: Create mysql data dir file: path="{{ mysql_datadir }}" state=directory owner={{ taishi_user }} group={{ taishi_user }} recurse=yes tags: mysql - name: Copy mysql.tar.gz to {{ groups["mysql"][0] }} copy: src="mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz" dest=/tmp tags: mysql - name: Install mysql for {{ groups["mysql"][0] }} unarchive: src="/tmp/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz" dest="{{ taishi_dir }}/mysql" copy=no mode=0755 tags: mysql - name: "chown mysql dir to {{ taishi_user }}" file: path="{{ taishi_dir }}/mysql" state=directory owner={{ taishi_user }} group={{ taishi_user }} recurse=yes tags: mysql - name: Create etc dir file: path="{{ taishi_dir }}/etc" state=directory owner={{ taishi_user }} group={{ taishi_user }} recurse=yes tags: mysqlcnf - name: "copy the my.cnf" template: src=my.cnf.j2 dest="{{ taishi_dir }}/etc/my.cnf" owner={{ taishi_user }} group={{ taishi_user }} mode=0755 tags: mysqlcnf - name: "chown mysql dir to {{ taishi_user }}" file: path="{{ taishi_dir }}/etc" state=directory owner={{ taishi_user }} group={{ taishi_user }} recurse=yes tags: mysqlcnf - name: "copy the db.sql" copy: src=db.sql dest="{{ taishi_dir }}/mysql/db.sql" owner={{ taishi_user }} group={{ taishi_user }} mode=0755 tags: mysqlinitdb - name: "copy the initdb.sh" template: src=initdb.sh.j2 dest="{{ taishi_dir }}/mysql/initdb.sh" owner={{ taishi_user }} group={{ taishi_user }} mode=0755 tags: mysqlinitdb - name: "sh initdb.sh" shell: "{{ taishi_dir }}/mysql/initdb.sh" ignore_errors: True tags: mysqlinitdb
#!/bin/bash {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysqld --defaults-file={{ taishi_dir }}/etc/my.cnf --initialize nohup {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysqld --defaults-file={{ taishi_dir }}/etc/my.cnf & > /dev/null 2>&1 #Wait 300s for mysql to start echo "Wait for mysql to start..." count=0 for((i=1;i<=5;i++)); do count=`netstat -antp | grep {{ MYSQL_PORT }} | grep mysqld | wc -l` sleep 5 if [ "$count" -gt 0 ];then break fi done if [ "$count" -gt 0 ];then echo "Mysql success to start." else echo "Mysql failed to start in 5 minutes." fi sleep 3 tmpPasswd=`grep "temporary password" {{ mysql_datadir }}/error.log | grep -ioE "root@localhost:.*" | cut -d ' ' -f 2` newPasswd={{ MYSQL_CONNECT_PASSWORD }} {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysql -S {{ mysql_datadir }}/mysql.sock -u root -p"$tmpPasswd" --connect-expired-password -e "ALTER USER root@localhost IDENTIFIED BY "$newPasswd"" {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysql -S {{ mysql_datadir }}/mysql.sock -u root -p"$newPasswd" -e "CREATE USER {{MYSQL_CONNECT_USERNAME}}@'%' IDENTIFIED BY '{{MYSQL_CONNECT_PASSWORD}}'" {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysql -S {{ mysql_datadir }}/mysql.sock -u root -p"$newPasswd" -e "GRANT ALL PRIVILEGES ON *.* TO '{{MYSQL_CONNECT_USERNAME}}'@'%'" {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysql -S {{ mysql_datadir }}/mysql.sock -u root -p"$newPasswd" -e "CREATE USER {{MYSQL_CONNECT_USERNAME}}@'localhost' IDENTIFIED BY '{{MYSQL_CONNECT_PASSWORD}}'" {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysql -S {{ mysql_datadir }}/mysql.sock -u root -p"$newPasswd" -e "GRANT ALL PRIVILEGES ON *.* TO '{{MYSQL_CONNECT_USERNAME}}'@'localhost'" {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysql -S {{ mysql_datadir }}/mysql.sock -u root -p"$newPasswd" -e "FLUSH PRIVILEGES" #初始化数据 {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysql -S {{ mysql_datadir }}/mysql.sock -u root -p"$newPasswd" < {{ taishi_dir }}/mysql/db.sql #初始化数据后关闭数据库进程 # {{ taishi_dir }}/mysql/mysql-5.7.31-linux-glibc2.12-x86_64/bin/mysqladmin -S {{ mysql_datadir }}/mysql.sock -u root -p"$newPasswd" shutdown 2>&1 >/dev/null