ansible

Ansible-playbook

匆匆过客 提交于 2020-02-01 20:44:29
1.Ad-Hoc简介 1)ad-hoc命令 执行shell命令,或shell脚本。可以执行一些简单的命令,不需要将这些执行的命令特别保存下来。 适合执行简单的命令 2)Ansible playbook 可以解决比较复杂的任务,可以将命令保存下来。适合执行配置管理或部署客户机 2.Ansible playbook playbook是由一个或多个模块组成的,使用多个不同的模块,完成一件事 playbook通过yaml语法识别描述的状态文件.扩展名是yaml 1).YAML三板斧 缩进 YAML使用一个固定的缩进风格表示层级结构,每个缩进由两个空格组成,不能使用tab(默认一个tab=4个空格) 解决方法: [root@m01 ~]# cat .vimrc set number set tabstop=2 冒号 以冒号结尾的除外,其他所有冒号后面必须有空格 短横线 表示列表项,使用一个短横杠加一个空格。 多个项使用同样的缩进级别作为同一列表。 who 谁 what 事情 how 动作 ansible-playbook命令格式 ansible-playbook [option] filename 常用选项: -C, --check 模拟运行 --list-hosts 列出剧本主机清单 [root@m01 ansible_playbook]# ansible-playbook --list

Ansible模块——script

早过忘川 提交于 2020-02-01 16:11:26
在本地写一个脚本,然后在远程服务器上执行。脚本一直存在于 ansible 管理主机本地,不需要手动拷贝到远程主机后再执行。 一、主要参数 free_form:required,指定需要执行的脚本,脚本位于 ansible 管理主机本地。 chdir: 远程主机中执行工作目录。 creates:指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本。 removes:指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本。 二、示例 上面命令表示 ansible 主机中的 /testdir/testscript.sh 脚本将在 host1 主机中执行,执行此脚本之前,会先进入到 host1 主机中的 /opt 目录 [ root@ansible-manager ~ ] # ansible host1 -m script -a "chdir=/opt /testdir/testscript.sh" host1 主机中的 /testdir/testfile1文件已经存在,ansible 主机中的 /testdir/testscript.sh 脚本将不会在 host1 主机中执行。 [ root@ansible-manager ~ ] # ansible host1 -m script -a "creates=/testdir/testfile1 /testdir

Ansible模块——setup

*爱你&永不变心* 提交于 2020-02-01 16:03:02
收集远程主机的信息 Gathers facts about remote hosts 一、主要参数 filter :条件过滤。 二、示例 1.获取IP地址 [ admin@node1 tmp ] $ ansible 192.168.20.138 -m setup -a 'filter=ansible_default_ipv4' 192.168.20.138 | SUCCESS = > { "ansible_facts" : { "ansible_default_ipv4" : { "address" : "10.2.13.186" , "alias" : "eth0" , "broadcast" : "10.2.13.255" , "gateway" : "10.2.13.1" , "interface" : "eth0" , "macaddress" : "52:54:00:7d:e2:a7" , "mtu" : 1500, "netmask" : "255.255.255.0" , "network" : "10.2.13.0" , "type" : "ether" } } , "changed" : false } 2.获取内存信息 [ admin@node1 tmp ] $ ansible 192.168.20.138 -m setup -a 'filter=ansible

How to get variables from ansible result

牧云@^-^@ 提交于 2020-02-01 04:53:26
问题 I have a shell script whose output is an echo of the following format <variable_1>;<variable_2>;<variable_3> etc I want to use these variables and run a mysql query to update a DB like so mysql -u<user> -p<password> -h<host> -e'insert into test_table values ("variable_1","variable_2","variable_3")' My ansible playbook looks like this. --- - hosts: infoServers sudo: yes gather_facts: no tasks: - name: gather info script: get_hostdata.sh register: result - name: print result local_action:

How to get variables from ansible result

落爺英雄遲暮 提交于 2020-02-01 04:52:36
问题 I have a shell script whose output is an echo of the following format <variable_1>;<variable_2>;<variable_3> etc I want to use these variables and run a mysql query to update a DB like so mysql -u<user> -p<password> -h<host> -e'insert into test_table values ("variable_1","variable_2","variable_3")' My ansible playbook looks like this. --- - hosts: infoServers sudo: yes gather_facts: no tasks: - name: gather info script: get_hostdata.sh register: result - name: print result local_action:

ansible之playbook循环(loop)

自作多情 提交于 2020-01-30 22:41:04
当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过loop语句指明迭代的元素列表即可。loop的值是python list数据结构,每个task会循环读取list的值,然后后key的名称是item,list里面也支持python字典。 例子一:安装多个软件 tasks: - name: "Install Packages" yum: name={{ item }} state=latest loop: - httpd - mysql-server - php 例子二:批量创建多个用户。(with_items) - hosts: slave remote_user: suixiaofeng sudo: yes tasks: - name: "add user" user: name={{ item.name }} state=present groups={{ item.groups }} loop: - {name: "test5", groups: "tom"} - {name: "test6", groups: "tom"} 其中引用变量时前缀item变量是固定的,而item后跟的键名就是在loop中定义的字典键名。 来源: CSDN 作者: JetBrains_孙健 链接: https://blog.csdn.net

tar压缩文件 .tar.gz

拥有回忆 提交于 2020-01-30 13:39:29
打包并压缩文件 tar -zcf ansible.tar.gz ansible/* z gzip属性 c 建立压缩文件 f 指定文件名 v 显示过程 解压文件 tar xzf ansible.tar.gz tar xf ansible.tar.gz -C /tmp/ C 指定输出位置 PS: tar压缩解压缩命令详解 linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结 来源: https://www.cnblogs.com/firewalld/p/12242599.html

Ansible inside script command not found

独自空忆成欢 提交于 2020-01-30 13:21:09
问题 Ansible: - name: Node package manager npm: name: pm2 global: yes notify: - restart nginx - name: start the app script: ../files/common/pm2.sh app_name {{ user }} tags: test Script file: #!/bin/bash APP_NAME=$1 USER=$2 if [ "$USER" != "" ]; then PATH="/home/$USER/" else PATH="/var/www/" fi pm2 describe ${APP_NAME} > /dev/null # line no 11 RUNNING=$? if [ "${RUNNING}" -ne 0 ]; then cd ${PATH}${APP_NAME}/ && pm2 start npm --name "${APP_NAME}" -- start else pm2 restart ${APP_NAME} fi; When I

Ansible inside script command not found

戏子无情 提交于 2020-01-30 13:20:56
问题 Ansible: - name: Node package manager npm: name: pm2 global: yes notify: - restart nginx - name: start the app script: ../files/common/pm2.sh app_name {{ user }} tags: test Script file: #!/bin/bash APP_NAME=$1 USER=$2 if [ "$USER" != "" ]; then PATH="/home/$USER/" else PATH="/var/www/" fi pm2 describe ${APP_NAME} > /dev/null # line no 11 RUNNING=$? if [ "${RUNNING}" -ne 0 ]; then cd ${PATH}${APP_NAME}/ && pm2 start npm --name "${APP_NAME}" -- start else pm2 restart ${APP_NAME} fi; When I

Is it possible to create a fully self-contained Python package?

折月煮酒 提交于 2020-01-30 06:43:27
问题 The question Ansible is a python moduel, installable via pip. It relies on several dependencies, also pip modules. Is it possible to "roll up" all of those dependencies and Ansible itself into some sort of a single package, that can be installed offline, without root? It's highly preferable to not need pip for the install, although it will be available for package creation. Extra background I'm trying to install Ansible on one of our servers. The server does not have access to the internet,