在剧本中我得到了以下代码:
---
- hosts: db
vars:
postgresql_ext_install_contrib: yes
postgresql_pg_hba_passwd_hosts: ['10.129.181.241/32']
...
Run Code Online (Sandbox Code Playgroud)
我想postgresql_pg_hba_passwd_hosts用我所有的webservers私有ips替换值.我知道我能得到这样的价值观这样一个模板:
{% for host in groups['web'] %}
{{ hostvars[host]['ansible_eth1']['ipv4']['address'] }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
将此循环的结果分配给剧本中的变量的最简单/最简单的方法是什么?或者,有没有更好的方法来收集这些信息?我应该把这个循环放在模板中吗?
额外的挑战:我必须添加/32到每个条目.
我遇到了最愚蠢的问题.我无法弄清楚如何在Ansible 2.2任务文件中测试布尔值.
在vars/main.yml,我有:
destroy: false
Run Code Online (Sandbox Code Playgroud)
在剧本中,我有:
roles:
- {'role': 'vmdeploy','destroy': true}
Run Code Online (Sandbox Code Playgroud)
在任务文件中,我有以下内容:
- include: "create.yml"
when: "{{ destroy|bool }} == 'false'"
Run Code Online (Sandbox Code Playgroud)
我尝试了以下各种组合:
when: "{{ destroy|bool }} == false"
when: "{{ destroy|bool }} == 'false'"
when: "{{ destroy|bool == false}}"
when: "{{ destroy == false}}"
when: "{{ destroy == 'false'}}"
when: destroy|bool == false
when: destroy|bool == 'false'
when: not destroy|bool
Run Code Online (Sandbox Code Playgroud)
在以上所有情况下,我仍然得到:
statically included: .../vmdeploy/tasks/create.yml
Run Code Online (Sandbox Code Playgroud)
调试输出:
- debug:
msg: "{{ destroy }}"
---
ok: [atlcicd009] => {
"msg": true …Run Code Online (Sandbox Code Playgroud) 我目前正在编写一个遵循这种通用格式的Ansible游戏,并通过cron作业运行:
pre_tasks:
-Configuration / package installation
tasks:
-Work with installed packages
post_tasks:
-Cleanup / uninstall packages
Run Code Online (Sandbox Code Playgroud)
上面的问题是,有时部分中的命令tasks失败,并且当它执行时该post_tasks部分不运行,使系统处于凌乱状态.post_tasks即使发生故障或致命错误,是否可以强制命令运行?
我当前的方法是应用于ignore_errors: yes该tasks部分下的所有内容,然后when:对每个任务应用条件,以单独检查先前命令是否成功.
这个解决方案看起来像是一个黑客,但它变得更糟,因为即使使用ignore_errors: yesset,如果遇到任务的致命错误,整个游戏仍会立即失败,所以我还必须运行cron'd bash脚本来手动检查事物达到播放执行后.
我想要的只是保证,即使tasks失败,post_tasks仍然会运行.我确信有一种方法可以在不使用bash脚本包装器的情况下执行此操作.
我注意到Ansible使用分号删除临时脚本来分隔bash命令.
这是一个示例命令:
EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o
ControlPath="/Users/devuser/.ansible/cp/ansible-ssh-%h-%p-%r" -o
KbdInteractiveAuthentication=no -o
PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey
-o PasswordAuthentication=no -o ConnectTimeout=10 build /bin/sh -c
'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python
/home/ec2-user/.ansible/tmp/ansible-tmp-1430847489.81-75617096172775/docker;
rm -rf
/home/ec2-user/.ansible/tmp/ansible-tmp-1430847489.81-75617096172775/
>/dev/null 2>&1'
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉ansible用双符号替换分号或告诉它在运行ansible-playbook时保存脚本或输出内容?
我正在尝试调试此脚本中的错误,现在唯一出现的是:
failed: [build] => {"changed": false, "failed": true}
msg: ConnectionError(ProtocolError('Connection aborted.', error(2, 'No such file or directory')),)
Run Code Online (Sandbox Code Playgroud) 我在使用Ansible重启SSH守护进程时遇到了一些麻烦.
我正在使用截至2015年5月11日的最新软件(Ansible 1.9.1/Vagrant 1.7.2/VirtualBox 4.3.26/Host:OS X 10.10.1/Guest:ubuntu/trusty64)
tl; dr:我调用服务语法的方式似乎有问题.
剧本
- hosts: all
- remote_user: vagrant
- tasks:
...
- name: Forbid SSH root login
sudo: yes
lineinfile: dest=/etc/ssh/sshd_config regexp="^PermitRootLogin" line="permitRootLogin no" state=present
notify:
- restart ssh
...
- handlers:
- name: restart ssh
sudo: yes
service: name=ssh state=restarted
Run Code Online (Sandbox Code Playgroud)
产量
NOTIFIED: [restart ssh]
failed: [default] => {"failed": true}
FATAL: all hosts have already failed -- aborting
Run Code Online (Sandbox Code Playgroud)
nginx处理程序使用几乎相同的语法成功完成.
剧本
- name: Restart SSH server
sudo: yes …Run Code Online (Sandbox Code Playgroud) 在我的剧本中,我引用了很多用户名(仅限其"ubuntu").
是否有内置的方式来说"从命令行传递的值中获取它"?
我知道我能做到
ansible-playbook <task> -u <user> -K --extra-vars "user=<user>"
Run Code Online (Sandbox Code Playgroud)
然后我可以{{user}}在剧本中使用,但定义用户两次感觉很奇怪.
我想使用ansible playbook执行下一个命令:
curl -X POST -d@mesos-consul.json -H "Content-Type: application/json" http://marathon.service.consul:8080/v2/apps
Run Code Online (Sandbox Code Playgroud)
我该怎么办呢?
如果我跑:
- name: post to consul
uri:
url: http://marathon.service.consul:8080/v2/apps/
method: POST
body: "{{ lookup('file','mesos-consul.json') }}"
body_format: json
HEADER_Content-Type: "application/json"
Run Code Online (Sandbox Code Playgroud)
我有下一个失败:
fatal: [172.16.8.231]: FAILED! => {"failed": true, "msg": "ERROR! the file_name '/home/ikerlan/Ik4-Data-Platform/ansible/playbooks/Z_PONER_EN_MARCHA/dns-consul/mesos-consul.j2' does not exist, or is not readable"}
我想/etc/nginx/sites-enabled用符号链接代替我的仓库.我正在尝试使用file模块执行此操作,但这不起作用,因为文件模块不会使用force选项删除目录.
- name: setup nginx sites-available symlink
file: path=/etc/nginx/sites-available src=/repo/etc/nginx/sites-available state=link force=yes
notify: restart nginx
Run Code Online (Sandbox Code Playgroud)
我可以回到使用shell.
- name: setup nginx sites-available symlink
shell: test -d /etc/nginx/sites-available && rm -r /etc/nginx/sites-available && ln -sT /repo/etc/nginx/sites-available /etc/nginx/sites-available
notify: restart nginx
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这一点,而不是回到shell?
在这个用于Ansbile的mysql数据库模块列表中,有一个用于创建数据库或创建用户等.
我想针对预先存在的表运行查询,并使用该查询的结果填充Ansible变量(IP地址列表和节点类型),我将根据节点类型运行不同的任务.
怎么能在Ansible中完成?
我总是想知道shell使用"ansible方式"(用get_url等等)替换以下任务的好方法是什么:
- name: Install oh-my-zsh
shell: wget -qO - https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash -
Run Code Online (Sandbox Code Playgroud)
要么
- name: Install nodesource repo
shell: curl -sL https://deb.nodesource.com/setup_5.x | bash -
Run Code Online (Sandbox Code Playgroud) ansible ×10
ansible-playbook ×10
ansible-2.x ×2
bash ×1
boolean ×1
curl ×1
mysql ×1
sshd ×1
wget ×1