I need to throw a user prompt at task level hence, I cannot use var_prompt. For this I have a tiny shell script as follow which is running read command to catch user response and I am calling it from command module but its not working.
cat question.sh
read -r -p "Are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
echo "do_something"
;;
*)
echo "do_something_else"
;;
esac
My playbook:
---
- hosts: localhost
tasks:
- name: "Do you want to proceed?"
command: bash question.sh
You still need to use var_prompt. Split your play into few, and place var_prompt in between.
Was:
- hosts: foo
tasks:
- name: task1
debug:
- name: asking for foo
debug:
- name: task2
debug:
Become:
- hosts: foo
tasks:
- name: task1
debug:
- hosts: foo
vars_prompt:
- name: foo
prompt: bar
tasks:
- name: task2
debug:
Related
- name: test
hosts: all
gather_facts: no
tasks:
#command 1
- name: ansible-test command 1
iosxr_command:
commands:
- show inventory
when: ansible_network_os == 'iosxr'
register: output
- debug:
var: output.stdout
- name: print command executed
hosts: 127.0.0.1
connection: local
command:
- echo sh inventory
register: output1
- debug:
var: output1.stdout
this is my playbook
ERROR! conflicting action statements: hosts, command
The error appears to be in '/root/container/playbook2.yaml': line 16, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: print command executed
^ here
I am encountering this error.
Please help me fix the issue.
Indents. You need to start a new play with a new set of hosts and a new task list.
- name: test
hosts: all
gather_facts: no
tasks:
#command 1
- name: ansible-test command 1
iosxr_command:
commands:
- show inventory
when: ansible_network_os == 'iosxr'
register: output
- debug:
var: output.stdout
- name: print command executed
hosts: 127.0.0.1
connection: local
gather_facts: no
tasks:
- command: echo sh inventory
register: output1
- debug:
var: output1.stdout
I am having a play "partial_upgrade.yml" that has vars_prompt to prompt the user for input.
I am importing this playbook in another playbook "choose_play.yml" which imports based on condition.
Even though the "partial_upgrade.yml " is skipped it prompts for user input.
1. choose_play.yml
---
- hosts: localhost
vars_prompt:
- name: "option"
prompt: |
>>> 1. Partial Upgrade
>>> 2. Full Upgrade
>>> Enter the option which you want to run -
private: no
tasks:
- set_fact:
option: "{{ option }}"
- debug:
var: option
- import_playbook: partial_upgrade.yml
vars:
partial_upgrade: true
full_upgrade: false
when: hostvars["localhost"]["option"]|int == 1
- import_playbook: full-upgrade.yml
vars:
partial_upgrade: false
full_upgrade: true
when: hostvars["localhost"]["option"]|int == 2
2. deploy.yml
---
- hosts: nodes
gather_facts: false
sudo: true
vars_prompt:
- name: "server_ip"
prompt: "Enter Server IP"
private: no
- name: "server_path"
prompt: "Enter Server path"
private: no
roles:
- setup-master
- setup-worker
When I run the "choose_play.yml" and press 2 then it skips the "partial_upgrade.yml" but prompts for "Server IP" and "Server Path".
I don't want to enter the details for the skipped play.
Please help me to disable the vars_prompt when the play is skipped.
I have this playbook:
---
- name: Test
hosts: localhost
tasks:
- name: Ansible grep pattern with ignore_errors example
shell: "grep 'authorization' /tmp/junk.test"
register: grep_output
ignore_errors: true
- name: Output debug grep_output
debug:
var: grep_output
verbosity: 0
shell: "echo 'Hello!'"
when: grep_output.failed
When I run it I get this error:
ERROR! conflicting action statements: debug, shell
So I have to rewrite the playbook to look like this:
---
- name: Test
hosts: localhost
tasks:
- name: Ansible grep pattern with ignore_errors example
shell: "grep 'authorization' /tmp/junk.test"
register: grep_output
ignore_errors: true
- name: Output debug grep_output
debug:
var: grep_output
verbosity: 0
when: grep_output.failed
- name: Echo hello
shell: "echo 'Hello!'"
when: grep_output.failed
So I am repeating the when: grep_output.failed. Is there a better way of writing the above playbook?
Thanks
You can use the block statement. It allows you to group modules and use a single when statement for the entire block.
This should work:
---
- name: Test
hosts: localhost
tasks:
- name: Ansible grep pattern with ignore_errors example
shell: "grep 'authorization' /tmp/junk.test"
register: grep_output
ignore_errors: true
- block:
- name: Output debug grep_output
debug:
var: grep_output
verbosity: 0
- name: Echo hello
shell: "echo 'Hello!'"
when: grep_output.failed
I have a playbook which is repeating one task, shell is invoked from ansible task, but it does not exit successful on completion of task and repeating the shell instructions again.
Playbook:
---
hosts: dev-servers
tasks:
- name: clean up directoty
shell: rm -rf /opt/data/latest/*
- name: copy the file
copy: src=/home/data/metadata.zip dest=/opt/data
- name: Change directoty
shell: cd /opt/data
- name: copy the file
copy: src=/root/Run_Build.sh dest=/opt/deployment_scripts
- name: permission
shell: chmod +x /opt/deployment_scripts/Run_Build.sh
- name: deploy
command: sh /opt/deployment_scripts/Run_Build.sh
run_once: true
register: result
- debug: msg="{{ result.stdout }}"
- debug: msg="{{ result.stderr }}"
Run_Build.sh:
#!/bin/bash
Jboss_logs=/opt/jboss/jboss-eap-6.4/standalone/log/server.log
for i in {1..100}; do
sleep 10
if [ -f $Jboss_logs/server.log ]; then
if grep -e 'Deployed "deploy.war"' $Jboss_logs/server.log ; then
echo "Code successfully deployed"
exit 0
else
echo "*****************************************************************************"
echo " JBOSS is still loading deployment - please be patient .... Loading again in 10 sec"
echo "*****************************************************************************"
fi
else
echo "server log file has not been created yet, please wait for sometime."
fi
done
Given the following playbook:
---
- name: test local_action with_items
hosts: localhost
gather_facts: false
tasks:
- name: "add something to a file"
shell: echo '{{item}}' >> foo.txt
with_items:
- "aaaaa"
- "aaaaa"
# using 40 items
or
---
- name: test local_action with_items
hosts: localhost
gather_facts: false
tasks:
- name: "add something to a file"
shell: echo '{{item}}' >> foo.txt
with_sequence: count=40
The latter playbook run 5 seconds.
Using a bash loop is obviously much (1000 times) faster and takes 5 ms:
time for i in $(seq 1 40); do echo $i >> foo.txt; done
Now it is clear that Ansible has some overhead, but is there any possibility to speed this up?
Instead of shell module, use raw module. It will be as quick as the bash loop.
---
- name: test local_action with_items
hosts: localhost
gather_facts: false
tasks:
- name: "add something to a file"
raw: echo '{{item}}' >> foo.txt
with_sequence: count=40
...
Anyway, if you want performance, maybe write your code in C.