Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Ansible playbook task with predefined username and password

Tags:

ansible

This is code of my ansible script .

---
- hosts: "{{ host }}"
  remote_user: "{{ user }}"
  ansible_become_pass: "{{ pass }}"
  tasks:
    - name: Creates directory to keep files on the server
      file: path=/home/{{ user }}/fabric_shell state=directory

    - name: Move sh file to remote
      copy:

        src: /home/pankaj/my_ansible_scripts/normal_script/installation/install.sh
        dest: /home/{{ user }}/fabric_shell/install.sh



    - name: Execute the script
      command: sh /home/{{ user }}/fabric_shell/install.sh
      become: yes

I am running the ansible playbook using command>>> ansible-playbook send_run_shell.yml --extra-vars "user=sakshi host=192.168.0.238 pass=Welcome01" .

But I don't know why am getting error

ERROR! 'ansible_become_pass' is not a valid attribute for a Play

The error appears to have been in '/home/pankaj/go/src/shell_code/send_run_shell.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- hosts: "{{ host }}"
  ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

Please guide , what I am doing wrong.

Thanks in advance ...

like image 795
Pankaj Cheema Avatar asked Sep 13 '25 19:09

Pankaj Cheema


1 Answers

ansible_become_pass is a connection parameter which you can set as variable:

---
- hosts: "{{ host }}"
  remote_user: "{{ user }}"
  vars:
    ansible_become_pass: "{{ pass }}"
  tasks:
    # ...

That said, you can move remote_user to variables too (refer to the whole list of connection parameters), save it to a separate host_vars- or group_vars-file and encrypt with Ansible Vault.

like image 106
techraf Avatar answered Sep 17 '25 10:09

techraf