每次使用都先查查,很是头疼,整理一下
安装:
yum install -y ansible
ansible -h 查看帮助
规划:
master:192.168.68.10 ansible
node1 :192.168.68.20
node2 :192.168.68.30
master,node1,node2 分别执行:ssh-keygen -t rsa生成秘钥
在ssh下面建立authorized_keys文件
将master 公钥复制到node1,node2的authorized_keys文件中实现master到node1,node2的免密码登录
编辑master配置组文件
vim /etc/ansible/hosts 编辑ansible的组文件
[ceshi]
192.168.68.20
192.168.68.30
执行命令是否生效:
本机执行测试
[root@master ~]# ansible 127.0.0.1 -a \'date\'
127.0.0.1 | SUCCESS | rc=0 >>
Wed Dec 19 13:27:51 CST 2018
所有的群组执行测试
[root@master ~]# ansible all -a \'date\'
192.168.68.20 | SUCCESS | rc=0 >>
Wed Dec 19 13:28:15 CST 2018
192.168.68.30 | SUCCESS | rc=0 >>
Wed Dec 19 13:28:15 CST 2018
指定测试组执行ping命令测试
[root@master ~]# ansible all -l ceshi -m ping
192.168.68.20 | SUCCESS => {
\"changed\": false,
\"ping\": \"pong\"
}
192.168.68.30 | SUCCESS => {
\"changed\": false,
\"ping\": \"pong\"
}
使用:
ansible all -m file -a \"path=/home/haoxiaoyu state=touch\" 远程创建一个文件
ansible all -m file -a \"path=/home/haoxiaoyu state=absent\" 远程删除一个文件
ansible all -m copy -a \"src=/home/hello.txt dest=/home/ owner=root mode=0644\" 拷贝文件
ansible all -m copy -a \"src=/home/hello.txt dest=/home/ owner=root mode=0644 backup=yes\"
ansible all -m service -a \"name=nginx state=restarted sleep=10\"
nginx 重启,10秒后重启
ansible all -m get_url -a \'url=http://baidu.com/1.txt dest=/tmp/jesse/ mode=0644\'
Ansible Ad-hoc
Ad-hoc 是什么?
1,命令行工具
2,适用于业务的变更
3,所见即所得
将ceshi的所有主机的/root目录下打印出来
[root@master ~]# ansible ceshi -m shell -a \"ls /root\" --user=root
192.168.68.30 | SUCCESS | rc=0 >>
anaconda-ks.cfg
192.168.68.20 | SUCCESS | rc=0 >>
anaconda-ks.cfg
实战安装mariadb-server 版本为最新
ansible ceshi -m yum -a \"name=mariadb-server state=latest\"
一般启动命令:
systemctl start mariadb
systemctl stop mariadb
使用ansible控制mysql的启动:
ansible ceshi -m systemd -a \"name=mariadb state=started\"
使用ansible查看进程
ansible ceshi -m shell -a \"ps -ef|grep mysql|grep -v grep\"
[root@master ~]# ansible ceshi -m shell -a \"ps -ef|grep mysql|grep -v grep\"
192.168.68.20 | SUCCESS | rc=0 >>
mysql 2035 1 0 22:35 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe -- dir=/usr
mysql 2197 2035 0 22:35 ? 00:00:00 /usr/libexec/mysqld -- dir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
192.168.68.30 | SUCCESS | rc=0 >>
mysql 1777 1 0 22:35 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe -- dir=/usr
mysql 1939 1777 0 22:35 ? 00:00:00 /usr/libexec/mysqld -- dir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
说明启动成功!
playbook使用:
1,声明配置
2,编排复杂的任务
3,控制任务的执行
支持的特性:
1,变量定义
2,顺序结构
3,选择结构
4,循环结构
基本的结构:
host:被操作的机器的正则
remote_user:登录机器的用户
tasks:需要在机器上执行的任务
hello word playbook
远程创建一个文本
vim ceshi.yml
---
- hosts: ceshi
remote_user: root
tasks:
- name: Hello Word
shell: touch cehsi.txt
开始执行:
ansible-playbook ceshi.yml
play book 变量的使用:
注意:使用变量务必加\"\" 否则执行失败
vim ceshi2.yml
---
- hosts: ceshi
remote_user: root
vars:
com: touch hao.txt
tasks:
- name: Hello Word
shell: \"{{ com }}\"
ansible-playbook ceshi2.yml
实战:
根据系统安装采用不同的方式安装redis和flask
注意:-name 是安装的时候显示屏幕的,有利于排查
[root@master ~]# cat ceshi3.yml
---
- hosts: ceshi
remote_user: root
become: true
tasks:
- name: install python for centos
yum:
name: \"{{ item }}\"
state: installed
with_items:
- python-devel
- python-setuptools
when: ansible_distribution == \'CentOS\'
- name: install python for ubuntu
apt:
name: \"{{ item }}\"
state: latest
update_cache: yes
with_items:
- libpython-dev
- python-setuptools
when: ansible_distribution == \'Ubuntu\'
- name: install pip
shell: easy_install pip
- name: pip install flask and redis
pip:
name: \"{{ item }}\"
with_items:
- flask
- redis
开始执行:
ansible-playbook ceshi3.yml
执行的日志:
[root@master ~]# ansible-playbook ceshi3.yml
[WARNING]: Ignoring invalid attribute: state
ERROR! the field \'hosts\' is required but was not set
[root@master ~]# ansible-playbook ceshi3.yml
[WARNING]: Ignoring invalid attribute: state
PLAY [ceshi] ****************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************
ok: [192.168.68.30]
ok: [192.168.68.20]
TASK [install python for centos] ********************************************************************************************
changed: [192.168.68.30] => (item=[u\'python-devel\', u\'python-setuptools\'])
changed: [192.168.68.20] => (item=[u\'python-devel\', u\'python-setuptools\'])
TASK [install python for ubuntu] ********************************************************************************************
skipping: [192.168.68.20] => (item=[])
skipping: [192.168.68.30] => (item=[])
TASK [install pip] **********************************************************************************************************
changed: [192.168.68.30]
changed: [192.168.68.20]
TASK [pip install flask and redis] ******************************************************************************************
changed: [192.168.68.20] => (item=flask)
changed: [192.168.68.30] => (item=flask)
changed: [192.168.68.20] => (item=redis)
changed: [192.168.68.30] => (item=redis)
PLAY RECAP ******************************************************************************************************************
192.168.68.20 : ok=4 changed=3 unreachable=0 failed=0
192.168.68.30 : ok=4 changed=3 unreachable=0 failed=0
192.168.68.30 : ok=4 changed=3 unreachable=0 failed=0
四个任务,执行了3个
继续阅读与本文标签相同的文章
上一篇 :
华为手机重要好消息:今后微信发图爽翻了
下一篇 :
vue中路由控制登陆不能随便跳转
-
怎样才能让用户更喜欢你的APP应用
2026-05-18栏目: 教程
-
上线5个月:夸克无障碍服务瞄准视障用户
2026-05-18栏目: 教程
-
重磅,蒲公英私有化部署上线,724成为贴身助理!
2026-05-18栏目: 教程
-
华登区块狗区块猫APP系统平台开发源码定制
2026-05-18栏目: 教程
-
网络零售商的规模越大,对全品类经营的需求就更强烈
2026-05-18栏目: 教程
