ansible ERROR! conflicting action statements: hosts, command while running - ansible

- 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

Related

Registering a result when calling a role/script inside a playbook

I am working with ansible in Linux and have this playbook, which calls a role which passes the script name to run with an argument:
- hosts: localhost
gather_facts: no
ignore_errors: True
roles:
- { role: execute-my-script,
myscript_script_name: script.py,
myscript_args: "{{ my_var }}" }
- hosts: localhost
tasks:
- name: Status
ansible.builtin.debug:
msg: "WARNING: THERE IS AN ERROR!!!"
when:
- result is defined
- result.stdout is search("This resulted in error")
This is working when I run this playbook, and it will execute the script.py with arguments my_var.
This is my main.yml (execute-my-script):
---
- name: Execute script
ansible.builtin.command: "script.sh
{{ argument }}"
register: result
tags: execute-my-script
failed_when: "'This resulted in error' in result.stderr"
- set_fact: {"result": "{{ result }}"}
Note: This resulted in error comes from the script.sh.
I want to register the result of this task and then add another task which will do something if the result.stdout has this : "This resulted in error".
Although, I cannot register the result using register: result.
So I registered result inside main.yml task. But when I try to display an output in the playbook its not working.
Any ideas how to register the result of the given task in the playbook?

Deactivate the Current Ansible User with Ansible

In setting up a new Raspberry Pi with Ansible, I would like to perform the following actions:
Using the default pi user, create a new user named my_new_admin
Using the new my_new_admin user, deactivate the default pi user
Continue executing the playbook as my_new_admin
I am finding this difficult to achieve in a single playbook. Is it even possible to switch the active user like this in Ansible?
# inventory.yaml
---
all:
children:
rpis:
hosts:
myraspberrypi.example.com:
ansible_user: my_new_admin # or should `pi` go here?
...
# initialize.yaml
- hosts: rpis
remote_user: 'pi'
become: true
tasks:
- name: 'create new user'
user:
name: 'my_new_admin'
append: true
groups:
- 'sudo'
- name: 'add SSH key to my_new_admin'
*snip*
- name: 'lock default user'
remote_user: 'my_new_admin'
user:
name: 'pi'
expires: '{{ ("1970-01-02 00:00:00" | to_datetime).timestamp() | float }}'
password_lock: true
...
If you want to switch users, the easiest solution is to start another play. For example, the following playbook will run the first play as user pi and the second play as user root:
- hosts: pi
gather_facts: false
remote_user: pi
tasks:
- command: whoami
register: whoami
- debug:
msg: "{{ whoami.stdout }}"
- hosts: pi
gather_facts: false
remote_user: root
tasks:
- command: whoami
register: whoami
- debug:
msg: "{{ whoami.stdout }}"
In this playbook I'm being explicit about remote_user in both plays, but you could also set a user in your inventory and only override it when necessary. E.g., if I have:
pi ansible_host=raspberrypi.local ansible_user=root
Then I could rewrite the above playbook like this:
- hosts: pi
gather_facts: false
vars:
ansible_user: pi
tasks:
- command: whoami
register: whoami
- debug:
msg: "{{ whoami.stdout }}"
- hosts: pi
gather_facts: false
tasks:
- command: whoami
register: whoami
- debug:
msg: "{{ whoami.stdout }}"
Note that I'm setting the ansible_user variable here rather than using remote_user, because it looks as if ansible_user has precedence.

ansible ERROR! conflicting action statements: var, include_tasks

Following is my code:
- hosts: 11RRvEPG01
gather_facts: no
vars_files:
- /etc/ansible/epg/epg_command.yml
vars:
- output: []
tasks:
- name: run epg command
eric_eccli_command:
commands: "{{item}}"
with_items: "{{epg_command}}"
register: command_output
- name: show execution output
include_tasks:
file: /etc/ansible/epg_check_output.yml
var:
output: "{{item.stdout|last}}"
with_items: "{{command_output.results}}"
I got an error message when I ran it:
ERROR! conflicting action statements: var, include_tasks
Correct syntax is
- name: show execution output
include_tasks:
file: /etc/ansible/epg_check_output.yml
vars:
output: "{{ item.stdout|last }}"
with_items: "{{ command_output.results }}"
(change var for vars)

How to avoid repeating when conditionals in ansible?

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

ansible does not read in variables when connected to another host

I have a playbook which reads in a list of variables:
vars_files:
- vars/myvariables.yml
tasks:
- name: Debug Variable List
debug:
msg: "An item: {{item}}"
with_list: "{{ myvariables }}"
This prints out the list of "myvariables" from a file variables.yml, which contains:
---
myvariables:
- variable1
- variable2
I get the following as expected.
"msg": "An item: variable1"
"msg": "An item: variable2"
However, when I connect to another host, and run the same Debug statement, it throws an error:
vars_files:
- vars/myvariables.yml
tasks:
- name: Configure instance(s)
hosts: launched
become: True
remote_user: ubuntu
port: 22
gather_facts: False
tasks:
- name: Wait for SSH to come up
delegate_to: ***
remote_user: ubuntu
connection: ssh
register: item
- name: Debug Variable List
debug:
msg: "An item: {{item}}"
with_list: "{{ myvariables }}"
OUTPUT:
"msg": "'myvariables' is undefined"
How do I define the variables file when connecting to another host that is not localhost?
Any help on this would be greatly appreciated.
With "hosts: launched" you started new playbook. Put the vars_files: into the scope of this playbook (see below).
- name: Configure instance(s)
hosts: launched
become: True
remote_user: ubuntu
port: 22
gather_facts: False
vars_files:
- vars/myvariables.yml
tasks:
Review the Scoping variables.

Resources