我有一个 site.yml 剧本,当我使用这个配置时:
- hosts: target
tags:
- setup_target
tasks:
- name: See some data
debug: msg={{ hostvars[inventory_hostname][ansible_lsb].codename }}
Run Code Online (Sandbox Code Playgroud)
Ansible 报告我一个错误:
PLAY [target] *****************************************************************
GATHERING FACTS ***************************************************************
ok: [target]
TASK: [See some data] *********************************************************
fatal: [target] => One or more undefined variables: dict object has no element {u'release': u'14.04', u'major_release': u'14', u'codename': u'trusty', u'id': u'Ubuntu', u'description': u'Ubuntu 14.04.1 LTS'}
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/home/robe/site.retry
target : ok=1 changed=0 unreachable=1 …
Run Code Online (Sandbox Code Playgroud) 我正在使用lineinfile
Ansible模块来更改XML文件中的某些值.
我必须更新该XML文件中几个键的值.
为此,我需要传递正确的正则表达式,并确保正确放置值,即使我重复运行Playbook它不会添加任何额外的行或值,只需添加我需要的.
我们的ansible库存文件日益变得越来越大.所以我们想用目录和文件模块化它.比如说.
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
Run Code Online (Sandbox Code Playgroud)
这可以转换成
|--production
| |--WEBSERVERS
| | |--webservers
| |--DBSERVERS
| | |--dbservers
Run Code Online (Sandbox Code Playgroud)
当Web服务器是一个文件;
[webservers]
foo.example.com
bar.example.com
Run Code Online (Sandbox Code Playgroud)
和dbservers是一个文件;
[dbservers]
one.example.com
two.example.com
three.example.com
Run Code Online (Sandbox Code Playgroud)
for simple inventory file it works fine. Problem comes when I create group of groups.
like
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
[master:children]
webservers
dbservers
Run Code Online (Sandbox Code Playgroud)
I cant imagine a directory structure for this and it. Can someone please guide me to the right tutorial. Thanks
我在让变量在模板中工作时遇到问题。变量在我的剧本中有效,但在模板中,它们按原样呈现而不被其值替换。这是test-playbook.yml
我尝试的一个简单方法。
---
- name: Test playbook vars
hosts: webservers
vars:
hello_var: Hello World
hello_file_path: /tmp/hello_file.txt
tasks:
- name: Copy hello world file
copy: src=templates/hello_world.txt.j2 dest={{ hello_file_path }}
Run Code Online (Sandbox Code Playgroud)
在我中templates/hello_world.txt.j2
,我有以下内容
hi {{ hello_var }}
Run Code Online (Sandbox Code Playgroud)
运行剧本后,主机上/tmp/hello_world.txt
的内容与模板中的内容相同
hi {{ hello_var }}
Run Code Online (Sandbox Code Playgroud)
该变量hello_file_path
在剧本的作品使用,但该变量hello_var
在我的模板中使用不工作。
[Ansible version == 2.1.0]
In order to run a script which is present locally on the target server, we can use Ansible's "command" module. Following can be done easily:
- name: Executing getpkgs.sh to download packages.
command: sh "/path/to /dir/scriptName.sh" arg1 arg2 arg3 arg4
Run Code Online (Sandbox Code Playgroud)
I have my script names and the arguments stored in ansible variables. For example, the following variable contains all the script names and the arguments to be passed to those scripts:
scripts_to_execute:
- { filename: "/path/to/file/file1.sh", …
Run Code Online (Sandbox Code Playgroud) 我有一个从 Vagrantfile 调用的 ansible playbook:
config.vm.provision "ansible" do |ansible|
ansible.playbook = "provision/playbook.yml"
ansible.sudo = true
ansible.verbose = "vvvv"
ansible.limit = "all"
# ansible.inventory_path = "provision/hosts"
end
Run Code Online (Sandbox Code Playgroud)
这是剧本:
---
- hosts: all
roles:
- common
Run Code Online (Sandbox Code Playgroud)
我的目录结构是:
.
??? provision
? ??? hosts
? ??? playbook.yml
? ??? roles
? ??? common
? ??? install_conda.yml
? ??? reqs.yml
??? ubuntu-xenial-16.04-cloudimg-console.log
??? Vagrantfile
Run Code Online (Sandbox Code Playgroud)
我的问题是当我运行vagrant up
它不运行 install_conda.yml 和 reqs.yml
相关输出:
ansible/.vagrant/provisioners/ansible/inventory --sudo -vvvv provision/playbook.yml
Using /home/lowks/.ansible.cfg as config file
Loaded callback default …
Run Code Online (Sandbox Code Playgroud) 我必须使用Ansible检查Linux机器上的不同硬件和配置元素,而且我不确定如何做到(RAM,磁盘空间,DNS,CPU ...),我知道我几乎可以找到我只想了解这些不可思议的事实,但我不知道如何使用它。
例如,我必须检查RAM量是否至少为4GB,如果不是,则发出警报,因此我尝试了很多事情,并且...无济于事。
这是我尝试过的一个例子。
- hosts: client
remote_user: user
tasks:
- debug: var=ansible_memory_mb
- debug: msg="total RAM is {{ ansible_memory_mb.real.total }}"
- fail: msg="not enough RAM"t
- when: {{ ansible_memory_mb.real.total }} < 4096
Run Code Online (Sandbox Code Playgroud)
你能告诉我它是如何工作的吗?也许有更好的方法来使用Ansible做我想做的事情?
谢谢您的回答。
I have the following problem:
A host_var defining my nginx sites (excerpt):
nginx_sites:
- server:
name: site1
location1:
config:
name: "/"
[...]
- server:
name: site2
location1:
config:
name: "/"
[...]
location2:
config:
name: "/secretspace"
[...]
htaccess:
username:
password: somepassword
Run Code Online (Sandbox Code Playgroud)
In this example I have 2 sites. The second one has two locations where the second one has a subelement named "htaccess". This is what I want to use in order to create an corresponding htaccess file.
I tried this …
我试图检查服务是否正在运行,然后将其输出注册到某个变量,如果它没有运行,则启动服务。下面是我的 Ansible 剧本片段。
- hosts: localhost
tasks:
- name: check if service is running
shell: pgrep node
register: pgrep
- name: stop running service
shell: pkill node
when: pgrep.stdout_lines != ''
tags:
- stop
- name: start running service
shell: pkill node
when: pgrep.stdout_lines == ''
tags:
- start
Run Code Online (Sandbox Code Playgroud)
现在在上述情况下,如果进程未运行,则该pgrep node
命令将退出状态代码返回为 1,这将使“检查服务是否正在运行”任务失败并中止进一步执行任务。我知道通过设置ignore_errors: true
将忽略错误并继续进行,但它无法运行 Ansible。有没有办法可以优雅地处理这个问题?