Ansible playbook syntax error with tasks: - ansible

I just started experimenting with ansible and I am trying to write my first simple playbook.
But I am getting a syntax error with the task keywork,
---
name: add ansible user
hosts: all
become: true
become_method: sudo
become_user:root
tasks:
- user:
name: ansible
groups: ansible
When I run this get the following:
utility:~/scripts/ansible# ansible-playbook --check add-ansible-user.yml
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/root/scripts/ansible/add-ansible-user.yml': line 8, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
^ here
From searching I belive the best bet is that I have an indent problem, but nomatter how I have tried to change it up, I cant get it too work.

- name: add ansible user
hosts: all
become: true
become_method: sudo
become_user: root
tasks:
- user:
name: ansible
groups: ansible
It's indeed the indentation problem. Please try the code written above.

Facing same issue, by making correct Indent spacing able to resolve it
As ref below
---
- name: my ansible
hosts: webserver
remote_user: root
become: true
tasks:
- name: intall httpd
yum:
name: httpd
state: latest
- name: run httpd
service:
name: httpd
state: started
- name: create content
copy:
content: “Congratulation on installing ansible”
dest: /var/www/html/index.html

The problem is here:
become_user:root
You need a space between : and root
become_user: root

Related

Error! conflicting action statements: project_path, state (ansible)

Trying to execute this code but having the said errors encountered above
code
---
- hosts: localhost
connection: local
collections:
- community.general.terraform
tasks:
- name: Execute Terraform Template
project_path: '/Users/<username>/Desktop/<repository>/<file>'
state: present
force_init: true
The offending line appears to be:
tasks:
- name: Execute Terraform Template
^ here
been trying to figure this one out.. I am using a macOS, installed Ansible locally already.
Thank you in advance!!
Trying to execute the code above but unable to succeed.
You have an indentation problem in your script.
Should work like this:
- name: Run Terraform
gather_facts: No
hosts: localhost
tasks:
- name: Execute Terraform Template
terraform:
project_path: '/Users/<username>/Desktop/<repository>/<file>'
state: present
force_init: true

not found what is the error actually means. Error: "(<unknown>): mapping values are not allowed in this context at line 3 column 16 "

- hosts: local_host
remote_user: ansible
become: yes
become_method: sudo
connection: ssh
gather_fact: yes
tasks:
name: installing MariaDB
yum:
name: mariadb-server
state: latest
notify: startservice
handlers:
name: startservice
service:
name: mariadb
state: restarted
The error is in the first two lines:
- hosts: local_host
remote_user: ansible
host cannot have both a scalar value (local_host) and a mapping value (starting at remote_user:). Chances are that you want remote_user to be on the level of hosts, making it a sibling key:
- host: local_host
remote_user: ansible
# and so on

Ansible - How to run multiple tasks in a playbook

I'm new to ansible, and am attempting to run multiple tasks to install docker on a particular host group in an ansible playbook.
I have the following playbook...
---
- hosts: all
tasks:
- name: Update and upgrade apt packages
become: yes
apt:
upgrade: yes
update_cache: yes
cache_valid_time: 86400 #One day
- hosts: loadbalancer
become: yes
tasks:
- name: Install docker packages
apt:
name:
- 'apt-transport-https'
- 'ca-certificates'
- 'curl'
- 'software-properties-common'
state: present
- name: Add Docker official GPG key
apt-key:
url: https://download.docker.com/linux/ubuntu/gpg
This is the error I get when attempting to run the playbook...
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to be in '/home/vagrant/ansible/playbooks/hostname.yml': line 23, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Add Docker official GPG key
^ here
What am I doing wrong here?
Thanks,
It is not apt-key, it is apt_key. Please update and try again

Ansible: editing a config file as sudo (CoreOS)

I'm an ansible newbie.
I'm using ansible 2.3.0.0
I have the playbook below to bootstrap nodes for a k8s cluster in openstack:
- name: bootstrap
hosts: coreos
become_user: root
become_method: su
gather_facts: False
roles:
- defunctzombie.coreos-bootstrap
tasks:
- lineinfile:
path: /etc/coreos/update.conf
state: present
regexp: '^REBOOT_STRATEGY'
line: 'REBOOT_STRATEGY=off'
I want to turn off auto-reboots on coreos because our openstack installation has a problem with reboots not coming back up properly and having coreos reboot often is causing instance to have to be manually shut down and restarted.
Anyway, the playbook above doesn't work. I get this error:
"The destination directory (/etc/coreos) is not writable by the current user. Error was: [Errno 13] Permission denied: '/etc/coreos/.ansible_tmppQCJrCupdate.conf'"
So my syntax is wrong (I've tried a few different combinations with no luck).
Could someone point me in the right direction? And feel free to make a suggestion on anything about this playbook.
Thanks!
Instead of execute playbook as root user, use different user with sudo access.
Please try this:
- name: bootstrap
hosts: coreos
user: <user_name>
become_method: sudo
gather_facts: False
roles:
- defunctzombie.coreos-bootstrap
tasks:
- lineinfile:
path: /etc/coreos/update.conf
state: present
regexp: '^REBOOT_STRATEGY'
line: 'REBOOT_STRATEGY=off'
Replace <user_name> with your user.
Run your playbook as ansible-playbook <playbook_name> --ask-sudo-pass

how to define login user and become root in playbook

my loginuser is user1 and i want to execute the playbook with root. how can i do this. if i use in cmdline it does not work like this
ansible-playbook main.yaml -i hosts --user=git -k --become-user=root --ask-become-pass --become-method=su
Please tell me how to implement this.
name: Install and Configure IEM
hosts: rhel
ansible_become: yes
ansible_become_method: su
ansible_become_user: root
ansible_become_pass: passw0rd
tasks:
- name: Creating masthead file path
file: path=/etc/opt/BESClient state=directory
- name: Creating install directory
I use :
deploy.yml
- name: Todo something
hosts: all
become: yes
become_user: root
become_method: su
When you execute the playbook pass the password as an extra var.
--extra-vars='ansible_become_pass=password'
From Ansible docs:
you can set those in the playbook as #Raul-Hugo, with become_user and become_user;
alternatively, it can also be done in the inventory, which allows setting per host or group. But then the variables get "ansible_" prefix: ansible_become_user, ansible_become_user, etc. That's why the playbook you gave in your question did not work: it used variable names that are used in the inventory.
You can become root like below and install the packages
tasks:
- name: install apache package
become: yes
become_user: root
yum:
name: httpd
state: present
- name: ensure apache is running
become: yes
become_user: root
service:
name: httpd
state: started
All the above answers caused Ansible to try to login as root from the beginning. but in this case, the user you request is git so the below example worked for me:
- name: Install and Configure IEM
hosts: rhel
tasks:
- name: Creating masthead file path
file: path=/etc/opt/BESClient state=directory
remote_user: git
become: yes # when not specifying `become_user` it's "root"
This will cause it to login as git and after the login - switch to root

Resources