做美妆网站名称,钱包网站建设策划,网站建设都分几个阶段,网络营销是什么时候出现的一、Ansible介绍 1.安装ansible: yum install ansible -y 2.ansible的架构图#xff1a; 3.ansible四部分#xff1a; inventory:ansible管理的主机信息#xff0c;包括ip地址、ssh端口、账号和密码等 modules:任务均由模块完成 plugins:增加ansible的核心功能 pla…一、Ansible介绍 1.安装ansible: yum install ansible -y 2.ansible的架构图 3.ansible四部分 inventory:ansible管理的主机信息包括ip地址、ssh端口、账号和密码等 modules:任务均由模块完成 plugins:增加ansible的核心功能 playbooks:模块化当以一些列任务供外部调用 4.主机清单(示例代码)
[webservers]
192.168.142.152
192.168.142.153
www.xiao.org 5.命令行使用ad-hoc可以输入内容快速执行某个操作 远程连接主机认证
[webservers]
192.168.142.152 ansible_ssh_userroot ansible_ssh_passxiao
192.168.142.153 ansible_ssh_userroot ansible_ssh_passxiao ssh密钥对认证ansible是基于ssh进行远程连接执行命令
[webservers]
192.168.142.152 ansible_ssh_userroot ansible_ssh_key/root/.ssh/id_rsa
192.168.142.153 ansible_ssh_userroot 上述的密钥信息也可以在ansible.cfg配置文件中指定 另外也可以先建立起控制主机和受控主机之间的ssh的免密连接通过在控制主机上创建一对密钥对将公钥通过ssh_copy_id root192.168.142.152/3传递到被控主机上可以顺利执行ansible的ad-hoc命令。 6.ansible命令行常用的选项 -C / --check playbook.yml的语法检查不执行任何操作 -e vartest 设置附加变量 keyvalue --userxiao ssh连接的用户,默认为none -k ssh连接用户的密码 -b / --become 提权默认为root -K 提权密码 7.命令行使用 ansible all -m ping 网络连通性测试 ansible all -m shell -als /root -u root -k 在命令行执行脚本查看/root信息 ansible webservers -m copy -a src/etc/hosts dest/tmp/host 指定webservers主机组中的主机使得将本主机的指定文件复制到指定主机组的指定目录下。
二、ansible的常用模块 1.shell在远程主机上执行脚本: ansible webservers -m shell -a ls /root 其中如果执行多条shell语句可以使用分号将其分隔开。 2.copy将文件复制到远程主机: ansible webservers -m copy -a src/etc/hosts dest/tmp/host mode777 ownerxiao groupxiao backupyes 上述指令的作用时将指定的文件复制到远程主机上并指定复制后的主机的属主和文件的权限其中的backup表示如果远程主机上该文件存在会将其创建为一个备份。 3.file(管理文件和文件属性) ansible webservers -m file -a path/tmp/hosts stateabsent 上述命令表示删除远程主机上的path路径的文件或者递归删除目录 上述中state参数的值决定对文件进行什么操作其中absent表示删除present表示创建 4.yum(软件包管理) ansible webservers -m yum -a namehttpd statelatest 该条命令表示在远程主机上安装最新版本的http服务,其中state可以取absent(卸载) present(安装yum源中的软件包) latest(安装最新的软件) 5.systemd(管理服务) ansible webservers -m systemd -a namehttpd statestarted enabledyes 该命令表示启动httpd服务并使其开机自启动也可以设定其服务状态为stopped restarted reloaded 6.unarchive(解压) ansible webservers -m unarchive -a srctest.tar.gz dest/tmp 该命令表示将本机文件的压缩文件解压到远程主机的指定目录上 7.debug(执行过程中打印语句) ansible webservers -m debug -a varhostvars[inventory_hostname] 该命令表示打印远程主机的所有变量
三、Playbook的简单操作 1.主机和用户
- hosts: webserversremote_user: xiaobecome: yesbecome_user: yes 2.定义变量
#命令行传递
-e vartest
#主机变量和组变量在inventory中定义变量#主机变量
[webservers]
192.168.142.152 ansible_ssh_userroot hostnameweb1
192.168.142.153 ansible_ssh_userroot hostnameweb2#主机组变量
[webservers:vars]
ansible_ssh_userroot 单文件存储 /etc/ansible/group_vars/all.yml 对所有的主机有效 /etc/ansible/host_vars/webservers.yml 只对webservers主机组的主机生效 在playbook中自定义变量 - hosts: webservers vars: http_port: 80 server_name: www.xiao.org register变量将其他命令的运行结果保存下来作为变量的值 -shell: /etc/passwd register: result -debug: var: result 3.任务列表
tasks:
- name: 安装nginx最新版yum: pkgnginx statelatest 4.任务控制 可以将大的playbook中的多个任务进行标记可以让其只执行有特殊标记的任务或者让其跳过执行有特殊标记的任务。
tasks:
- name 安装nginx最新版yum: pkgnginx statelatesttags: install
- name: 写入nginx的配置文件template: src/test/nginx.conf dest/etc/nginx/nginx.conftags: config ansible-playbook test.yml --tags install 表示只执行带有install标签的任务 ansible-playbook test.yml --skip-tags install 表示跳过带有install标签的任务 5.流程控制
tasks:
- name: 仅在192.168.142.152上执行任务debug msg {{ansible_default_ipv4.address}}when: ansible_default_ipv4.address 192.168.142.152 循环
tasks:
-name 批量创建用户user: name{{ item }} statepresent groupsxiaowith_items:- testuser1- testuser2 常用的循环语句 with_items 标准循环 with_fileglob 遍历目录文件 with_dict 遍历字典