在这个用于Ansbile的mysql数据库模块列表中,有一个用于创建数据库或创建用户等.
我想针对预先存在的表运行查询,并使用该查询的结果填充Ansible变量(IP地址列表和节点类型),我将根据节点类型运行不同的任务.
怎么能在Ansible中完成?
我正在尝试让这个任务在本地运行(在运行playbook的机器上):
- name: get the local repo's branch name
local_action: git branch | awk '/^\*/{print $2}'
register: branchName
Run Code Online (Sandbox Code Playgroud)
我尝试了很多变化而没有成功
所有其他任务都是在目标主机上运行,这就是为什么不能选择本地运行整个playbook的原因
TASK: [get the local repo's branch name] **************************************
<127.0.0.1> REMOTE_MODULE git branch | awk '/^\*/{print $2}'
<127.0.0.1> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172 && echo $HOME/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172']
<127.0.0.1> PUT /tmp/tmpQVocvw TO /home/max/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172/git
<127.0.0.1> EXEC ['/bin/sh', '-c', '/usr/bin/python /home/max/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172/git; rm -rf /home/max/.ansible/tmp/ansible-tmp-1407258765.57-75899426008172/ >/dev/null 2>&1']
failed: [portal-dev] => {"failed": true}
msg: this module requires key=value arguments (['branch', '|', 'awk', …Run Code Online (Sandbox Code Playgroud) 在我的剧本中我有
- name: Grab h5bp/server-configs-nginx
git: repo=https://github.com/h5bp/server-configs-nginx.git
dest=/tmp/server-configs-nginx
version="3db5d61f81d7229d12b89e0355629249a49ee4ac"
force=yes
- name: Copy over h5bp configuration
command: cp -r /tmp/server-configs-nginx/{{ item }} /etc/nginx/{{ item }}
with_items:
- "mime.types"
- "h5bp/"
Run Code Online (Sandbox Code Playgroud)
这引发了ansible-lint的警告:
[ANSIBLE0006] cp used in place of copy module
/Users/austinpray/Dropbox/DEV/opensauce/bedrock-ansible/roles/nginx/tasks/main.yml:0
Task/Handler: Copy over h5bp configuration
Run Code Online (Sandbox Code Playgroud)
所以这提出了一个问题:有没有更好的方法来使用ansible模块而不是命令?
我有一个这样的对象
objs:
- { key1: value1, key2: [value2, value3] }
- { key1: value4, key2: [value5, value6] }
Run Code Online (Sandbox Code Playgroud)
我想创建以下文件
value1/value2
value1/value3
value4/value5
value4/value6
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用双循环 with_items
我的基本问题是,在创建一组aws服务器时,我想配置它们以了解彼此.
创建每个服务器后,它们的详细信息将保存在已注册的"服务器"var中(如下所示).我真正希望在创建之后能够执行的任务是这样的:
- name: Add servers details to all other servers
lineinfile:
dest: /path/to/configfile
line: "servername={{ item.1.private_ip }}"
delegate_to: "{{ item.0.public_dns_name }}"
with_nested:
- list_of_servers
- list_of_servers
Run Code Online (Sandbox Code Playgroud)
将列表提供两次到'with_nested'是必不可少的.
获取列表清单很容易:
"{{ servers.results | map(attribute='tagged_instances') | list }}"
Run Code Online (Sandbox Code Playgroud)
返回:
[
[ { "private_ip": "ip1", "public_dns_name": "dns1" } , { ... }],
[ { ... }, { ... } ]
]
Run Code Online (Sandbox Code Playgroud)
但你怎么会把它变成:
[
{ "private_ip": "ip1", "public_dns_name": "dns1" },
{ ... },
{ ... },
{ ... }
]
Run Code Online (Sandbox Code Playgroud)
'servers'注册的var看起来像:
"servers": {
"changed": …Run Code Online (Sandbox Code Playgroud) 我有存储在项目存储库中的配置文件的模板.我想要做的是在从存储库克隆项目之后,使用Ansible的模板模块在远程服务器上使用该模板创建配置文件.
查看模板模块的文档,似乎该src属性仅支持本地文件.
我想避免使用我的Ansible playbook存储配置模板,因为我更有意义的是将这些项目特定模板保存在项目存储库中.
我可以使用模板模块的替代方案吗?
我创建了一个脚本来启动/停止我的应用程序.现在我想将它添加为centos系统服务.首先,我创建了一个任务来创建从我的脚本到/etc/init.d/service_name的链接,如下所示.
---
- name: create startup link
file: src={{ cooltoo_service_script }} dest={{ cooltoo_service_init }} state=link
Run Code Online (Sandbox Code Playgroud)
创建服务后,我想将其添加到系统服务.用于执行此操作的命令是"chkconfig --add service_name".我想知道是否有一个ansible模块来做,而不是在ansible-playbook文件中硬编码命令.我查看了这个页面http://docs.ansible.com/ansible/service_module.html但它只显示了如何管理服务而不是创建一个新服务.
我正在编写的剧本中有以下任务(结果列在<>中的调试语句旁边):
- debug: var=nrpe_installed.stat.exists <true>
- debug: var=force_install <true>
- debug: var=plugins_installed.stat.exists <true>
- name: Run the prep
include: prep.yml
when: (nrpe_installed.stat.exists == false or plugins_installed.stat.exists == true or force_install == true)
tags: ['prep']
- debug: var=nrpe_installed.stat.exists <true>
- debug: var=force_install <true>
- debug: var=force_nrpe_install <false>
- name: Install NRPE
include: install-nrpe.yml
when: (nrpe_installed.stat.exists == false or force_install == true or force_nrpe_install == true)
tags: ['install_nrpe']
vars:
nrpe_url: 'http://url.goes.here'
nrpe_md5: 3921ddc598312983f604541784b35a50
nrpe_version: 2.15
nrpe_artifact: nrpe-{{ nrpe_version }}.tar.gz
nagios_ip: {{ nagios_ip …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Ansible上执行我的第一个远程shell脚本.我首先生成并复制了SSH密钥.这是我的yml文件:
---
- name: Ansible remote shell
hosts: 192.168.10.1
user: myuser1
become: true
become_user: jboss
tasks:
- name: Hello server
shell: /home/jboss/script.sh
Run Code Online (Sandbox Code Playgroud)
然而,当启动剧本时,结果是"没有主机匹配":
ansible-playbook setup.yml
PLAY [Ansible remote shell
********************************************
skipping: no hosts matched
PLAY RECAP ********************************************************************
Run Code Online (Sandbox Code Playgroud)
我也试过使用主机名(而不是IP地址),但没有改变.有帮助吗?
我的库存文件的内容 -
[webservers]
x.x.x.x ansible_ssh_user=ubuntu
[dbservers]
x.x.x.x ansible_ssh_user=ubuntu
Run Code Online (Sandbox Code Playgroud)
在我的任务文件中,它是共同的角色,即它将在两台主机上运行,但我想在主机Web服务器上运行以下任务,而不是在库存文件中定义的dbservers中运行
- name: Install required packages
apt: name={{ item }} state=present
with_items:
- '{{ programs }}'
become: yes
tags: programs
Run Code Online (Sandbox Code Playgroud)
模块有用还是有其他方法?我怎么能这样做?