I need to deploy TICK.
How do you use variables in kapacitor.conf?
EX: username = "{{ admin }}"
I have a kapacitor.conf with variables to replace, and I have a file default.yml with variables.
Kapacitor.conf
username = "{{ admin }}"
password = "{{ admin_password }}"
default.yml
---
admin: admin
admin_password: admin
An option would be to use lineinfile. Given the variables
> cat default.yml
username: admin
password: admin_password
the playbook below
- hosts: localhost
vars_files:
- default.yml
tasks:
- lineinfile:
path: Kapacitor.conf
regexp: "^{{ item.key }}:"
line: "{{ item.key }}:{{ item.value }}"
create: yes
loop:
- {key: 'admin', value: "{{ username }}"}
- {key: 'admin_password', value: "{{ password }}"}
gives:
> cat Kapacitor.conf
admin:admin
admin_password:admin_password
next (for some first) option would be template.
Related
new to ansible ..
Trying to get this result:
variable tgt_wls_pwd = the result of env1.wls_pwd = 1234
variable tgt_apps_pwd = the result of env1.apps_pwd = 5678
The unencrypted password file vault.yml
env1:
wls_pwd: 1234
apps_pwd: 5678
Playbook
ansible-playbook tgt-app-stop.yml --extra-vars="target_clone=env1"
- name: Stop Application Tier(s) process
hosts: " {{ target_clone }}-app01"
any_errors_fatal: true
remote_user: ansible
become: yes
become_user: install
roles:
- oraapp-stop
vars_files:
- vault.yml
tasks:
- set_fact:
target_clone: "{{ target_clone }}"
vars:
# tgt_wls_pwd: "{{ target_clone }}||{{ wls_pwd }}"
# tgt_apps_pwd: "{{ target_clone }}||{{ apps_pwd }}"
# tgt_wls_pwd: "{{ target_clone ['{{ wls_pwd }}'] }}"
# tgt_apps_pwd: "{{ target_clone ['{{ apps_pwd }}'] }}"
tgt_wls_pwd: "{{ target_clone.wls_pwd }}"
tgt_apps_pwd: "{{ target_clone.apps_pwd }}"
I've tried quite a few permutations
target_clone is an extra variable passed to the playbook when running.
Thanks.
You'll need the vars lookup plugin. See
shell> ansible-doc -t lookup vars
For example, given the file
shell> cat vault.yml
env1:
wls_pwd: 1234
apps_pwd: 5678
and the inventory
shell> cat hosts
env1-app01
The playbook
shell> cat tgt-app-stop.yml
- hosts: "{{ target_clone }}-app01"
gather_facts: false
vars_files:
- vault.yml
vars:
tgt_wls_pwd: "{{ lookup('vars', target_clone).wls_pwd }}"
tgt_apps_pwd: "{{ lookup('vars', target_clone).apps_pwd }}"
tasks:
- debug:
msg: |
tgt_wls_pwd: {{ tgt_wls_pwd }}
tgt_apps_pwd: {{ tgt_apps_pwd }}
gives
shell> ansible-playbook tgt-app-stop.yml -e "target_clone=env1"
PLAY [env1-app01] ****************************************************************************
TASK [debug] *********************************************************************************
ok: [env1-app01] =>
msg: |-
tgt_wls_pwd: 1234
tgt_apps_pwd: 5678
PLAY RECAP ***********************************************************************************
env1-app01: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Need some help to retrieve child values from var file.
Below is my var file
api_user: root
api_password: !vault |
$ANSIBLE_VAULT;1.2;AES256;isi33835326238346338613761366531376662613865376263656262
6138
Huston:
onefs_host: 10.88.55.00
Phoenix:
onefs_host: 10.76.52.01
Below is my playbook
---
- name: isi_increase
hosts: localhost
connection: local
vars_files:
- isilonvars.yml
tasks:
- name: Print
debug:
msg:
- "{{ Huston.onefs_host }}"
- "{{ api_user }}"
- "{{ api_password }}"
This code works perfectly
TASK [Print] ********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
"10.88.55.00",
"root",
"Sh00tm3!"
]
}
But as per my requirement i have to retrieve onefs_host IP as per location in my playbook. I am using extra vars here -e "location=Huston"
- name: Print
debug:
msg:
# - "{{ wcc.onefs_host }}"
- "{{ {{location}}.onefs_host }}"
- "{{ api_user }}"
- "{{ api_password }}"
I am getting the below error.
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected token ':', got '}'. String: {{ {{isilon_location}}.onefs_host }}"}
Can you try this way
- name: Print
debug:
msg:
- "{{ vars[location]['onefs_host'] }}"
- "{{ api_user }}"
- "{{ api_password }}"
my ansible tasks/main.yml looks like this atm:
---
- name: Create Directories
file:
path: "{{ item.path }}"
recurse: yes
owner: "{{ item.owner | default(\"nobody\") }}"
group: "{{ item.group | default(\"nogroup\") }}"
state: directory
with_items:
- "{{ app_dirs }}"
- name: Move Templates
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ item.owner | default(\"nobody\") }}"
group: "{{ item.group | default(\"nogroup\") }}"
with_items: "{{ app_templates }}"
- name: Move Dependencies
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ item.owner | default(\"nobody\") }}"
group: "{{ item.group | default(\"nogroup\") }}"
with_items: "{{ app_dependencies }}"
I have to move a couple of dependencies and templates to the remote machine. Now I need to add some conditionals or tags so we can decide if we want to copy all depedencies or just a few of them. I tried it like this:
- name: Create Directories
file:
path: "{{ item.path }}"
recurse: yes
owner: "{{ item.owner | default(\"nobody\") }}"
group: "{{ item.group | default(\"nogroup\") }}"
state: directory
tags: "{{ item.tag }}"
with_items:
- "{{ app_dirs }}"
But Ansible throws this error: ERROR! 'item' is undefined. Probably the tags option has to be inside the "file" block so with_items knows where to item.tag from. But the tags option has to be intended like that. Is there a way to use variables for the tags options? I actually liked the structure of this tasks/main.yml, it was easier to handle all variables in one place. My other idea was to create blocks with when conditions like this:
tasks:
- name: Install app1
block:
- name: Create Directory
file:
....
- name: Move Templates
template:
....
- name: Move Dependencies
copy:
....
tags:
- app1
- name: Install app2
block: .....
With this approach I can't store all variables as a dictonary like before. If there is no other way I'll use the block approach but I hope that someone has a better idea. Thanks in advance!
I'm running a playbook against a host and getting this error: ` "msg": "Unable to connect to vCenter or ESXi API at 192.11.11.111 on TCP/443: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)"
We are using vcenter 6.5. I have a playbook that should let ansible controller talk to the vsphere vcenter. I exported the trusted root SSL certificates from the vsphere home page. Copied over to my ansible controller and installed with:
sudo mv 9dab0099.0.crt 9dab0099.r0.crl 11ec582d.0.crt /etc/pki/ca-trust/source/anchors
sudo update-ca-trust force -enable
sudo update-ca-trust extract
able to telnet to the host on 443
able to ping host continously
tried changing validate_certs to: no
tried changing validate_certs to: yes
tried changing validate_certs to: false
My playbook:
- name: Add an additional cpu to virtual machine server
hosts: '{{ target }}'
tasks:
- name: Login into vCenter and get cookies
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: "{{ vm_folder }}"
cluster: "{{ vcenter_cluster }}"
datacenter: "{{ vcenter_datacenter }}"
name: "{{ vm_name }}"
- name:
uri:
url: https://{{ vcenter_hostname }} #/rest/com/vmware/cis/session
force_basic_auth: yes
validate_certs: no
method: POST
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: "{{ vm_folder }}"
cluster: "{{ vcenter_cluster }}"
datacenter: "{{ vcenter_datacenter }}"
name: "{{ vm_name }}"
#register: login
- name: Stop virtual machine
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
folder: "{{ vm_folder }}"
cluster: "{{ vcenter_cluster }}"
datacenter: "{{ vcenter_datacenter }}"
name: "{{ vm_name }}"
state: "poweredoff"
- name: reconfigure CPU and RAM of VM
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
cluster: "{{ vcenter_cluster }}"
datacenter: "{{ vcenter_datacenter }}"
name: "{{ vm_name }}"
state: "present"
validate_certs: "false"
folder: "{{ vm_folder }}"
hardware:
memory_gb: "{{ memory }}"
num_cpus: "{{ cpu }}"
scsi: "lsilogic"
ESXi fw rules are open.
Reproduced error with python 2.7.5 and python 3.6. Newest version of pyvmomi installed.
Can someone point me in the right direction from here?
Try to put
validate_certs: no
into the tasks
I'm trying to use the same set of variables for the various modules in my play (with some slight variations as you will see).
It seemed logical to include them as 'vars' at the top of my play, but i then have trouble referring to them later on. So far i've done this :
- name: destruction instance sur GCP
hosts: localhost
gather_facts: no
vars:
gcp_project: ansible-test-248409
gcp_cred_kind: serviceaccount
gcp_cred_file: /google/service-accounts/ansible-test-248409-fbadc808948d.json
zone: europe-west1-b
region: europe-west1
machine_type: n1-standard-1
machines:
- webserver-1
- webserver-2
- webserver-3
- devops-1
- devops-2
tasks:
- name: destruction des machines
gcp_compute_instance:
name: "{{ machines }}"
state: absent
zone: "{{ zone }}"
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
- name: destruction des disques
gcp_compute_disk:
name: "{{ machines }}-disk"
state: absent
zone: "{{ zone }}"
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
Which gives me this error message
[WARNING]: The value ['webserver-1', 'webserver-2', 'webserver-3', 'devops-1', 'devops-2'] (type list) in a string field was
converted to u"['webserver-1', 'webserver-2', 'webserver-3', 'devops-1', 'devops-2']" (type string). If this does not look like what
you expect, quote the entire value to ensure it does not change.
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Invalid JSON response with error: <HTML>\n<HEAD>\n<TITLE>Bad Request</TITLE
>\n</HEAD>\n<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n<H1>Bad Request</H1>\n<H2>Error 400</H2>\n</BODY>\n</HTML>\n"}
Using 'lookup' or 'query' doesn't work either. Can anyone see what i'm missing ?
you use with_items: option.
tasks:
- name: destruction des machines
gcp_compute_instance:
name: "{{ item }}"
state: absent
zone: "{{ zone }}"
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
scopes:
- https://www.googleapis.com/auth/compute
with_items: "{{ machines }}"