一、安装
yum install -y ansible
二、密钥授权
ssh-keygen -t rsa
注释:-t type指定要创建的密钥类型。 rsa 加密算法
三、模块介绍(https://www.cnblogs.com/mcsiberiawolf/articles/10056777.html)
修改/etc/ansible/hosts文件
[host]
10.10.10.10 ansible_ssh_port=9022 ansible_ssh_user="root" ansible_ssh_pass="123456" ansible_become_pass=123456
四、命令行模块使用
ansible host -m ping
ansible host -m shell -a 'ls -l /root'
ansible host -m cron -a 'name="cron1" job="ntpdate time1.aliyun.com minute=0 hour=*/1"'
ansible host -m copy -a "src=/root/shell.sh dest=/root/shell.sh" #src=源地址 dest=目标地址
ansible host -m script -a "/root/shell.sh"
ansible host -m systemd -a "daemon_reload=yes" #reload加载nginx
ansible host -m systemd -a "-name=nginx state=restarted"
ansible host -m group -a "name=dada" #用户创建组
五 、 shell和command模块区别
shell 可执行shell内置命令和特性(比如管道符,重定向)
command 无法使用shell内置命令
六、playbook案例
---
- hosts: data
become: yes
become_user: root
become_method: su
tasks:
- name: fusioninventory-agent
yum: name=fusioninventory-agent.x86_64 name=nginx
- name: modify
shell: ll /root
- name: start
service: name=nginx state=started
- name: test
command: '/usr/bin/systemctl start firewalld'
service:
name: nginx
enable: true
state: started