I'm trying to build a playbook with rolling update use case (Sequential Execution) with the serial element. Since I have to use the serial value in multiple places in the playbook, I wanted to use it as a variable which can be used to define somewhere as a group variable.
Inventory file
[all]
webserver1 ansible_host=10.1.1.1 ansible_user=root
webserver2 ansible_host=10.1.1.2 ansible_user=root
webserver3 ansible_host=10.1.1.3 ansible_user=root
dbserver1 ansible_host=10.1.2.1 ansible_user=root
[webserver]
webserver1
webserver2
webserver3
[dbserver]
dbserver1
[app-stack:children]
webservers
dbservers
[app-stack:vars]
app_stack_serial_value=1
Playbook
- hosts: app-stack
serial: "{{ app_stack_serial_value }}"
tasks:
- name: debug
debug:
msg: "Ansible Host - {{ ansible_host }}"
Error
ERROR! The field 'serial' has an invalid value, which includes an undefined variable. The error was: 'app_stack_serial_value' is undefined
This will work if I specify the value in the serial field like serial: 1. But I wanted to use this value as a variable. Any Idea ??
Ansible Version: ansible 2.7.10
python version = 3.7.2 (default, Jan 13 2019, 12:50:01) [Clang 10.0.0 (clang-1000.11.45.5)]
As far as I know, you cannot set it in the group variables. But you can try set ENV SERIAL_NUMBER=1 before run this playbook and set the serial parameter like:
serial: "{{lookup('env','SERIAL_NUMBER')|default(3, true)}}"
Related
i've encountered strange problem with env var.
Here's my profiles.yml file
project:
outputs:
prod:
type: postgres
threads: 4
host: my_host
port: 5432
user: "{{ env_var('DBT_USER') }}"
pass: "{{ env_var('DBT_PASS') }}"
dbname: my_db
schema: my_schema
target: prod
Here's my env variables:
echo $DBT_USER;
my_user
echo $DBT_PASS;
my_password
dbt-debug output:
connection to server at "my_host", port 5432 failed: FATAL: password authentication failed for user "my_user"
But if I change
pass: "{{ env_var('DBT_PASS') }}"
to
my_password
in profiles.yml, dbt-debug shows no error.
dbt version is 1.0.1.
What am i doing wrong?
#dcpt The way you used the env_var function looks correct per the env_var docs but there's two things I can think of.
Are you restarting your shell after setting your env variables? Maybe there's something about the CLI session that needs to be renewed after a profiles.yml or env_var change.
I think that you need to layout your profiles.yml with target as a higher header? Looking at the example here on profiles.yml it looks like:
profiles.yml
<profile-name>:
target: <target-name>
outputs:
<target-name>:
type: <bigquery | postgres | redshift | snowflake | other>
schema: <schema_identifier>
threads: <natural_number>
But you have your target: at the bottom? Is that supposed to be an additional target definition or should it be higher above your prod: tag.
I have wired problem with running ansible
I'd like to print to log or anywhere else the extra-vars I'm executing the playbook with.
I am running the following command:
ansible-playbook /foo/main.yml --extra-vars "main_playbook=app_install start_path=work" --extra-vars {"db_config":{"db_multi_config":["value1","value2"]}}'
with the following main.yml playbook
- name: start playbook
import_playbook: "{{ playbook_dir }}/{{ main_playbook}}/{{ start_path}}.yml"
I'd like to print the values of all extra-vars passed on the command line before it gets to main.yml. Is there any way to do this?
You can get this information in the output running ansible in full debug mode:
Given the following test.yml playbook:
- hosts: localhost
gather_facts: false
and running it with the command:
ansible-playbook test.yml -vvvv -e toto=bla -e '{"test1":2}'
I get the following result (see the debug lines for playbook before play starts)
ansible-playbook [core 2.11.3]
config file = None
configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible-playbook
python version = 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0]
jinja version = 2.11.3
libyaml = True
No config file found; using defaults
The vault password file /home/user/bin/vault-keyring-client is a client script.
Executing vault password client script: /home/user/bin/vault-keyring-client --vault-id avaultid1
The vault password file /home/user/bin/vault-keyring-client is a client script.
Executing vault password client script: /home/user/bin/vault-keyring-client --vault-id avaultid2
The vault password file /home/user/bin/vault-keyring-client is a client script.
Executing vault password client script: /home/user/bin/vault-keyring-client --vault-id avaultid3
setting up inventory plugins
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Parsed /etc/ansible/hosts inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/local/lib/python3.8/dist-packages/ansible/plugins/callback/default.py
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
PLAYBOOK: test.yml **********************************************************************************************************************
Positional arguments: test.yml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/etc/ansible/hosts',)
extra_vars: ('toto=bla', '{"test1":2}')
forks: 5
1 plays in test.yml
PLAY [all] ******************************************************************************************************************************
skipping: no hosts matched
PLAY RECAP ******************************************************************************************************************************
You can't separate extra vars from all other vars, because they are merged. But you can see all variables for the host. (except for setup facts, which you can browse separately with -m setup).
The trick is to print all variables from hostvars group for 'itself', ansible_host:
ansible -i inventory -e extra_vars_here -m debug -a 'msg={{hostvars[inventory_hostname]}}' all
(replace all with specific hostname if you wish).
If you are overwhelmed, you can filter it with .keys(): hostvars[inventory_hostname].keys().
Concerning: Ansible-Playbooks
Is it possible to run a command on the remote machine and store the resulting output into a variable?
I am trying to get the kernel version and install the matching headers like this:
---
- hosts: all
tasks:
- name: install headers
become: yes
become_method: sudo
apt:
name: "{{ packages }}"
vars:
packages:
- linux-headers-`uname -r`
#- a lot of other packages here
Unfortunately uname -r is not executed here.
I am aware of this question: Ansible: Store command's stdout in new variable?
But it looks like this is another topic.
By definition:
Ansible facts are data related to your remote systems, including
operating systems, IP addresses, attached filesystems, and more.
In this link you can see all ansible facts that you can use.
https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html
One of those variables is ansible_kernel, this is the version of the kernel of your remote system. By default ansible gets this variables, but if you want to be sure that ansible will get this variables you have to use gather_facts: yes.
---
- hosts: all
gather_facts: yes
tasks:
- name: install
become: yes
become_method: sudo
apt:
name: "{{ packages }}"
vars:
packages:
- linux-headers-{{ ansible_kernel }}
I found a solution but I am not sure if this is really elegant
---
- hosts: all
tasks:
- name: Getting Kernel Version
command: "uname -r"
register: kernel_version
- name: install
become: yes
become_method: sudo
apt:
name: "{{ packages }}"
vars:
packages:
- linux-headers-{{ kernel_version.stdout }}
I want to filter ec2 instances according to the Environment tag which I define when I run the scripts, i.e ansible-playbook start.yml -e env=dev
However, it seems that the plugin is not parsing variables. Any idea on how to achieve this task?
my aws_ec2.yml:
---
plugin: aws_ec2
regions:
- eu-central-1
filters:
tag:Secure: 'yes'
tag:Environment: "{{ env }}"
hostnames:
- private-ip-address
strict: False
groups:
keyed_groups:
- key: tags.Function
separator: ''
Edit
There is no error message resulting when running the playbook. The only problem that ansible handle the variable exactly as a string tag:Environment: "{{ env }}"instead of value tag:Environment: dev
ansible 2.1.2.0
I have a situation here for which I'm trying to check with you all to see if the solution is even possible with Ansible vars_prompt feature and when conditional.
Ansible has this feature called: vars_prompt and I want to club this with the when conditional (when: ... which we generally use in tasks at task or playbook level actions)
PS: My question is different than this post: Ansible to Conditionally Prompt for a Variable? i.e. my vars: variables are not dependent upon any machine's OS ENV variables, in my case, I just have a simple hardcoded value for vars: variables. I'm also running the when condition at the main playbook level (and not at the task's level).
What I'm trying to do is to prompt a user for entering a variable value, only if at command line, the user has called the ansible-playbook install_tool.yml ... ... with --extra-vars "install_tool=true"
In workspace/ansible/install_tool.yml I have:
- hosts: all
become: true
gather_facts: false
vars:
instance: test
#install_tool: false
company_api_url: DUMMY_INVALID_URL
vars_prompt:
- name: "company_api_url"
prompt: "Enter Company API URL: "
default: "https://{{ instance }}.company.com"
private: no
when: install_tool == "true" and company_api_url == "DUMMY_INVALID_URL"
# or
#when: ( "{{ install_tool }}" == "true" and "{{ company_api_url }}" == "DUMMY_INVALID_URL" )
pre_tasks:
- debug: msg="install_tool = {{ install_tool }} and instance = {{ instance }}"
when: install_tool == "true"
My question:
How can I get the above conditional prompt working (with a valid substituted default value for a prompt variable) and also SKIP to prompt a user altogether depending upon the value of install_tool variable?
My understanding:
In top level playbook file (install_tool.yml), I'm defining a variable instance with value as test. Why the default value for the vars_prompt section did NOT pick test and substituted its value when it's defined at the playbook level (--OR what if a user is passing instance variable at command line for ex: --extra-vars "instance=qa"? In that case, I would want the default value as [https://qa.company.com], how can I do that)? I don't see any errors while running it for any syntax.
I'm getting the following output (but I was expecting a substituted URL value like: [https://test.company.com]) and it's still prompting even when install_tool was set or passed at command line as false:
Running:
$ ansible-playbook -i "localhost," --connection=local install_tool.yml --extra-vars "install_tool=false instance=qa"
or
$ ansible-playbook -i inventory -l localhost install_tool.yml --extra-vars "install_tool=false"
...
.....some lines here
...
PLAYBOOK: install_tool.yml *******************************************
1 plays in install_tool.yml
Enter Company API URL: [https://{{ instance }}.company.com]:
As using when: ( "{{ install_tool }}" == "true" and ...) condition within vars_prompt section, under/for - name: company_api_url variable, it didn't error out for any syntax issues or etc (that means, Ansible doesn't think it's wrong), then WHY, Ansible is still prompting me to enter the above vars_prompts variable, even when I'm passing --extra-vars "install_tool=false" --OR-- if I uncomment the line: install_tool: false under vars: section?
PS: when: ... condition is successfully working for the pre_tasks section i.e. when install_tool is true, it shows the value of those 2 variables and when install_tool is set to false, then it does NOT run the - debug step and skips it (which is expected).
Thanks.
You can check all supported attributes for prompted vars here.
when statement is not supported for prompted vars. And never was.
Templated default values for prompted vars is not supported also.
The only way to prevent vars prompt is to pass the variable in question via extra_vars. In your example, if you pass -e company_api_url=my_custom_url, Ansible will skip prompting for it.