ansible-playbook

Case statement for setting var in Ansible/Jinja2

笑着哭i 提交于 2019-11-30 06:48:45
I'm using Ansible with Jinja2 templates, and this is a scenario that I can't find a solution for in Ansible's documentation or googling around for Jinja2 examples. Here's the logic that I want to achieve in Ansible: if {{ existing_ansible_var }} == "string1" new_ansible_var = "a" else if {{ existing_ansible_var }} == "string2" new_ansible_var = "b" <...> else new_ansible_var = "" I could probably do this by combining several techniques, the variable assignment from here: Set variable in jinja , the conditional comparison here: http://jinja.pocoo.org/docs/dev/templates/#if-expression , and the

Running an Ansible Playbook on a particular group of servers

百般思念 提交于 2019-11-30 06:05:49
I have the following /etc/ansible/hosts: [ESNodes] isk-vsrv643 isk-vsrv644 isk-vsrv645 [PerfSetup] isk-dsrv613 isk-dsrv614 I know there is an option to run a playbook on particular hosts with -l Is there a way to run a playbook only on the PerfSetup group? Same way as you would do for hosts : -l PerfSetup 来源: https://stackoverflow.com/questions/22129422/running-an-ansible-playbook-on-a-particular-group-of-servers

Ansible: Insert line if not exists

我怕爱的太早我们不能终老 提交于 2019-11-30 04:42:57
I'm trying insert a line in a property file using ansible. I want to add some property if it does not exist, but not replace it if such property already exists in the file. I add to my ansible role - name: add couchbase host to properties lineinfile: dest=/database.properties regexp="^couchbase.host" line="couchbase.host=127.0.0.1" But this replaces the property value back to 127.0.0.1 if it exists already in the file. What I'm doing wrong? udondan The lineinfile module does what it's supposed to: It ensures the line as defined in line is present in the file and the line is identified by your

One loop over multiple Ansible tasks

孤者浪人 提交于 2019-11-30 03:27:29
I've created an Ansible playbook that creates a cloud instance and then installs some programs on the instance. I want to run this playbook multiple times (without using a bash script). Is it possible to use a loop to loop over those two tasks together (I.E. One loop for two tasks?). All I've been able to find so far is one loop for each individual task No that's currently not possible. with_items used to work with the include statement in previous versions of Ansible but was unfortunately dropped. Though it will be brought back in Ansible 2.0, see slide 14/15 of What's New in v2 - AnsibleFest

Which is the best way to make config changes in conf files in ansible

回眸只為那壹抹淺笑 提交于 2019-11-30 03:10:56
问题 Initially I used a makefile to deploy my application in linux . I had various sed commands to replace variables like the PHP upload file size, post size, log file location etc. Now I am shifting to ansible. I know I can copy the files, but how can I make changes to the conf files? Like if i just want to change the upload_filesize = 50M parameter. I don't want to make copies of the whole conf file and then replace with my file. Sometimes it’s only a one-line change. Is there any better way to

Pass array in --extra-vars - Ansible

这一生的挚爱 提交于 2019-11-30 02:40:37
How can I pass yaml array to --extra-vars in Ansible playbook. Ansible documentation does not declares its syntax nor I can find that on any internet resource. I mean if I have a playbook: --- - hosts: {{hostName}} - remote_user: admin ... Then I should call my playbook like ansible-playbook DeployWar.yml --extra-vars="hostName=tomcat-webApp" But I want to run this playbook on two servers say tomcat-webApp and tomcat-all , and I want to control it from out side i.e. using --extra-vars . What I have tried to do is: ansible-playbook DeployWar.yml --extra-vars="hostName=[tomcat-webApp, tomcat-all

how to use include_vars in ansible

孤街醉人 提交于 2019-11-30 01:37:15
问题 I have created my own custom library, I added my custom library in the common folder of my repository. In that I need to pass variables dynamically. It's a confidential password, so I am using "vault" in ansible. In that my requirement is how to pass include_vars in the tasks\main.yml before hosts. e.g: mytasks.yml - include_vars: sample_vault.yml - include: sample_tasks.yml - hosts: localhost tasks: name: "free task" command: ls -a my directory structure like this: myfolder - common -library

Write variable to a file in Ansible

寵の児 提交于 2019-11-30 00:12:25
I am pulling JSON via the URI module and want to write the received content out to a file. I am able to get the content and output it to the debugger so I know the content has been received, but I do not know the best practice for writing files. You could use the copy module, with the content parameter: - copy: content="{{ your_json_feed }}" dest=/path/to/destination/file The docs here: copy module Unless you are writing very small files, you should probably use templates . Example: - name: copy upstart script template: src: myCompany-service.conf.j2 dest: "/etc/init/myCompany-service.conf"

How can I test jinja2 templates in ansible?

為{幸葍}努か 提交于 2019-11-29 20:20:40
Sometimes I need to test some jinja2 templates that I use in my ansible roles. What is the simplest way for doing this? For example, I have a template (test.j2): {% if users is defined and users %} {% for user in users %}{{ user }} {% endfor %} {% endif %} and vars (in group_vars/all): --- users: - Mike - Smith - Klara - Alex At this time exists 4 different variants: 1_Online (using https://cryptic-cliffs-32040.herokuapp.com/ ) Based on jinja2-live-parser code. 2_Interactive (using python and library jinja2, PyYaml) import yaml from jinja2 import Template >>> template = Template(""" ... {% if

Run Command Inside of Docker Container Using Ansible

你说的曾经没有我的故事 提交于 2019-11-29 20:10:19
what I'm trying to accomplish is to run commands inside of a Docker container that has already been created on a Digital Ocean Ubuntu/Docker Droplet using Ansible. Can't seem to find anything on this, or I'm majorly missing something. This is my Ansible task in my play book. I'm very new to Ansible so any advice or wisdom would be greatly appreciated. - name: Test Deploy hosts: [my-cluster-of-servers] tasks: - name: Go Into Docker Container And Run Multiple Commands docker: name: [container-name] image: [image-ive-created-container-with-on-server] state: present command: docker exec -it