I am following this page, Clone a private git repository with Ansible (using password prompt) to solve my requirement. Re-used the same template in my playbook main.yml whose contents are as
---
- name: move CentOS repo definitions outside temp
copy:
src: "{{ item }}"
dest: /etc/yum.repos.d/
owner: "root"
mode: 0600
with_fileglob:
- /etc/yum.repos.d/temp/*
become: true
- name: passing git credentials for cloning the repos
vars_prompt:
- name: "githubuser"
prompt: "Enter your github username"
private: no
- name: "githubpassword"
prompt: "Enter your github password"
private: yes
and some more below. Am facing an error
The error appears to have been in '/tmp/.../tasks/main.yml': line 12, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: passing git credentials for cloning the repos
^ here
The error appears to have been in '/tmp/.../tasks/main.yml': line 12, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: passing git credentials for cloning the repos
^ here
I validated the yml using the syntax check option available
ansible-playbook main.yml --syntax-check
and also on the YAML lint, but can't seem to find the reason why the error is seen.
You can't use vars_prompt at task level, only at playbook level.
If your main.yml is a part of role, you should move prompt block to upper level playbook that includes your role.
Related
Worked on another machine with Ansible 2.9.27. It is installed on a new machine ansible [core 2.12.2].I assume that the problem is the difference in versions.
[root#localhost ansible]# ansible-playbook test.yml
ERROR! couldn't resolve module/action 'synchronize'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/root/ansible/test.yml': line 4, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: Start copy scripts
^ here
Playbook contents.
---
- hosts: gate_test
tasks:
- name: Start copy scripts
synchronize:
src: ~/ansible/build/base_ca
dest: ~/
mode: push
The task of the playbook, transferring a directory with contents from a local machine to a virtual one
I use modules from the collection netapp.ontap in my ansible playbook which works perfectly fine when run using ansible-playbook command.
However, when run from AWX, it fails to detect the collection and immediately throws an errors that it cannot detect the ansible module/collection.
I even tried to re-install the collection from the playbook itself but with no luck.
The ansible collection is confirmed to be installed as it already works fine when run outside AWX.
The host is running ansible 2.10.4.
Here is my playbook:
---
- hosts: all
gather_facts: yes
collections:
- netapp.ontap
tasks:
- name: Install Netapp Collection from Ansible Galaxy
shell: ansible-galaxy collection install netapp.ontap
- name: Run Task
import_tasks: tasks/hil.yml
Task:
- name: 'Gather SVMs'
netapp.ontap.na_ontap_info:
state: info
hostname: "{{ netapp_hostname }}"
username: "{{ netapp_username }}"
password: "{{ netapp_hv_password }}"
gather_subset:
- vserver_info
Error from AWX:
SSH password:
Vault password:
ERROR! couldn't resolve module/action 'netapp.ontap.na_ontap_info'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/tmp/awx_421_gey54bdw/project/tasks/hil.yml': line 6, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: 'Gather SVMs'
^ here
This one looks easy to fix. It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote. For instance:
when: "ok" in result.stdout
Could be written as:
when: '"ok" in result.stdout'
Or equivalently:
when: "'ok' in result.stdout"
Update:
I created a collections/requirements.yml file, with the below details but now AWX fails the task itself.
collections/requirements.yml
collections:
name: https://github.com/ansible-collections/netapp.git
type: git
Error:
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 1279, in run self.pre_run_hook(self.instance, private_data_dir) File
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 1862, in pre_run_hook sync_task.run(local_project_sync.id) File
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 698, in _wrapped return f(self, *args, **kwargs) File
"/var/lib/awx/venv/awx/lib/python3.6/site-packages/awx/main/tasks.py",
line 1444, in run raise AwxTaskError.TaskError(self.instance, rc)
Exception: project_update 435 (failed) encountered an error (rc=2), please
see task stdout for details.
I am trying to take running config back up from two nexus switches, what am I missing from below
configuration?
- name: copy nexus switch running configurations
hosts: nxos-devices
gather_facts: no
ignore_errors: yes
tasks:
- name: timestamp
local_action: command date +%Y%m%d
register: timestamp
- name: get running configuration from nexus switch
nxos_config: running_config
register: running_config
- copy:
content: "{{ running_config.stdout[0] }}"
dest: ".config/{{ item.hostname }}_{{ timestamp.stdout }}.txt"
with_items:
- { hostname: bur1-mrt1 }
- { hostname: bur1-mrt2 }
Getting below error
[root#ansible-net-001 bkrishna]# ansible-playbook -i hosts running-config.yml
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, but
still be user configurable on deprecation. This feature will be removed in version 2.10. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
ERROR! this task 'nxos_config' has extra params, which is only allowed in the following modules: shell, win_shell, include_vars, add_host, raw, include_role, meta, set_fact, include, import_tasks, script, import_role, include_tasks, group_by, command, win_command
The error appears to be in '/root/ansible/bkrishna/running-config.yml': line 12, column 7, but may be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: get running configuration from nexus switch
^ here
I have a follow up question to the thread in here:
How do I pass username and password while using Ansible Git module?
I am trying to achieve a similar task where I am passing a username and password to GitHub through Ansible. Here is what I am using for my playbook:
- name: ANSIBLE - Shop Installation
hosts: host_list
vars_prompt:
- name: "githubuser"
prompt: "Enter your github username"
private: no
- name: "githubpassword"
prompt: "Enter your github password"
private: yes
- hosts: host_list
tasks:
- name: Get the latest version through Git
git:
repo: 'https://{{ githubuser }}:{{ githubpassword }}#github.com/foo/bar.git'
dest: /tmp
After running through this, I get the following error message:
fatal: []: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'githubuser' is undefined\n\nThe error appears to have been in 'playbook.yml'
Any ideas where I may have gone wrong here?
I am running Ansible 2.7.1
I think the structure of your playbook is wrong. Try this:
---
- hosts: host_list
vars_prompt:
- name: "githubuser"
prompt: "Enter your github username"
private: no
- name: "githubpassword"
prompt: "Enter your github password"
private: yes
tasks:
- name: Get the latest version through Git
git:
repo: 'https://{{ githubuser }}:{{ githubpassword }}#github.com/foo/bar.git'
dest: /tmp
Scope of vars_prompt is a playbook.
Variable githubuser is defined in the first play. Second play knows nothing about it.
Would I have to pass the variable value from the first play to the second? What's the correct way of doing that in this context?
An option would be to configure fact_caching. For example with Redis
[defaults]
fact_caching=redis
In the first play store the variables with set_fact cacheable: yes
In the second play fetch data from Redis
The following Ansible playbook for setting up a server for a Laravel app works fine:
---
- name: Set up a standard Laravel install
hosts: localhost
vars_prompt:
- name: "domain"
prompt: "Domain name"
private: no
- name: "dbname"
prompt: "Database name"
private: no
- name: "dbuser"
prompt: "Database username"
private: no
- name: "dbpassword"
prompt: "Database password"
private: yes
roles:
- create_droplet
- create_domain
- name: Install dependencies
hosts: launched
roles:
- upgrade
- utilities
- users
- nginx-php
- composer
- nginx_firewall
- redis
- postgres
- git
The following similar one for setting up a Wordpress install doesn't:
---
- name: Set up Wordpress with Apache, Memcached and Varnish
hosts: localhost
vars_prompt:
- name: "domain"
prompt: "Domain name"
private: no
- name: "title"
prompt: "Wordpress title"
private: no
- name: "email"
prompt: "Wordpress email"
private: no
- name: "user"
prompt: "Admin username"
private: no
- name: "pass"
prompt: "Admin password"
private: yes
roles:
- create_droplet
- create_domain
- name: Install dependencies
hosts: launched
roles:
- upgrade
- utilities
- users
- apache
- varnish
- memcached
- mysql
- wordpress
Both playbooks set up a new droplet on Digital Ocean using the create_droplet and create_domain roles, and add it to the launched group. However, the variables prompted for in the second playbook don't appear to be defined, as in this error message:
TASK [wordpress : Add user "wordpress", belonging to group "wordpress" and having a home dir of /var/www] ***
fatal: [<IP_ADDRESS_REDACTED>]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'pass' is undefined\n\nThe error appears to have been in '/home/matthew/Projects/ansible-setup/playbooks/roles/wordpress/tasks/main.yml': line 28, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Add user \"wordpress\", belonging to group \"wordpress\" and having a home dir of /var/www\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nunbalanced quotes. If starting a value with a quote, make sure the\nline ends with the same set of quotes. For instance this arbitrary\nexample:\n\n foo: \"bad\" \"wolf\"\n\nCould be written as:\n\n foo: '\"bad\" \"wolf\"'\n"}
Use of debug statements has confirmed that in none of the roles called in the second playbook does the domain variable appear to be defined. I'm not sure why that is. However, if I remove the part that creates the droplet and run it against an existing droplet, it seems to work OK.
Can anyone see why this is showing up as undefined? Is it something to do with the scope of these variables?
Is it something to do with the scope of these variables?
Yes, your variables are play-bound, so they are available for the first play (where you prompt them) and unavailable for the second one.
If you need variable to survive between plays, you need to convert it to host fact.
For example add post_tasks to your first play:
post_tasks:
- set_fact:
domain: '{{ domain }}'
delegate_to: '{{ item }}'
delegate_facts: true
with_inventory_hostnames: launched
This will add domain fact to every host in launched group.